-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_test.cc
186 lines (145 loc) · 5.75 KB
/
socket_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "tools/socket.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
using primihub::crypto::Status;
using primihub::crypto::network::ClientChannel;
using primihub::crypto::network::CryptoChannel;
using primihub::crypto::network::ServerChannel;
static std::string gen_random(uint32_t len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::string tmp_s;
tmp_s.reserve(len);
for (uint32_t i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
TEST(ChannelTest, SimpleSendRecvTest) {
std::string host("127.0.0.1");
std::string tag("test_tag");
std::shared_ptr<CryptoChannel> server_channel =
std::dynamic_pointer_cast<CryptoChannel>(
std::make_shared<ServerChannel>(host, 35056, tag));
auto status = server_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
std::shared_ptr<CryptoChannel> client_channel =
std::dynamic_pointer_cast<CryptoChannel>(
std::make_shared<ClientChannel>(host, 35056, tag));
status = client_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
{
std::string recv_buf;
recv_buf.resize(1024);
std::string send_buf = gen_random(1024);
auto recv_fut = server_channel->asyncRecv(recv_buf.data(), recv_buf.size());
auto send_fut = client_channel->asyncSend(send_buf.data(), send_buf.size());
auto send_status = recv_fut.get();
auto recv_status = send_fut.get();
EXPECT_EQ(recv_status.IsOK(), true);
EXPECT_EQ(send_status.IsOK(), true);
EXPECT_EQ(send_buf == recv_buf, true);
}
{
std::string send_buf = gen_random(1024);
std::string recv_buf;
recv_buf.resize(1024);
auto recv_fut = client_channel->asyncRecv(recv_buf.data(), recv_buf.size());
auto send_fut = server_channel->asyncSend(send_buf.data(), send_buf.size());
auto send_status = send_fut.get();
auto recv_status = recv_fut.get();
EXPECT_EQ(recv_status.IsOK(), true);
EXPECT_EQ(send_status.IsOK(), true);
EXPECT_EQ(send_buf == recv_buf, true);
}
}
TEST(ChannelTest, ForkTest) {
std::string host("127.0.0.1");
std::string tag("test_tag");
std::shared_ptr<ServerChannel> server_channel =
std::make_shared<ServerChannel>(host, 35056, tag);
auto status = server_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
std::shared_ptr<ClientChannel> client_channel =
std::make_shared<ClientChannel>(host, 35056, tag);
status = client_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
uint16_t fork_num = 10;
std::vector<std::shared_ptr<CryptoChannel>> client_fork_channels;
std::vector<std::shared_ptr<CryptoChannel>> server_fork_channels;
for (uint16_t i = 0; i < fork_num; i++) {
client_fork_channels.push_back(client_channel->fork());
server_fork_channels.push_back(server_channel->fork());
}
std::string send_buf = gen_random(1024);
auto send_fn = [&client_fork_channels, &send_buf]() {
std::vector<std::future<Status>> send_futs;
for (auto &channel : client_fork_channels)
send_futs.push_back(channel->asyncSend(send_buf.data(), send_buf.size()));
for (auto &fut : send_futs) EXPECT_EQ(fut.get().IsOK(), true);
};
auto recv_fn = [&server_fork_channels, &send_buf]() {
std::vector<std::string> all_recv_buf;
all_recv_buf.resize(server_fork_channels.size());
for (auto &recv_buf : all_recv_buf) recv_buf.resize(1024);
std::vector<std::future<Status>> recv_futs;
for (size_t i = 0; i < server_fork_channels.size(); i++)
recv_futs.push_back(server_fork_channels[i]->asyncRecv(
all_recv_buf[i].data(), all_recv_buf[i].size()));
for (auto &fut : recv_futs) EXPECT_EQ(fut.get().IsOK(), true);
for (auto &recv_buf : all_recv_buf) EXPECT_EQ(recv_buf == send_buf, true);
};
std::future<void> recv_fut = std::async(recv_fn);
std::future<void> send_fut = std::async(send_fn);
send_fut.get();
recv_fut.get();
}
TEST(channel_test, recv_resize_test) {
std::string host("127.0.0.1");
std::string tag("test_tag");
std::shared_ptr<ServerChannel> server_channel =
std::make_shared<ServerChannel>(host, 35056, tag);
auto status = server_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
std::shared_ptr<ClientChannel> client_channel =
std::make_shared<ClientChannel>(host, 35056, tag);
status = client_channel->initChannel();
EXPECT_EQ(status.IsOK(), true);
std::string send_msg = gen_random(102400);
std::string recv_msg{};
auto send_fn = [client_channel, &send_msg]() {
auto fut = client_channel->asyncSend(send_msg.data(), send_msg.size());
auto status = fut.get();
if (!status.IsOK()) LOG(ERROR) << "Send message failed.";
return status;
};
auto recv_fn = [server_channel, &recv_msg]() {
auto status = server_channel->recvResize(recv_msg);
if (!status.IsOK()) LOG(ERROR) << "Run recvResize failed.";
return status;
};
auto send_fut = std::async(send_fn);
auto recv_fut = std::async(recv_fn);
EXPECT_EQ(send_fut.get().IsOK(), true);
EXPECT_EQ(recv_fut.get().IsOK(), true);
EXPECT_EQ(recv_msg, send_msg);
send_msg = gen_random(102400);
recv_msg.clear();
auto send_fn2 = [server_channel, &send_msg]() {
auto fut = server_channel->asyncSend(send_msg.data(), send_msg.size());
auto status = fut.get();
if (!status.IsOK()) LOG(ERROR) << "Send message failed.";
return status;
};
auto recv_fn2 = [client_channel, &recv_msg]() {
auto status = client_channel->recvResize(recv_msg);
if (!status.IsOK()) LOG(ERROR) << "Run recvResize failed.";
return status;
};
auto send_fut2 = std::async(send_fn2);
auto recv_fut2 = std::async(recv_fn2);
EXPECT_EQ(send_fut2.get().IsOK(), true);
EXPECT_EQ(recv_fut2.get().IsOK(), true);
EXPECT_EQ(recv_msg, send_msg);
}