Loading...
Searching...
No Matches
viewchange_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
26#include <future>
27
35#include "platform/proto/checkpoint_info.pb.h"
36
37namespace resdb {
38namespace {
39
40using ::resdb::testing::EqualsProto;
41using ::testing::_;
42using ::testing::Invoke;
43using ::testing::Return;
44using ::testing::Test;
45
46class ViewChangeManagerTest : public Test {
47 public:
48 ViewChangeManagerTest()
49 : // just set the monitor time to 1 second to return early.
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(3, "127.0.0.1", 1234)),
55 system_info_(config_) {
56 config_.EnableCheckPoint(true);
57 config_.SetViewchangeCommitTimeout(1000); // set to 1s
58 checkpoint_manager_ = std::make_unique<CheckPointManager>(
59 config_, &replica_communicator_, &mock_verifier_);
60 message_manager_ = std::make_unique<MessageManager>(
61 config_, nullptr, checkpoint_manager_.get(), &system_info_);
62 manager_ = std::make_unique<ViewChangeManager>(
63 config_, checkpoint_manager_.get(), message_manager_.get(),
64 &system_info_, &replica_communicator_, &mock_verifier_);
65
66 ON_CALL(mock_verifier_, SignMessage).WillByDefault(Return(SignatureInfo()));
67 ON_CALL(mock_verifier_, VerifyMessage(_, EqualsProto(SignatureInfo())))
68 .WillByDefault(Return(true));
69 }
70
71 protected:
72 ResDBConfig config_;
73 MockSignatureVerifier mock_verifier_;
74 SystemInfo system_info_;
75 std::unique_ptr<CheckPointManager> checkpoint_manager_;
76 std::unique_ptr<MessageManager> message_manager_;
77 std::unique_ptr<ViewChangeManager> manager_;
78 MockReplicaCommunicator replica_communicator_;
79};
80
81TEST_F(ViewChangeManagerTest, SendViewChange) {
82 manager_->MayStart();
83 std::unique_ptr<Request> request = std::make_unique<Request>();
84 request->set_seq(1);
85 checkpoint_manager_->AddCommitData(std::move(request));
86 std::promise<bool> propose_done;
87 std::future<bool> propose_done_future = propose_done.get_future();
88 EXPECT_CALL(replica_communicator_, BroadCast)
89 .WillOnce(Invoke([&](const google::protobuf::Message& message) {
90 propose_done.set_value(true);
91 }));
92 propose_done_future.get();
93}
94
95TEST_F(ViewChangeManagerTest, SendNewView) {
96 manager_->MayStart();
97 std::promise<bool> propose_done;
98 std::future<bool> propose_done_future = propose_done.get_future();
99 EXPECT_CALL(replica_communicator_, BroadCast)
100 .WillRepeatedly(Invoke([&](const google::protobuf::Message& message) {
101 Request viewchange_request;
102 viewchange_request.CopyFrom(message);
103 if (viewchange_request.type() == Request::TYPE_NEWVIEW) {
104 propose_done.set_value(true);
105 }
106 }));
107
108 for (int i = 0; i < 3; ++i) {
109 ViewChangeMessage viewchange_message;
110 std::unique_ptr<Request> request =
111 NewRequest(Request::TYPE_VIEWCHANGE, Request(), i + 1);
112 viewchange_message.set_view_number(3);
113 viewchange_message.SerializeToString(request->mutable_data());
114
115 int ret = manager_->ProcessViewChange(std::make_unique<Context>(),
116 std::move(request));
117 EXPECT_EQ(ret, 0);
118 }
119 propose_done_future.get();
120}
121
122} // namespace
123
124} // namespace resdb
MockReplicaCommunicator replica_communicator_
void AddCommitData(std::unique_ptr< Request > request)
CheckPointManager checkpoint_manager_
SystemInfo system_info_
std::unique_ptr< MessageManager > message_manager_
std::unique_ptr< Request > NewRequest(Request::Type type, const Request &request, int sender_id)
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
TEST_F(ResDBTest, TestPBFTService)
std::unique_ptr< ResDBConfig > config_
ResponseManager manager_
MockSignatureVerifier mock_verifier_