Loading...
Searching...
No Matches
replica_communicator_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 <gtest/gtest.h>
23
24#include <future>
25
29
30namespace resdb {
31namespace {
32
33using ::testing::Invoke;
34using ::testing::Return;
35
36ReplicaInfo GenerateReplicaInfo(const std::string& ip, int port) {
37 ReplicaInfo replica_info;
38 replica_info.set_ip(ip);
39 replica_info.set_port(port);
40 return replica_info;
41}
42
43class MockReplicaCommunicator : public ReplicaCommunicator {
44 public:
45 MockReplicaCommunicator(const std::vector<ReplicaInfo>& replicas,
46 bool use_lonn_conn = false)
47 : ReplicaCommunicator(replicas, nullptr, use_lonn_conn){};
48 MOCK_METHOD(std::unique_ptr<NetChannel>, GetClient, (const std::string&, int),
49 (override));
50 MOCK_METHOD(AsyncReplicaClient*, GetClientFromPool, (const std::string&, int),
51 (override));
52};
53
54TEST(ReplicaCommunicatorTest, SendMessage) {
55 std::vector<ReplicaInfo> replicas;
56 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1234));
57 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1235));
58
59 MockReplicaCommunicator client(replicas);
60 EXPECT_CALL(client, GetClient("127.0.0.1", 1234))
61 .WillOnce(Invoke([&](const std::string& ip, int port) {
62 return std::make_unique<MockNetChannel>(ip, port);
63 }));
64 EXPECT_CALL(client, GetClient("127.0.0.1", 1235))
65 .WillOnce(Invoke([&](const std::string& ip, int port) {
66 return std::make_unique<MockNetChannel>(ip, port);
67 }));
68
69 Request expected_request;
70 expected_request.set_type(Request::TYPE_HEART_BEAT);
71 EXPECT_EQ(client.SendMessage(expected_request), 2);
72}
73
74TEST(ReplicaCommunicatorTest, SendMessageToClient) {
75 std::vector<ReplicaInfo> replicas;
76 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1234));
77 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1235));
78
79 MockReplicaCommunicator client(replicas);
80
81 std::vector<ReplicaInfo> client_replicas;
82 client_replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1236));
83 client_replicas.back().set_id(3);
84
85 client.UpdateClientReplicas(client_replicas);
86
87 EXPECT_CALL(client, GetClient("127.0.0.1", 1236))
88 .WillOnce(Invoke([&](const std::string& ip, int port) {
89 return std::make_unique<MockNetChannel>(ip, port);
90 }));
91
92 Request expected_request;
93 expected_request.set_type(Request::TYPE_HEART_BEAT);
94 client.SendMessage(expected_request, 3);
95}
96
97TEST(ReplicaCommunicatorTest, PartialSendMessage) {
98 std::vector<ReplicaInfo> replicas;
99 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1234));
100 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1235));
101
102 MockReplicaCommunicator client(replicas);
103 EXPECT_CALL(client, GetClient("127.0.0.1", 1234))
104 .WillOnce(Invoke([&](const std::string& ip, int port) {
105 return std::make_unique<MockNetChannel>(ip, port);
106 }));
107 EXPECT_CALL(client, GetClient("127.0.0.1", 1235))
108 .WillOnce(
109 Invoke([&](const std::string& ip, int port) { return nullptr; }));
110
111 Request expected_request;
112 expected_request.set_type(Request::TYPE_HEART_BEAT);
113 EXPECT_EQ(client.SendMessage(expected_request), 1);
114}
115
116TEST(ReplicaCommunicatorTest, Lonnconnection) {
117 std::promise<bool> bc;
118 std::future<bool> bc_done = bc.get_future();
119 std::vector<ReplicaInfo> replicas;
120 replicas.push_back(GenerateReplicaInfo("127.0.0.1", 1234));
121
122 boost::asio::io_service io_service;
123 auto resdb_client = std::make_unique<MockAsyncReplicaClient>(&io_service);
124 MockReplicaCommunicator client(replicas, true);
125 EXPECT_CALL(client, GetClientFromPool("127.0.0.1", 1234))
126 .WillOnce(Invoke([&](const std::string& ip, int port) {
127 bc.set_value(true);
128 return resdb_client.get();
129 }));
130
131 Request expected_request;
132 expected_request.set_type(Request::TYPE_HEART_BEAT);
133 EXPECT_EQ(client.SendMessage(expected_request), 0);
134 bc_done.get();
135}
136
137} // namespace
138
139} // namespace resdb
MOCK_METHOD(int, SendHeartBeat,(const Request &),(override))
ReplicaCommunicator(const std::vector< ReplicaInfo > &replicas, SignatureVerifier *verifier=nullptr, bool is_use_long_conn=false, int epoll_num=1, int tcp_batch=100)
virtual std::unique_ptr< NetChannel > GetClient(const std::string &ip, int port)
virtual AsyncReplicaClient * GetClientFromPool(const std::string &ip, int port)
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
TEST(NetworkUtilsTest, GetDNSName)