Loading...
Searching...
No Matches
resdb_txn_accessor_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
29
30namespace resdb {
31namespace {
32
33using ::resdb::testing::EqualsProto;
34using ::testing::_;
35using ::testing::ElementsAre;
36using ::testing::Invoke;
37using ::testing::Pointee;
38using ::testing::Return;
39
40class MockResDBTxnAccessor : public ResDBTxnAccessor {
41 public:
42 MockResDBTxnAccessor(const ResDBConfig& config) : ResDBTxnAccessor(config) {}
43 MOCK_METHOD(std::unique_ptr<NetChannel>, GetNetChannel,
44 (const std::string&, int), (override));
45};
46
47TEST(ResDBTxnAccessorTest, GetTransactionsFail) {
48 ResDBConfig config({GenerateReplicaInfo(1, "127.0.0.1", 1234),
49 GenerateReplicaInfo(2, "127.0.0.1", 1235),
50 GenerateReplicaInfo(3, "127.0.0.1", 1236),
51 GenerateReplicaInfo(4, "127.0.0.1", 1237)},
52 GenerateReplicaInfo(1, "127.0.0.1", 1234));
53
54 QueryRequest request;
55 request.set_min_seq(0);
56 request.set_max_seq(0);
57 MockResDBTxnAccessor client(config);
58 EXPECT_CALL(client, GetNetChannel)
59 .Times(4)
60 .WillRepeatedly(Invoke([&](const std::string& ip, int port) {
61 auto client = std::make_unique<MockNetChannel>(ip, port);
62 EXPECT_CALL(*client,
63 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
64 .WillOnce(Return(0));
65 EXPECT_CALL(*client, RecvRawMessageStr)
66 .WillOnce(Invoke([&](std::string* resp) { return -1; }));
67 return client;
68 }));
69 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
70 client.GetTxn(0, 0);
71 EXPECT_FALSE(resp.ok());
72}
73
74TEST(ResDBTxnAccessorTest, GetTransactions) {
75 ResDBConfig config({GenerateReplicaInfo(1, "127.0.0.1", 1234),
76 GenerateReplicaInfo(2, "127.0.0.1", 1235),
77 GenerateReplicaInfo(3, "127.0.0.1", 1236),
78 GenerateReplicaInfo(4, "127.0.0.1", 1237)},
79 GenerateReplicaInfo(1, "127.0.0.1", 1234));
80
81 QueryResponse query_resp;
82 auto txn = query_resp.add_transactions();
83 txn->set_seq(1);
84 txn->set_data("test_resp");
85
86 QueryRequest request;
87 request.set_min_seq(1);
88 request.set_max_seq(1);
89 MockResDBTxnAccessor client(config);
90 EXPECT_CALL(client, GetNetChannel)
91 .Times(4)
92 .WillRepeatedly(Invoke([&](const std::string& ip, int port) {
93 auto client = std::make_unique<MockNetChannel>(ip, port);
94 EXPECT_CALL(*client,
95 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
96 .WillOnce(Return(0));
97
98 EXPECT_CALL(*client, RecvRawMessageStr)
99 .WillOnce(Invoke([&](std::string* resp) {
100 query_resp.SerializeToString(resp);
101 return 0;
102 }));
103 return client;
104 }));
105
106 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
107 client.GetTxn(1, 1);
108 EXPECT_TRUE(resp.ok());
109 EXPECT_THAT(*resp, ElementsAre(std::make_pair(1, "test_resp")));
110}
111
112TEST(ResDBTxnAccessorTest, GetTransactionsOneDelay) {
113 ResDBConfig config({GenerateReplicaInfo(1, "127.0.0.1", 1234),
114 GenerateReplicaInfo(2, "127.0.0.1", 1235),
115 GenerateReplicaInfo(3, "127.0.0.1", 1236),
116 GenerateReplicaInfo(4, "127.0.0.1", 1237)},
117 GenerateReplicaInfo(1, "127.0.0.1", 1234));
118
119 QueryResponse query_resp;
120 auto txn = query_resp.add_transactions();
121 txn->set_seq(1);
122 txn->set_data("test_resp");
123
124 QueryRequest request;
125 request.set_min_seq(1);
126 request.set_max_seq(1);
127 MockResDBTxnAccessor client(config);
128 EXPECT_CALL(client, GetNetChannel)
129 .Times(4)
130 .WillRepeatedly(Invoke([&](const std::string& ip, int port) {
131 auto client = std::make_unique<MockNetChannel>(ip, port);
132 EXPECT_CALL(*client,
133 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
134 .WillOnce(Return(0));
135
136 if (port == 1237) {
137 EXPECT_CALL(*client, RecvRawMessageStr)
138 .WillOnce(Invoke([&](std::string* resp) {
139 sleep(2);
140 return 0;
141 }));
142 } else {
143 EXPECT_CALL(*client, RecvRawMessageStr)
144 .WillOnce(Invoke([&](std::string* resp) {
145 query_resp.SerializeToString(resp);
146 return 0;
147 }));
148 }
149 return client;
150 }));
151
152 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
153 client.GetTxn(1, 1);
154 EXPECT_TRUE(resp.ok());
155 EXPECT_THAT(*resp, ElementsAre(std::make_pair(1, "test_resp")));
156}
157
158} // namespace
159} // namespace resdb
MockResDBTxnAccessor(const ResDBConfig &config)
MOCK_METHOD((absl::StatusOr< std::vector< std::pair< uint64_t, std::string > > >), GetTxn,(uint64_t, uint64_t),(override))
virtual std::unique_ptr< NetChannel > GetNetChannel(const std::string &ip, int port)
ResDBTxnAccessor(const ResDBConfig &config)
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
TEST(NetworkUtilsTest, GetDNSName)