-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.cpp
260 lines (250 loc) · 7.72 KB
/
test_client.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "socket/msg.h"
#include "client/client.h"
#include "proto/NetworkTest.grpc.pb.h"
#include "proto/NetworkTest.pb.h"
#include <algorithm>
#include <arpa/inet.h>
#include <asm-generic/socket.h>
#include <bits/types/FILE.h>
#include <chrono>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <exception>
#include <fcntl.h>
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <grpcpp/support/status.h>
#include <memory>
#include <mutex>
#include <ostream>
#include <random>
#include <stdexcept>
#include <string>
#include <sys/socket.h>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
#include <vector>
class ClientTester {
friend void RunClientTest(std::shared_ptr<ClientTester> tester);
using NT = NetworkTest::NT;
using Stub = NetworkTest::NT::Stub;
using Result = NetworkTest::Result;
using runtime_error = std::runtime_error;
using Context = ::grpc::ClientContext;
std::unique_ptr<Stub> stub;
std::default_random_engine re;
std::uniform_int_distribution<char> AsciiStringGenerator;
std::uniform_int_distribution<char> BinStringGenerator;
std::uniform_int_distribution<uint32_t> LenGenerator;
int fd;
void QueryStatus(uint64_t idx, Result &response) {
if (idx < 0)
runtime_error("No Exist msg Idx<0\n");
if (idx <= SuccessMaxIdx) {
response.set_id(SuccessMaxIdx);
response.set_reason(Success);
return;
}
Context context;
NetworkTest::Query query;
query.set_id(idx);
auto res = stub->ResultQuery(&context, query, &response);
if (!res.ok())
runtime_error("Test Error,Please Retry!\n");
if (response.reason() >= ErrorLevel)
throw std::runtime_error(
ErrorCode2Msg(static_cast<TestStatus>(response.reason())));
if (response.reason() == Success)
SuccessMaxIdx = std::max(SuccessMaxIdx, response.id());
}
void SendAnswer(const std::string &s) {
SendSeq++;
Context context;
Result response;
::NetworkTest::Register answer;
answer.set_content(s);
auto res = stub->AnswerRegister(&context, answer, &response);
if (!res.ok())
runtime_error("Test Error,Please Retry!\n");
if (response.reason() >= ErrorLevel)
throw std::runtime_error(
ErrorCode2Msg(static_cast<TestStatus>(response.reason())));
if (response.reason() == Success)
SuccessMaxIdx = std::max(SuccessMaxIdx, response.id());
}
uint32_t SendSeq = -1;
uint32_t SuccessMaxIdx = -1;
static const char *ErrorCode2Msg(TestStatus s) noexcept {
switch (s) {
case Success:
return "Success";
case Wait:
return "Wait For Msg";
case WaitRPC:
return "Wait For Test";
case Diff:
return "Msg is Error";
case Unknow:
return "Unknow Error";
case ErrorLevel:
case TestError:;
}
return "Tester is Error";
}
TestStatus Check() {
using namespace std::chrono_literals;
Result response;
QueryStatus(SendSeq, response);
if (response.id() == SendSeq && response.reason() == Success)
return Success;
std::this_thread::sleep_for(3s);
return (response.id() == SendSeq && response.reason() == Success)
? Success
: static_cast<TestStatus>(response.reason());
}
void genAsciiMsg(uint64_t size) {
for (uint64_t i = 0; i < size; i++) {
auto len = LenGenerator(re);
auto ch = AsciiStringGenerator(re);
std::string s(len, ch);
SendAnswer(s);
msgs->push(std::move(s));
}
}
void genBinMsg(uint64_t size) {
for (uint64_t i = 0; i < size; i++) {
auto len = LenGenerator(re);
std::string s;
for (auto t = 0; t < len; t++)
s.push_back(BinStringGenerator(re));
SendAnswer(s);
msgs->push(std::move(s));
}
}
uint64_t getSeed() {
fd = open("/dev/urandom", O_RDONLY);
uint64_t seed;
auto rc = read(fd, &seed, sizeof(seed));
if (rc != sizeof(seed))
throw runtime_error("read /dev/random failed");
return seed;
}
public:
ClientTester(std::string addr)
: stub(NT::NewStub(
grpc::CreateChannel(addr, grpc::InsecureChannelCredentials()))),
re(getSeed()), msgs(std::make_shared<MsgBuf>()),
AsciiStringGenerator(' ', '~'), BinStringGenerator(CHAR_MIN, CHAR_MAX),
LenGenerator(0, 4096) {}
std::shared_ptr<MsgBuf> msgs;
void FinishCheck() {
auto res = Check();
if (res == Success) {
puts("Congratulations! You Pass The Test!");
_exit(0);
}
printf("Sorry! You did not pass all Test. Reason:%s :(\n",
ErrorCode2Msg(res));
}
};
void RunClientTest(std::shared_ptr<ClientTester> tester) {
try {
using namespace std::chrono_literals;
tester->genAsciiMsg(1);
std::this_thread::sleep_for(2s);
auto reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 1\n");
}
tester->genAsciiMsg(1);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 2\n");
}
tester->genAsciiMsg(1);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 3\n");
}
tester->genBinMsg(1);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 4\n");
}
tester->genBinMsg(1);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 5\n");
}
tester->genBinMsg(1);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 6\n");
}
tester->genAsciiMsg(1024);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 7\n");
}
tester->genBinMsg(1024);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 8\n");
}
tester->genAsciiMsg(1024);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 9\n");
}
tester->genBinMsg(1024);
reslut = tester->Check();
if (reslut != Success) {
printf("QAQ: Failed at 10\n");
}
printf("Success Pass All Test\n");
_exit(0);
} catch (...) {
printf("Exception:\n");
}
}
std::shared_ptr<MsgBuf> InitTestClient(std::string addr) {
try {
auto tester = std::make_shared<ClientTester>(addr);
auto test_thread = std::thread(RunClientTest, tester);
test_thread.detach();
return tester->msgs;
} catch (std::exception &e) {
printf("Exception: %s\n", e.what());
}
return nullptr;
}
struct Message {
int msgID;
int partID;
std::string data;
};
class mess {
int partid;
int len;
};
int main() {
// Server 端的监听地址
auto msg = InitTestClient("127.0.0.1:1234");
TcpClient client;
bool ret = client.connectToHost(ServerAddr,PORT);
if (!ret) {
return -1;
}
while (1) {
std::string str = msg->pop();
client.getSocket()->sendMsg(str);
}
}