/
test_network_worker.cpp
300 lines (235 loc) · 8.86 KB
/
test_network_worker.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright (C) 2008 - 2014 by Pauli Nieminen <paniemin@cc.hut.fi>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-test"
#include <boost/test/unit_test.hpp>
#include "utils/auto_parameterized.hpp"
#include "utils/predicate.hpp"
#include "network_worker.hpp"
#include "thread.hpp"
#include "filesystem.hpp"
#include "config.hpp"
#include "game_config.hpp"
/**
* Test networking to prevent bugs there.
* Try to create all kind of unlikely error conditions
* Some test should lock management_mutex from worker to settup stress test
* It is like that this will need some threading also :(
*/
BOOST_AUTO_TEST_SUITE( test_network )
const int TEST_PORT = 15010;
const int MIN_THREADS = 1;
const int MAX_THREADS = 5;
const std::string LOCALHOST = "localhost";
network::manager* wes_manager;
network::server_manager* wes_server;
network::connection client_client1;
network::connection client_client2;
network::connection server_client1;
network::connection server_client2;
BOOST_AUTO_TEST_CASE( test_connect )
{
int connections = network::nconnections();
BOOST_WARN_MESSAGE(connections == 0, "There is open "<< connections <<" connections before test!");
BOOST_CHECK_MESSAGE(wes_manager = new network::manager(MIN_THREADS,MAX_THREADS), "network::manager failed to initialize");
BOOST_CHECK_MESSAGE(wes_server = new network::server_manager(TEST_PORT,network::server_manager::MUST_CREATE_SERVER),
"network::server_manager failed to initialize");
BOOST_REQUIRE_MESSAGE(wes_server->is_running(), "Can't start server!");
client_client1 = network::connect(LOCALHOST, TEST_PORT);
BOOST_CHECK_MESSAGE(client_client1 > 0, "Can't connect to server");
server_client1 = network::accept_connection();
BOOST_CHECK_MESSAGE(server_client1 > 0, "Can't accept connection");
}
template<class T>
static network::connection receive(T& cfg, int max_tries = 100)
{
network::connection receive_con;
while ((receive_con = network::receive_data(cfg)) == network::null_connection)
{
// loop untill data is received
SDL_Delay(50);
if (--max_tries <= 0)
{
BOOST_WARN_MESSAGE(max_tries > 0,"receiving data took too long. Preventing for ever loop");
break;
}
}
return receive_con;
}
BOOST_AUTO_TEST_CASE( test_send_client )
{
config cfg_send;
config& child = cfg_send.add_child("test_client_send");
child["test"] = "yes!";
cfg_send["test_running"] = true;
network::send_data(cfg_send, client_client1);
network::connection receive_from;
config received;
receive_from = receive(received);
BOOST_CHECK_MESSAGE( receive_from == server_client1, "Received data is not from test client 1" );
BOOST_CHECK_EQUAL(cfg_send, received);
}
class connect_aborter : public threading::waiter
{
public:
connect_aborter() : start_(SDL_GetTicks())
{}
ACTION process();
private:
size_t start_;
};
connect_aborter::ACTION connect_aborter::process()
{
// Abort connection after 5 ms
if(SDL_GetTicks() - start_ >= 5) {
return ABORT;
} else {
return WAIT;
}
}
#if 0
BOOST_AUTO_TEST_CASE( test_sdl_thread_wait_crash )
{
delete wes_server;
wes_server = 0;
delete wes_manager;
wes_manager = 0;
BOOST_CHECK_MESSAGE(wes_manager == 0, "network::manager nono zero after delete");
BOOST_CHECK_MESSAGE(wes_manager = new network::manager(MIN_THREADS,MAX_THREADS), "network::manager failed to initialize");
BOOST_CHECK_THROW(client_client1 = network::connect(LOCALHOST, TEST_PORT), network::error);
BOOST_CHECK_MESSAGE(client_client1 > 0, "Can't connect to server");
delete wes_manager;
wes_manager = 0;
BOOST_CHECK_MESSAGE(wes_manager = new network::manager(MIN_THREADS,MAX_THREADS), "network::manager failed to initialize");
connect_aborter aborter;
BOOST_CHECK_MESSAGE((client_client1 = network::connect("server.wesnoth.org", 15000, aborter)) == 0, "Connection create but not shoul");
delete wes_manager;
BOOST_CHECK_MESSAGE(wes_manager = new network::manager(MIN_THREADS,MAX_THREADS), "network::manager failed to initialize");
BOOST_CHECK_MESSAGE(wes_server = new network::server_manager(TEST_PORT,network::server_manager::MUST_CREATE_SERVER), "");
BOOST_CHECK_MESSAGE((client_client1 = network::connect(LOCALHOST, TEST_PORT, aborter)) == 0, "Connection create but not shoul");
delete wes_manager;
wes_manager = new network::manager(MIN_THREADS,MAX_THREADS);
client_client1 = network::connect(LOCALHOST, TEST_PORT);
BOOST_CHECK_MESSAGE(client_client1 > 0, "Can't connect to server");
delete wes_server;
delete wes_manager;
wes_manager = new network::manager(MIN_THREADS,MAX_THREADS);
wes_server = new network::server_manager(TEST_PORT,network::server_manager::MUST_CREATE_SERVER);
}
#endif
// Use 1kb, 500kb and 10Mb files for testing
struct sendfile_param {
sendfile_param(size_t size, bool system) : size_(size), system_(system) {}
size_t size_;
bool system_;
};
sendfile_param sendfile_sizes[] = {sendfile_param(1*1024,true),
sendfile_param(5*1024*1024,true),
sendfile_param(1*1024,false),
sendfile_param(5*1024*1024,false)};
static std::string create_random_sendfile(size_t size)
{
char buffer[1024];
const int buffer_size = sizeof(buffer)/sizeof(buffer[0]);
int *begin = reinterpret_cast<int*>(&buffer[0]);
int *end = begin + sizeof(buffer)/sizeof(int);
std::string filename = "sendfile.tmp";
filesystem::scoped_ostream file = filesystem::ostream_file(filename);
std::generate(begin,end,std::rand);
while( size > 0
&& !file->bad())
{
file->write(buffer, buffer_size);
size -= buffer_size;
}
return filename;
}
static void delete_random_sendfile(const std::string& file)
{
filesystem::delete_file(file);
}
template<class T>
class auto_resetter {
T& value_to_change_;
T old_val_;
public:
auto_resetter(const T& new_value, T& value_to_change) : value_to_change_(value_to_change), old_val_(value_to_change)
{
value_to_change_ = new_value;
}
~auto_resetter()
{
value_to_change_ = old_val_;
}
};
WESNOTH_PARAMETERIZED_TEST_CASE( test_multi_sendfile, sendfile_param, sendfile_sizes, size )
{
auto_resetter<std::string> path("", game_config::path);
network::set_raw_data_only();
std::string file = create_random_sendfile(size.size_);
network_worker_pool::set_use_system_sendfile(size.system_);
network::connection cl_client1, se_client1;
network::connection cl_client2, se_client2;
network::connection cl_client3, se_client3;
BOOST_CHECK_MESSAGE((cl_client1 = network::connect(LOCALHOST, TEST_PORT)) > 0, "Can't connect to server!");
BOOST_CHECK_MESSAGE((se_client1 = network::accept_connection()) > 0, "Coulnd't accept new connection");
BOOST_CHECK_MESSAGE((cl_client2 = network::connect(LOCALHOST, TEST_PORT)) > 0, "Can't connect to server!");
BOOST_CHECK_MESSAGE((se_client2 = network::accept_connection()) > 0, "Coulnd't accept new connection");
BOOST_CHECK_MESSAGE((cl_client3 = network::connect(LOCALHOST, TEST_PORT)) > 0, "Can't connect to server!");
BOOST_CHECK_MESSAGE((se_client3 = network::accept_connection()) > 0, "Coulnd't accept new connection");
network::send_file(file, cl_client1);
network::send_file(file, cl_client2);
network::send_file(file, cl_client3);
std::vector<char> data;
BOOST_CHECK_PREDICATE(test_utils::one_of<network::connection> , (receive(data,500))(3)(se_client1)(se_client2)(se_client3));
BOOST_CHECK_EQUAL(data.size(), static_cast<size_t>(filesystem::file_size(file)));
BOOST_CHECK_PREDICATE(test_utils::one_of<network::connection> , (receive(data,500))(3)(se_client1)(se_client2)(se_client3));
BOOST_CHECK_EQUAL(data.size(), static_cast<size_t>(filesystem::file_size(file)));
BOOST_CHECK_PREDICATE(test_utils::one_of<network::connection> , (receive(data,500))(3)(se_client1)(se_client2)(se_client3));
BOOST_CHECK_EQUAL(data.size(), static_cast<size_t>(filesystem::file_size(file)));
network::disconnect(cl_client1);
network::disconnect(cl_client2);
network::disconnect(cl_client3);
BOOST_CHECK_THROW(receive(data),network::error);
BOOST_CHECK_THROW(receive(data),network::error);
BOOST_CHECK_THROW(receive(data),network::error);
delete_random_sendfile(file);
}
#if 0
BOOST_AUTO_TEST_CASE( test_multiple_connections )
{
}
BOOST_AUTO_TEST_CASE( test_cancel_transfer )
{
}
BOOST_AUTO_TEST_CASE( test_detect_errors )
{
}
BOOST_AUTO_TEST_CASE( test_broken_data )
{
}
BOOST_AUTO_TEST_CASE( test_config_with_macros )
{
}
BOOST_AUTO_TEST_CASE( test_disconnect )
{
network::disconnect(client_client1);
network::disconnect(server_client1);
}
#endif
BOOST_AUTO_TEST_CASE( test_shutdown )
{
delete wes_server;
BOOST_CHECK_MESSAGE(true,"Not true");
delete wes_manager;
}
BOOST_AUTO_TEST_SUITE_END()
/* vim: set ts=4 sw=4: */