Loading...
Searching...
No Matches
async_replica_client_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
27
28namespace resdb {
29namespace {
30
31TEST(AsyncReplicaClientTest, SendMessage) {
32 std::promise<bool> bc;
33 std::future<bool> bc_done = bc.get_future();
34
35 std::thread svr_thead = std::thread([&]() {
36 TcpSocket svr_socket;
37 svr_socket.Listen("127.0.0.1", 1234);
38 auto client_socket = svr_socket.Accept();
39 char *buf = nullptr;
40 size_t len = 0;
41 int ret = client_socket->Recv((void **)&buf, &len);
42 EXPECT_EQ(ret, 4);
43 EXPECT_EQ(std::string(buf, len), "test");
44 bc.set_value(true);
45 free(buf);
46 client_socket->Close();
47 svr_socket.Close();
48 });
49
50 boost::asio::io_service io_service;
51 std::thread t([&]() { io_service.run(); });
52
53 boost::asio::io_service::work *work =
54 new boost::asio::io_service::work(io_service);
55 AsyncReplicaClient client(&io_service, "127.0.0.1", 1234);
56 EXPECT_EQ(client.SendMessage("test"), 0);
57 bc_done.get();
58 delete work;
59 work = nullptr;
60 svr_thead.join();
61 t.join();
62}
63
64TEST(AsyncReplicaClientTest, SendLargeMessage) {
65 std::promise<bool> bc;
66 std::future<bool> bc_done = bc.get_future();
67
68 std::thread svr_thead = std::thread([&]() {
69 TcpSocket svr_socket;
70 svr_socket.Listen("127.0.0.1", 1234);
71 auto client_socket = svr_socket.Accept();
72 char *buf = nullptr;
73 size_t len = 0;
74 int ret = client_socket->Recv((void **)&buf, &len);
75 EXPECT_EQ(ret, 1000000);
76 EXPECT_EQ(std::string(buf, len), std::string(1000000, 't'));
77 bc.set_value(true);
78 free(buf);
79 client_socket->Close();
80 svr_socket.Close();
81 });
82
83 boost::asio::io_service io_service;
84 std::thread t([&]() { io_service.run(); });
85
86 boost::asio::io_service::work *work =
87 new boost::asio::io_service::work(io_service);
88 AsyncReplicaClient client(&io_service, "127.0.0.1", 1234);
89 EXPECT_EQ(client.SendMessage(std::string(1000000, 't')), 0);
90 bc_done.get();
91 delete work;
92 work = nullptr;
93 svr_thead.join();
94 t.join();
95}
96
97TEST(AsyncReplicaClientTest, MultiSendMessage) {
98 std::promise<bool> bc;
99 std::future<bool> bc_done = bc.get_future();
100
101 std::thread svr_thead = std::thread([&]() {
102 TcpSocket svr_TcpSocket;
103 svr_TcpSocket.Listen("127.0.0.1", 1234);
104 auto client_socket = svr_TcpSocket.Accept();
105 char *buf = nullptr;
106 size_t len = 0;
107 for (int i = 0; i < 10; ++i) {
108 int ret = client_socket->Recv((void **)&buf, &len);
109 EXPECT_EQ(ret, 4);
110 EXPECT_EQ(std::string(buf, len), "test");
111 }
112 bc.set_value(true);
113 free(buf);
114 });
115
116 boost::asio::io_service io_service;
117 std::thread t([&]() { io_service.run(); });
118 boost::asio::io_service::work *work =
119 new boost::asio::io_service::work(io_service);
120 AsyncReplicaClient client(&io_service, "127.0.0.1", 1234);
121 for (int i = 0; i < 10; ++i) {
122 EXPECT_EQ(client.SendMessage("test"), 0);
123 }
124 bc_done.get();
125 delete work;
126 work = nullptr;
127 svr_thead.join();
128 t.join();
129}
130
131TEST(AsyncReplicaClientTest, Reconnect) {
132 std::promise<bool> bc1, bc2;
133 std::future<bool> bc1_done = bc1.get_future(), bc2_done = bc2.get_future();
134
135 std::thread svr_thead = std::thread([&]() {
136 TcpSocket svr_TcpSocket;
137 svr_TcpSocket.Listen("127.0.0.1", 1234);
138 for (int j = 0; j < 2; ++j) {
139 auto client_socket = svr_TcpSocket.Accept();
140 char *buf = nullptr;
141 size_t len = 0;
142 int ret = client_socket->Recv((void **)&buf, &len);
143 EXPECT_EQ(ret, 4);
144 EXPECT_EQ(std::string(buf, len), "test");
145 client_socket->Close();
146 free(buf);
147 if (j == 0) bc2.set_value(true);
148 }
149 bc1.set_value(true);
150 });
151
152 boost::asio::io_service io_service;
153 std::thread t([&]() { io_service.run(); });
154 boost::asio::io_service::work *work =
155 new boost::asio::io_service::work(io_service);
156 AsyncReplicaClient client(&io_service, "127.0.0.1", 1234);
157 for (int i = 0; i < 2; ++i) {
158 EXPECT_EQ(client.SendMessage("test"), 0);
159 if (i == 0) bc2_done.get();
160 }
161 bc1_done.get();
162 delete work;
163 work = nullptr;
164 svr_thead.join();
165 t.join();
166}
167
168} // namespace
169
170} // namespace resdb
TEST(NetworkUtilsTest, GetDNSName)