22#include <glog/logging.h>
23#include <gmock/gmock.h>
24#include <gtest/gtest.h>
33using ::resdb::testing::EqualsProto;
35using ::testing::ElementsAre;
36using ::testing::Invoke;
37using ::testing::Pointee;
38using ::testing::Return;
40class MockResDBTxnAccessor :
public ResDBTxnAccessor {
44 (
const std::string&,
int), (
override));
47TEST(ResDBTxnAccessorTest, GetTransactionsFail) {
55 request.set_min_seq(0);
56 request.set_max_seq(0);
57 MockResDBTxnAccessor client(config);
58 EXPECT_CALL(client, GetNetChannel)
60 .WillRepeatedly(Invoke([&](
const std::string& ip,
int port) {
61 auto client = std::make_unique<MockNetChannel>(ip, port);
63 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
65 EXPECT_CALL(*client, RecvRawMessageStr)
66 .WillOnce(Invoke([&](std::string* resp) {
return -1; }));
69 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
71 EXPECT_FALSE(resp.ok());
74TEST(ResDBTxnAccessorTest, GetTransactions) {
81 QueryResponse query_resp;
82 auto txn = query_resp.add_transactions();
84 txn->set_data(
"test_resp");
87 request.set_min_seq(1);
88 request.set_max_seq(1);
89 MockResDBTxnAccessor client(config);
90 EXPECT_CALL(client, GetNetChannel)
92 .WillRepeatedly(Invoke([&](
const std::string& ip,
int port) {
93 auto client = std::make_unique<MockNetChannel>(ip, port);
95 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
98 EXPECT_CALL(*client, RecvRawMessageStr)
99 .WillOnce(Invoke([&](std::string* resp) {
100 query_resp.SerializeToString(resp);
106 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
108 EXPECT_TRUE(resp.ok());
109 EXPECT_THAT(*resp, ElementsAre(std::make_pair(1,
"test_resp")));
112TEST(ResDBTxnAccessorTest, GetTransactionsOneDelay) {
119 QueryResponse query_resp;
120 auto txn = query_resp.add_transactions();
122 txn->set_data(
"test_resp");
124 QueryRequest request;
125 request.set_min_seq(1);
126 request.set_max_seq(1);
127 MockResDBTxnAccessor client(config);
128 EXPECT_CALL(client, GetNetChannel)
130 .WillRepeatedly(Invoke([&](
const std::string& ip,
int port) {
131 auto client = std::make_unique<MockNetChannel>(ip, port);
133 SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
134 .WillOnce(Return(0));
137 EXPECT_CALL(*client, RecvRawMessageStr)
138 .WillOnce(Invoke([&](std::string* resp) {
143 EXPECT_CALL(*client, RecvRawMessageStr)
144 .WillOnce(Invoke([&](std::string* resp) {
145 query_resp.SerializeToString(resp);
152 absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
154 EXPECT_TRUE(resp.ok());
155 EXPECT_THAT(*resp, ElementsAre(std::make_pair(1,
"test_resp")));
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)