File: test_Net.h

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (340 lines) | stat: -rw-r--r-- 8,880 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Copyright (C) 2014 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "lib/self_test.h"

#include "graphics/TerrainTextureManager.h"
#include "lib/external_libraries/enet.h"
#include "lib/external_libraries/libsdl.h"
#include "lib/tex/tex.h"
#include "network/NetServer.h"
#include "network/NetClient.h"
#include "network/NetTurnManager.h"
#include "ps/CLogger.h"
#include "ps/Game.h"
#include "ps/Filesystem.h"
#include "ps/Loader.h"
#include "ps/XML/Xeromyces.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"

class TestNetComms : public CxxTest::TestSuite
{
public:
	void setUp()
	{
		g_VFS = CreateVfs(20 * MiB);
		TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/"mods"/"public", VFS_MOUNT_MUST_EXIST));
		TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir()/"_testcache"));
		CXeromyces::Startup();

		// Need some stuff for terrain movement costs:
		// (TODO: this ought to be independent of any graphics code)
		new CTerrainTextureManager;
		g_TexMan.LoadTerrainTextures();

		enet_initialize();
	}

	void tearDown()
	{
		enet_deinitialize();

		delete &g_TexMan;

		CXeromyces::Terminate();
		g_VFS.reset();
		DeleteDirectory(DataDir()/"_testcache");
	}

	bool clients_are_all(const std::vector<CNetClient*>& clients, uint state)
	{
		for (size_t j = 0; j < clients.size(); ++j)
			if (clients[j]->GetCurrState() != state)
				return false;
		return true;
	}

	void connect(CNetServer& server, const std::vector<CNetClient*>& clients)
	{
		TS_ASSERT(server.SetupConnection());
		for (size_t j = 0; j < clients.size(); ++j)
			TS_ASSERT(clients[j]->SetupConnection("127.0.0.1"));

		for (size_t i = 0; ; ++i)
		{
//			debug_printf(L".");
			for (size_t j = 0; j < clients.size(); ++j)
				clients[j]->Poll();

			if (clients_are_all(clients, NCS_PREGAME))
				break;

			if (i > 20)
			{
				TS_FAIL("connection timeout");
				break;
			}

			SDL_Delay(100);
		}
	}

#if 0
	void disconnect(CNetServer& server, const std::vector<CNetClient*>& clients)
	{
		for (size_t i = 0; ; ++i)
		{
//			debug_printf(L".");
			server.Poll();
			for (size_t j = 0; j < clients.size(); ++j)
				clients[j]->Poll();

			if (server.GetState() == SERVER_STATE_UNCONNECTED && clients_are_all(clients, NCS_UNCONNECTED))
				break;

			if (i > 20)
			{
				TS_FAIL("disconnection timeout");
				break;
			}

			SDL_Delay(100);
		}
	}
