Loading...
Searching...
No Matches
geo_transaction_executor_test.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23#include <gmock/gmock.h>
24#include <gtest/gtest.h>
25
26#include <future>
27
33
34namespace resdb {
35namespace {
36using ::resdb::testing::EqualsProto;
37using ::testing::_;
38using ::testing::Invoke;
39using ::testing::Matcher;
40using ::testing::Return;
41using ::testing::Test;
42
43MATCHER_P(SendMessageMatcher, replica, "") { return true; }
44
45TEST(LocalExecutorTest, PushToQueue) {
46 auto mock_executor = std::make_unique<MockTransactionManager>();
47 auto replica_communicator = std::make_unique<MockReplicaCommunicator>();
48 ResConfigData config_data_ = ResConfigData();
49 config_data_.set_self_region_id(1);
50
51 // setup region id=1
52 RegionInfo* region = config_data_.add_region();
53 region->set_region_id(1);
54 *region->add_replica_info() = GenerateReplicaInfo(1, "127.0.0.1", 10001);
55 *region->add_replica_info() = GenerateReplicaInfo(2, "127.0.0.1", 10002);
56 *region->add_replica_info() = GenerateReplicaInfo(3, "127.0.0.1", 10003);
57 *region->add_replica_info() = GenerateReplicaInfo(4, "127.0.0.1", 10004);
58
59 // setup region id=2
60 region = config_data_.add_region();
61 region->set_region_id(2);
62 *region->add_replica_info() = GenerateReplicaInfo(6, "127.0.0.1", 10006);
63 *region->add_replica_info() = GenerateReplicaInfo(7, "127.0.0.1", 10007);
64 *region->add_replica_info() = GenerateReplicaInfo(8, "127.0.0.1", 10008);
65 *region->add_replica_info() = GenerateReplicaInfo(9, "127.0.0.1", 10009);
66
67 ResDBConfig config_ =
68 ResDBConfig(config_data_, GenerateReplicaInfo(1, "127.0.0.1", 10001),
69 KeyInfo(), CertificateInfo());
70 auto system_info_ = std::make_unique<SystemInfo>(config_);
71 // primary node id: 2
73 BatchUserRequest batch_request;
74 batch_request.set_seq(1);
75 batch_request.add_user_requests()->mutable_request()->set_data(
76 "local_execute_test");
77
78 ReplicaInfo rep_info = GenerateReplicaInfo(1, "127.0.0.1", 10001);
79
80 std::promise<bool> done;
81 std::future<bool> done_future = done.get_future();
82 EXPECT_CALL(*replica_communicator, SendBatchMessage).WillOnce(Invoke([&]() {
83 done.set_value(true);
84 return 0;
85 }));
86
87 GeoTransactionExecutor local_executor = GeoTransactionExecutor(
88 config_, std::move(system_info_), std::move(replica_communicator),
89 std::move(mock_executor));
90 LOG(ERROR) << "exe";
91 local_executor.ExecuteBatch(batch_request);
92 done_future.get();
93}
94
95TEST(LocalExecutorTest, PrimaryNodeBroadcast) {
96 auto mock_executor = std::make_unique<MockTransactionManager>();
97 auto replica_communicator = std::make_unique<MockReplicaCommunicator>();
98 ResConfigData config_data_ = ResConfigData();
99 config_data_.set_self_region_id(1);
100
101 // setup region id=1
102 RegionInfo* region = config_data_.add_region();
103 region->set_region_id(1);
104 *region->add_replica_info() = GenerateReplicaInfo(1, "127.0.0.1", 10001);
105 *region->add_replica_info() = GenerateReplicaInfo(2, "127.0.0.1", 10002);
106 *region->add_replica_info() = GenerateReplicaInfo(3, "127.0.0.1", 10003);
107 *region->add_replica_info() = GenerateReplicaInfo(4, "127.0.0.1", 10004);
108
109 // setup region id=2
110 region = config_data_.add_region();
111 region->set_region_id(2);
112 *region->add_replica_info() = GenerateReplicaInfo(6, "127.0.0.1", 10006);
113 *region->add_replica_info() = GenerateReplicaInfo(7, "127.0.0.1", 10007);
114 *region->add_replica_info() = GenerateReplicaInfo(8, "127.0.0.1", 10008);
115 *region->add_replica_info() = GenerateReplicaInfo(9, "127.0.0.1", 10009);
116
117 ResDBConfig config_ =
118 ResDBConfig(config_data_, GenerateReplicaInfo(1, "127.0.0.1", 10001),
119 KeyInfo(), CertificateInfo());
120 auto system_info_ = std::make_unique<SystemInfo>(config_);
121 // primary node id: 1
123 BatchUserRequest batch_request;
124 batch_request.set_seq(1);
125 batch_request.add_user_requests()->mutable_request()->set_data(
126 "local_execute_test");
127
128 ReplicaInfo rep_info = GenerateReplicaInfo(1, "127.0.0.1", 10001);
129
130 int call_times = 0;
131 std::promise<bool> done;
132 std::future<bool> done_future = done.get_future();
133 EXPECT_CALL(*replica_communicator, SendBatchMessage)
134 .WillRepeatedly(Invoke([&]() {
135 if (++call_times >= 3) {
136 done.set_value(true);
137 }
138 return 0;
139 }));
140
141 GeoTransactionExecutor local_executor = GeoTransactionExecutor(
142 config_, std::move(system_info_), std::move(replica_communicator),
143 std::move(mock_executor));
144 local_executor.ExecuteBatch(batch_request);
145 done_future.get();
146}
147
148} // namespace
149
150} // namespace resdb
void SetPrimary(uint32_t id)
SystemInfo system_info_
MATCHER_P(EqualsProto, replica, "")
Definition test_macros.h:36
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
TEST(NetworkUtilsTest, GetDNSName)
std::unique_ptr< ResDBConfig > config_