Loading...
Searching...
No Matches
transaction_constructor_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
28#include "platform/proto/client_test.pb.h"
29
30namespace resdb {
31namespace {
32
33using ::resdb::testing::EqualsProto;
34using ::testing::_;
35using ::testing::Invoke;
36using ::testing::Return;
37using ::testing::Test;
38
39std::string GenerateRequestData(const ClientTestRequest& client_request,
40 bool need_resp = false) {
41 ResDBMessage message;
42
43 Request request;
44 request.set_type(Request::TYPE_CLIENT_REQUEST);
45 request.set_need_response(need_resp);
46 client_request.SerializeToString(request.mutable_data());
47 request.SerializeToString(message.mutable_data());
48
49 std::string request_data;
50 message.SerializeToString(&request_data);
51
52 return request_data;
53}
54
55std::string GenerateResponseData(const ClientTestResponse& client_responses) {
56 std::string response_data;
57 client_responses.SerializeToString(&response_data);
58 return response_data;
59}
60
61class UserClientTest : public Test {
62 public:
63 UserClientTest() {
64 self_info_.set_ip("127.0.0.1");
65 self_info_.set_port(1234);
66
67 dest_info_.set_ip("127.0.0.1");
68 dest_info_.set_port(1235);
69 replicas_.push_back(dest_info_);
70
71 KeyInfo private_key;
72 private_key.set_key("private_key");
73 config_ = std::make_unique<ResDBConfig>(replicas_, self_info_, private_key,
74 CertificateInfo());
75 config_->SetClientTimeoutMs(10000);
76 }
77
78 protected:
79 ReplicaInfo dest_info_;
80 ReplicaInfo self_info_;
81 std::vector<ReplicaInfo> replicas_;
82 std::unique_ptr<ResDBConfig> config_;
83};
84
85TEST_F(UserClientTest, OnlySendRequestOK) {
86 ClientTestRequest client_request;
87 client_request.set_value("test_value");
88
89 std::unique_ptr<MockSocket> socket = std::make_unique<MockSocket>();
90 EXPECT_CALL(*socket, Connect(dest_info_.ip(), dest_info_.port()))
91 .WillOnce(Return(0));
92 EXPECT_CALL(*socket, Send(GenerateRequestData(client_request)))
93 .WillOnce(Return(0));
94 EXPECT_CALL(*socket, Recv(_, _)).Times(0);
95
96 TransactionConstructor client(*config_);
97 client.SetSocket(std::move(socket));
98 client.SetSignatureVerifier(nullptr);
99
100 int ret = client.SendRequest(client_request, Request::TYPE_CLIENT_REQUEST);
101 EXPECT_EQ(ret, 0);
102}
103
104TEST_F(UserClientTest, GetResponse) {
105 ClientTestRequest client_request;
106 client_request.set_value("test_value");
107
108 ClientTestResponse client_response, expected_response;
109 expected_response.set_value("ack");
110
111 std::unique_ptr<MockSocket> socket = std::make_unique<MockSocket>();
112 EXPECT_CALL(*socket, Connect(dest_info_.ip(), dest_info_.port()))
113 .WillOnce(Return(0));
114 EXPECT_CALL(*socket, Send(GenerateRequestData(client_request, true)))
115 .WillOnce(Return(0));
116 EXPECT_CALL(*socket, Recv(_, _))
117 .WillOnce(Invoke([&](void** buf, size_t* len) {
118 ClientTestResponse response;
119 response.set_value("ack");
120
121 std::string resp_str = GenerateResponseData(response);
122
123 *len = resp_str.size();
124 *buf = malloc(*len);
125 memcpy(*buf, resp_str.c_str(), *len);
126 return *len;
127 }));
128
129 TransactionConstructor client(*config_);
130 client.SetSocket(std::move(socket));
131 EXPECT_EQ(client.SendRequest(client_request, &client_response,
132 Request::TYPE_CLIENT_REQUEST),
133 0);
134 EXPECT_THAT(client_response, EqualsProto(expected_response));
135}
136
137TEST_F(UserClientTest, RecvResponseFail) {
138 ClientTestRequest client_request;
139 client_request.set_value("test_value");
140
141 ClientTestResponse client_response;
142
143 std::unique_ptr<MockSocket> socket = std::make_unique<MockSocket>();
144 EXPECT_CALL(*socket, Connect(dest_info_.ip(), dest_info_.port()))
145 .WillOnce(Return(0));
146 EXPECT_CALL(*socket, Send(GenerateRequestData(client_request, true)))
147 .WillOnce(Return(0));
148 EXPECT_CALL(*socket, Recv(_, _)).WillOnce(Return(-1));
149
150 TransactionConstructor client(*config_);
151 client.SetSocket(std::move(socket));
152
153 EXPECT_NE(client.SendRequest(client_request, &client_response,
154 Request::TYPE_CLIENT_REQUEST),
155 0);
156}
157
158} // namespace
159
160} // namespace resdb
TEST_F(ResDBTest, TestPBFTService)
ReplicaInfo self_info_
std::vector< ReplicaInfo > replicas_
std::unique_ptr< ResDBConfig > config_
ReplicaInfo dest_info_