Loading...
Searching...
No Matches
resdb_state_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 <gtest/gtest.h>
24
27
28namespace resdb {
29namespace {
30
31using ::google::protobuf::util::MessageDifferencer;
32using ::testing::ElementsAre;
33using ::testing::Invoke;
34using ::testing::Test;
35
36void AddReplicaToList(const std::string& ip, int port,
37 std::vector<ReplicaInfo>* replica) {
38 ReplicaInfo info;
39 info.set_ip(ip);
40 info.set_port(port);
41 replica->push_back(info);
42}
43
44class MockResDBStateAccessor : public ResDBStateAccessor {
45 public:
46 MockResDBStateAccessor(const ResDBConfig& config)
47 : ResDBStateAccessor(config) {}
48
49 MOCK_METHOD(std::unique_ptr<NetChannel>, GetNetChannel,
50 (const std::string&, int), (override));
51};
52
53class StateClientTest : public Test {
54 public:
55 StateClientTest() {
56 self_info_.set_ip("127.0.0.1");
57 self_info_.set_port(1234);
58
59 AddReplicaToList("127.0.0.1", 1235, &replicas_);
60 AddReplicaToList("127.0.0.1", 1236, &replicas_);
61 AddReplicaToList("127.0.0.1", 1237, &replicas_);
62 AddReplicaToList("127.0.0.1", 1238, &replicas_);
63
64 KeyInfo private_key;
65 private_key.set_key("private_key");
66 config_ = std::make_unique<ResDBConfig>(replicas_, self_info_, private_key,
67 CertificateInfo());
68 }
69
70 protected:
71 ReplicaInfo self_info_;
72 std::vector<ReplicaInfo> replicas_;
73 std::unique_ptr<ResDBConfig> config_;
74};
75
76TEST_F(StateClientTest, GetAllReplicaState) {
77 MockResDBStateAccessor client(*config_);
78
79 ReplicaState state;
80 auto region = state.mutable_replica_config()->add_region();
81
82 for (auto replica : replicas_) {
83 *region->add_replica_info() = replica;
84 }
85
86 std::atomic<int> idx = 0;
87 EXPECT_CALL(client, GetNetChannel)
88 .Times(1)
89 .WillRepeatedly(Invoke([&](const std::string& ip, int port) {
90 auto client = std::make_unique<MockNetChannel>(ip, port);
91 EXPECT_CALL(*client, RecvRawMessage)
92 .WillRepeatedly(Invoke([&](google::protobuf::Message* message) {
93 *reinterpret_cast<ReplicaState*>(message) = state;
94 return 0;
95 }));
96 return client;
97 }));
98 auto ret = client.GetReplicaStates();
99 EXPECT_TRUE(ret.ok());
100 std::set<int> results;
101 for (auto& state : *ret) {
102 auto it = std::find_if(
103 replicas_.begin(), replicas_.end(), [&](const ReplicaInfo& info) {
104 return MessageDifferencer::Equals(info, state.replica_info());
105 });
106 EXPECT_TRUE(it != replicas_.end());
107 results.insert(it - replicas_.begin());
108 }
109 EXPECT_EQ(results.size(), 4);
110}
111
112} // namespace
113
114} // namespace resdb
TEST_F(ResDBTest, TestPBFTService)
ReplicaInfo self_info_
std::vector< ReplicaInfo > replicas_
std::unique_ptr< ResDBConfig > config_