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 Dec 2020
18 * \brief
19 */
20
21#include "query/equal_task.h"
22#include <gtest/gtest.h>
23#include "index/mock_segment.h" // for MockSegment
24#include "mock_query_context.h" // for Mock*Context
25
26TEST(EqualTaskTest, TestTaskRun) {
27 { // Test Invalid arguments
28 MockEqualQueryContext context;
29 EqualTask task(nullptr, &context);
30 ASSERT_TRUE(task.run() != 0);
31 EqualTask task1(nullptr, nullptr);
32 ASSERT_TRUE(task1.run() != 0);
33 EqualTask task2(std::make_shared<MockSegment>(), nullptr);
34 ASSERT_TRUE(task2.run() != 0);
35 }
36
37 {
38 MockEqualQueryContext context;
39 EXPECT_CALL(context, primary_key()).WillRepeatedly(Return(1u));
40
41 auto segment = std::make_shared<MockSegment>();
42
43 { // Failed
44 EXPECT_CALL(*segment, kv_search(_, _))
45 .WillOnce(Return(1))
46 .RetiresOnSaturation();
47
48 EqualTask task(segment, &context);
49 task.status(Task::Status::SCHEDULED);
50 EXPECT_EQ(task.run(), 1);
51 EXPECT_EQ(task.exit_code(), 1);
52 }
53
54 {
55 EXPECT_CALL(*segment, kv_search(_, _))
56 .WillOnce(Return(0))
57 .RetiresOnSaturation();
58
59 EqualTask task(segment, &context);
60 task.status(Task::Status::SCHEDULED);
61 EXPECT_EQ(task.run(), 0);
62 EXPECT_EQ(task.exit_code(), 0);
63 EXPECT_TRUE(task.wait_finish());
64 EXPECT_TRUE(task.finished());
65 // EXPECT_TRUE(task.forward());
66 }
67
68 {
69 EXPECT_CALL(*segment, kv_search(_, _))
70 .WillOnce(Invoke([](uint64_t primary_key, QueryResult *result) {
71 EXPECT_EQ(primary_key, 1);
72 result->primary_key = 1;
73 result->revision = 2;
74 return 0;
75 }))
76 .RetiresOnSaturation();
77
78 EqualTask task(segment, &context);
79 task.status(Task::Status::SCHEDULED);
80 EXPECT_EQ(task.run(), 0);
81 EXPECT_EQ(task.exit_code(), 0);
82 EXPECT_TRUE(task.wait_finish());
83 EXPECT_TRUE(task.finished());
84 EXPECT_EQ(task.hit(), 1);
85
86 EXPECT_EQ(task.forward().primary_key, 1);
87 EXPECT_EQ(task.forward().revision, 2);
88 }
89 }
90}
91