#endif

	void wait(const std::vector<CNetClient*>& clients, size_t msecs)
	{
		for (size_t i = 0; i < msecs/10; ++i)
		{
			for (size_t j = 0; j < clients.size(); ++j)
				clients[j]->Poll();

			SDL_Delay(10);
		}
	}

	void test_basic_DISABLED()
	{
		// This doesn't actually test much, it just runs a very quick multiplayer game
		// and prints a load of debug output so you can see if anything funny's going on

		ScriptInterface scriptInterface("Engine", "Test", g_ScriptRuntime);
		JSContext* cx = scriptInterface.GetContext();
		JSAutoRequest rq(cx);
		
		TestStdoutLogger logger;

		std::vector<CNetClient*> clients;

		CGame client1Game(true);
		CGame client2Game(true);
		CGame client3Game(true);

		CNetServer server;

		JS::RootedValue attrs(cx);
		scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs);
		server.UpdateGameAttributes(&attrs, scriptInterface);

		CNetClient client1(&client1Game);
		CNetClient client2(&client2Game);
		CNetClient client3(&client3Game);

		clients.push_back(&client1);
		clients.push_back(&client2);
		clients.push_back(&client3);

		connect(server, clients);
		debug_printf(L"%ls", client1.TestReadGuiMessages().c_str());

		server.StartGame();
		SDL_Delay(100);
		for (size_t j = 0; j < clients.size(); ++j)
		{
			clients[j]->Poll();
			TS_ASSERT_OK(LDR_NonprogressiveLoad());
			clients[j]->LoadFinished();
		}

		wait(clients, 100);

		{
			CScriptValRooted cmd;
			client1.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client1 test sim command]\\n'})", cmd);
			client1Game.GetTurnManager()->PostCommand(cmd);
		}

		{
			CScriptValRooted cmd;
			client2.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client2 test sim command]\\n'})", cmd);
			client2Game.GetTurnManager()->PostCommand(cmd);
		}

		wait(clients, 100);
		client1Game.GetTurnManager()->Update(1.0f, 1);
		client2Game.GetTurnManager()->Update(1.0f, 1);
		client3Game.GetTurnManager()->Update(1.0f, 1);
		wait(clients, 100);
		client1Game.GetTurnManager()->Update(1.0f, 1);
		client2Game.GetTurnManager()->Update(1.0f, 1);
		client3Game.GetTurnManager()->Update(1.0f, 1);
		wait(clients, 100);
	}

	void test_rejoin_DISABLED()
	{
		ScriptInterface scriptInterface("Engine", "Test", g_ScriptRuntime);
		JSContext* cx = scriptInterface.GetContext();
		JSAutoRequest rq(cx);
		
		TestStdoutLogger logger;

		std::vector<CNetClient*> clients;

		CGame client1Game(true);
		CGame client2Game(true);
		CGame client3Game(true);

		CNetServer server;

		JS::RootedValue attrs(cx);
		scriptInterface.Eval("({mapType:'scenario',map:'maps/scenarios/Saharan Oases',mapPath:'maps/scenarios/',thing:'example'})", &attrs);
		server.UpdateGameAttributes(&attrs, scriptInterface);

		CNetClient client1(&client1Game);
		CNetClient client2(&client2Game);
		CNetClient client3(&client3Game);

		client1.SetUserName(L"alice");
		client2.SetUserName(L"bob");
		client3.SetUserName(L"charlie");

		clients.push_back(&client1);
		clients.push_back(&client2);
		clients.push_back(&client3);

		connect(server, clients);
		debug_printf(L"%ls", client1.TestReadGuiMessages().c_str());

		server.StartGame();
		SDL_Delay(100);
		for (size_t j = 0; j < clients.size(); ++j)
		{
			clients[j]->Poll();
			TS_ASSERT_OK(LDR_NonprogressiveLoad());
			clients[j]->LoadFinished();
		}

		wait(clients, 100);

		{
			CScriptValRooted cmd;
			client1.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client1 test sim command 1]\\n'})", cmd);
			client1Game.GetTurnManager()->PostCommand(cmd);
		}

		wait(clients, 100);
		client1Game.GetTurnManager()->Update(1.0f, 1);
		client2Game.GetTurnManager()->Update(1.0f, 1);
		client3Game.GetTurnManager()->Update(1.0f, 1);
		wait(clients, 100);

		{
			CScriptValRooted cmd;
			client1.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client1 test sim command 2]\\n'})", cmd);
			client1Game.GetTurnManager()->PostCommand(cmd);
		}

		debug_printf(L"==== Disconnecting client 2\n");

		client2.DestroyConnection();
		clients.erase(clients.begin()+1);

		debug_printf(L"==== Connecting client 2B\n");

		CGame client2BGame(true);
		CNetClient client2B(&client2BGame);
		client2B.SetUserName(L"bob");
		clients.push_back(&client2B);

		TS_ASSERT(client2B.SetupConnection("127.0.0.1"));

		for (size_t i = 0; ; ++i)
		{
			debug_printf(L"[%u]\n", client2B.GetCurrState());
			client2B.Poll();
			if (client2B.GetCurrState() == NCS_PREGAME)
				break;

			if (client2B.GetCurrState() == NCS_UNCONNECTED)
			{
				TS_FAIL("connection rejected");
				return;
			}

			if (i > 20)
			{
				TS_FAIL("connection timeout");
				return;
			}

			SDL_Delay(100);
		}

		wait(clients, 100);

		client1Game.GetTurnManager()->Update(1.0f, 1);
		client3Game.GetTurnManager()->Update(1.0f, 1);
		wait(clients, 100);
		server.SetTurnLength(100);
		client1Game.GetTurnManager()->Update(1.0f, 1);
		client3Game.GetTurnManager()->Update(1.0f, 1);
		wait(clients, 100);

		// (This SetTurnLength thing doesn't actually detect errors unless you change
		// CNetTurnManager::TurnNeedsFullHash to always return true)

		{
			CScriptValRooted cmd;
			client1.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client1 test sim command 3]\\n'})", cmd);
			client1Game.GetTurnManager()->PostCommand(cmd);
		}


		clients[2]->Poll();
		TS_ASSERT_OK(LDR_NonprogressiveLoad());
		clients[2]->LoadFinished();

		wait(clients, 100);

		{
			CScriptValRooted cmd;
			client1.GetScriptInterface().Eval("({type:'debug-print', message:'[>>> client1 test sim command 4]\\n'})", cmd);
			client1Game.GetTurnManager()->PostCommand(cmd);
		}

		for (size_t i = 0; i < 3; ++i)
		{
			client1Game.GetTurnManager()->Update(1.0f, 1);
			client2BGame.GetTurnManager()->Update(1.0f, 1);
			client3Game.GetTurnManager()->Update(1.0f, 1);
			wait(clients, 100);
		}
	}
};