Loading...
Searching...
No Matches
response_manager_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
30
31namespace resdb {
32namespace {
33
34using ::testing::_;
35using ::testing::Invoke;
36using ::testing::Test;
37
38CertificateInfo GetCertificateInfo() {
39 CertificateInfo info;
40 info.mutable_public_key()->mutable_public_key_info()->set_type(
41 CertificateKeyInfo::CLIENT);
42 return info;
43}
44
45class ResponseManagerTest : public Test {
46 public:
47 ResponseManagerTest()
48 : // just set the monitor time to 1 second to return early.
49 global_stats_(Stats::GetGlobalStats(1)),
50 config_({GenerateReplicaInfo(1, "127.0.0.1", 1234),
51 GenerateReplicaInfo(2, "127.0.0.1", 1235),
52 GenerateReplicaInfo(3, "127.0.0.1", 1236),
53 GenerateReplicaInfo(4, "127.0.0.1", 1237)},
54 GenerateReplicaInfo(1, "127.0.0.1", 1234), KeyInfo(),
55 GetCertificateInfo()),
56 system_info_(config_),
57 manager_(config_, &replica_communicator_, &system_info_, nullptr) {
58 global_stats_->Stop();
59 }
60
61 int AddResponseMsg(int sender_id, BatchUserResponse batch_resp) {
62 Request request;
63 request.set_current_view(1);
64 request.set_seq(1);
65 request.set_type(Request::TYPE_RESPONSE);
66 request.set_sender_id(sender_id);
67 request.set_data("resp_data");
68 batch_resp.SerializeToString(request.mutable_data());
69
70 return manager_.ProcessResponseMsg(std::make_unique<Context>(),
71 std::make_unique<Request>(request));
72 }
73
74 protected:
76 ResDBConfig config_;
77 SystemInfo system_info_;
78 MockReplicaCommunicator replica_communicator_;
79 ResponseManager manager_;
80};
81
82TEST_F(ResponseManagerTest, GetConext) {
83 Request request;
84 request.set_seq(1);
85
86 std::vector<std::unique_ptr<Context>> list(2);
87
88 EXPECT_EQ(manager_.AddContextList(std::move(list), 1), 0);
89 EXPECT_TRUE(manager_.FetchContextList(2).empty());
90
91 EXPECT_FALSE(manager_.FetchContextList(1).empty());
92 EXPECT_TRUE(manager_.FetchContextList(1).empty());
93}
94
95TEST_F(ResponseManagerTest, SendUserRequest) {
96 auto context = std::make_unique<Context>();
97 context->signature.set_signature("signature");
98
99 std::promise<bool> propose_done;
100 std::future<bool> propose_done_future = propose_done.get_future();
101 EXPECT_CALL(replica_communicator_, SendMessage(_, 1)).WillOnce(Invoke([&]() {
102 propose_done.set_value(true);
103 }));
104
105 EXPECT_EQ(
106 manager_.NewUserRequest(std::move(context), std::make_unique<Request>()),
107 0);
108 propose_done_future.get();
109}
110
111TEST_F(ResponseManagerTest, ProcessResponse) {
112 std::unique_ptr<MockNetChannel> channel =
113 std::make_unique<MockNetChannel>("127.0.0.1", 0);
114
115 EXPECT_CALL(*channel, SendRawMessageData).Times(1);
116
117 uint64_t local_id = 1;
118
119 BatchUserResponse batch_resp;
120 *batch_resp.add_response() = "test";
121 *batch_resp.add_signatures() = SignatureInfo();
122 batch_resp.set_local_id(local_id);
123
124 Request request;
125 request.set_sender_id(1);
126 request.set_current_view(1);
127 request.set_seq(1);
128 request.set_proxy_id(1);
129
130 auto context = std::make_unique<Context>();
131 context->client = std::move(channel);
132 context->signature.set_signature("signature");
133
134 std::vector<std::unique_ptr<Context>> context_list;
135 context_list.push_back(std::move(context));
136 manager_.AddContextList(std::move(context_list), local_id);
137
138 EXPECT_EQ(AddResponseMsg(1, batch_resp), 0);
139 EXPECT_EQ(AddResponseMsg(2, batch_resp), 0);
140}
141
142TEST_F(ResponseManagerTest, ProcessResponseWithMoreResp) {
143 std::unique_ptr<MockNetChannel> channel =
144 std::make_unique<MockNetChannel>("127.0.0.1", 0);
145
146 EXPECT_CALL(*channel, SendRawMessageData).Times(1);
147
148 uint64_t local_id = 1;
149
150 BatchUserResponse batch_resp;
151 *batch_resp.add_response() = "test";
152 *batch_resp.add_signatures() = SignatureInfo();
153 batch_resp.set_local_id(local_id);
154
155 Request request;
156 request.set_sender_id(1);
157 request.set_current_view(1);
158 request.set_seq(1);
159 request.set_proxy_id(1);
160
161 auto context = std::make_unique<Context>();
162 context->client = std::move(channel);
163 context->signature.set_signature("signature");
164
165 std::vector<std::unique_ptr<Context>> context_list;
166 context_list.push_back(std::move(context));
167 manager_.AddContextList(std::move(context_list), local_id);
168
169 EXPECT_EQ(AddResponseMsg(1, batch_resp), 0);
170 EXPECT_EQ(AddResponseMsg(2, batch_resp), 0);
171 // do nothing.
172 EXPECT_EQ(AddResponseMsg(3, batch_resp), -2);
173}
174
175TEST_F(ResponseManagerTest, ProcessResponseWithSameSender) {
176 std::unique_ptr<MockNetChannel> channel =
177 std::make_unique<MockNetChannel>("127.0.0.1", 0);
178
179 EXPECT_CALL(*channel, SendRawMessageData).Times(0);
180
181 uint64_t local_id = 1;
182
183 BatchUserResponse batch_resp;
184 *batch_resp.add_response() = "test";
185 *batch_resp.add_signatures() = SignatureInfo();
186 batch_resp.set_local_id(local_id);
187
188 Request request;
189 request.set_sender_id(1);
190 request.set_current_view(1);
191 request.set_seq(1);
192 request.set_proxy_id(1);
193
194 auto context = std::make_unique<Context>();
195 context->client = std::move(channel);
196 context->signature.set_signature("signature");
197
198 std::vector<std::unique_ptr<Context>> context_list;
199 context_list.push_back(std::move(context));
200 manager_.AddContextList(std::move(context_list), local_id);
201
202 EXPECT_EQ(AddResponseMsg(1, batch_resp), 0);
203 EXPECT_EQ(AddResponseMsg(1, batch_resp), 0);
204}
205
206} // namespace
207
208} // namespace resdb
MockReplicaCommunicator replica_communicator_
std::vector< std::unique_ptr< Context > > FetchContextList(uint64_t id)
int AddContextList(std::vector< std::unique_ptr< Context > > context, uint64_t id)
int NewUserRequest(std::unique_ptr< Context > context, std::unique_ptr< Request > user_request)
Stats * global_stats_
SystemInfo system_info_
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
TEST_F(ResDBTest, TestPBFTService)
std::unique_ptr< ResDBConfig > config_
ResponseManager manager_