1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * \author guonix
17 * \date Nov 2020
18 * \brief
19 */
20
21#include "query/executor/bthread_queue.h"
22#include <chrono>
23#include <ratio>
24#include <gtest/gtest.h>
25#include "common/error_code.h"
26#include "task-inl.h"
27
28using namespace proxima::be::query;
29using namespace proxima::be::query::test;
30
31const std::string kName("task name");
32const int kCode = 0;
33const int kMillSeconds = 1000;
34
35TEST(BThreadQueueTest, TestDefaultContructor) {
36 BThreadQueue queue;
37 ASSERT_EQ(queue.start(), 0);
38 ASSERT_TRUE(queue.started());
39 ASSERT_EQ(queue.stop(), 0);
40 ASSERT_EQ(queue.join(), 0);
41 ASSERT_FALSE(queue.started());
42}
43
44TEST(BThreadQueueTest, TestPutOperation) {
45 BThreadQueue queue;
46 ASSERT_EQ(queue.start(), 0);
47 ASSERT_TRUE(queue.started());
48 TaskPtr task = CreateTask(kName, kCode, kMillSeconds);
49 ASSERT_EQ(queue.put(task), 0);
50 ASSERT_TRUE(task->wait_finish());
51 ASSERT_EQ(queue.stop(), 0);
52 ASSERT_EQ(queue.join(), 0);
53 ASSERT_FALSE(queue.started());
54}
55
56TEST(BThreadQueueTest, TestFalseOperation) {
57 BThreadQueue queue;
58 ASSERT_EQ(queue.start(), 0);
59 ASSERT_TRUE(queue.started());
60 TaskPtr task = CreateTask(kName, kCode, kMillSeconds);
61 ASSERT_TRUE(queue.start() != 0);
62 ASSERT_TRUE(queue.join() != 0);
63
64 ASSERT_EQ(queue.put(task), 0);
65 // Task should sleep for 100ms,
66 ASSERT_TRUE(static_cast<int>(task->status()) >=
67 static_cast<int>(Task::Status::SCHEDULED));
68
69 ASSERT_EQ(queue.stop(), 0);
70 ASSERT_TRUE(queue.start() != 0);
71
72 ASSERT_EQ(queue.join(), 0);
73
74 // Can't enqueue task
75 ASSERT_EQ(queue.put(task), PROXIMA_BE_ERROR_CODE(RuntimeError));
76
77 ASSERT_FALSE(queue.started());
78}
79