ManyClientsOneServerDeallocateBlockingTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "ManyClientsOneServerDeallocateBlockingTest.h"
  11. void ManyClientsOneServerDeallocateBlockingTest::WaitForConnectionRequestsToComplete(RakPeerInterface **clientList, int clientNum, bool isVerbose)
  12. {
  13. SystemAddress currentSystem;
  14. bool msgWasPrinted=false;
  15. for (int i=0;i<clientNum;i++)
  16. {
  17. currentSystem.SetBinaryAddress("127.0.0.1");
  18. currentSystem.port=60000;
  19. while (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,false,true,true) )
  20. {
  21. if (msgWasPrinted==false)
  22. {
  23. printf("Waiting for connection requests to complete.\n");
  24. msgWasPrinted=true;
  25. }
  26. RakSleep(30);
  27. }
  28. }
  29. }
  30. void ManyClientsOneServerDeallocateBlockingTest::WaitAndPrintResults(RakPeerInterface **clientList, int clientNum, bool isVerbose,RakPeerInterface *server)
  31. {
  32. WaitForConnectionRequestsToComplete(clientList,clientNum,isVerbose);
  33. Packet *packet;
  34. if (isVerbose)
  35. printf("For server\n");
  36. for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive())
  37. {
  38. switch (packet->data[0])
  39. {
  40. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  41. if (isVerbose)
  42. printf("Another client has disconnected.\n");
  43. break;
  44. case ID_REMOTE_CONNECTION_LOST:
  45. if (isVerbose)
  46. printf("Another client has lost the connection.\n");
  47. break;
  48. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  49. if (isVerbose)
  50. printf("Another client has connected.\n");
  51. break;
  52. case ID_CONNECTION_REQUEST_ACCEPTED:
  53. if (isVerbose)
  54. printf("Our connection request has been accepted.\n");
  55. break;
  56. case ID_CONNECTION_ATTEMPT_FAILED:
  57. if (isVerbose)
  58. printf("A connection has failed.\n");
  59. break;
  60. case ID_NEW_INCOMING_CONNECTION:
  61. if (isVerbose)
  62. printf("A connection is incoming.\n");
  63. break;
  64. case ID_NO_FREE_INCOMING_CONNECTIONS:
  65. if (isVerbose)
  66. printf("The server is full.\n");
  67. break;
  68. case ID_ALREADY_CONNECTED:
  69. if (isVerbose)
  70. printf("Already connected\n");
  71. break;
  72. case ID_DISCONNECTION_NOTIFICATION:
  73. if (isVerbose)
  74. printf("We have been disconnected.\n");
  75. break;
  76. case ID_CONNECTION_LOST:
  77. if (isVerbose)
  78. printf("Connection lost.\n");
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. //RakSleep(100);
  85. // Log all events per peer
  86. for (int i=0;i<clientNum;i++)//Receive for all peers
  87. {
  88. if (isVerbose)
  89. printf("For client %i\n",i);
  90. for (packet=clientList[i]->Receive(); packet; clientList[i]->DeallocatePacket(packet), packet=clientList[i]->Receive())
  91. {
  92. switch (packet->data[0])
  93. {
  94. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  95. if (isVerbose)
  96. printf("Another client has disconnected.\n");
  97. break;
  98. case ID_REMOTE_CONNECTION_LOST:
  99. if (isVerbose)
  100. printf("Another client has lost the connection.\n");
  101. break;
  102. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  103. if (isVerbose)
  104. printf("Another client has connected.\n");
  105. break;
  106. case ID_CONNECTION_REQUEST_ACCEPTED:
  107. if (isVerbose)
  108. printf("Our connection request has been accepted.\n");
  109. break;
  110. case ID_CONNECTION_ATTEMPT_FAILED:
  111. if (isVerbose)
  112. printf("A connection has failed.\n");
  113. break;
  114. case ID_NEW_INCOMING_CONNECTION:
  115. if (isVerbose)
  116. printf("A connection is incoming.\n");
  117. break;
  118. case ID_NO_FREE_INCOMING_CONNECTIONS:
  119. if (isVerbose)
  120. printf("The server is full.\n");
  121. break;
  122. case ID_ALREADY_CONNECTED:
  123. if (isVerbose)
  124. printf("Already connected\n");
  125. break;
  126. case ID_DISCONNECTION_NOTIFICATION:
  127. if (isVerbose)
  128. printf("We have been disconnected.\n");
  129. break;
  130. case ID_CONNECTION_LOST:
  131. if (isVerbose)
  132. printf("Connection lost.\n");
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. /*
  141. The server timeout for this test is set to one second.
  142. What is being done here is having 256 clients connect to a server.
  143. Then the client is deallocated.
  144. It is put to sleep for double the timeout amount.
  145. After that the timeout should trigger and the clients register as disconnected.
  146. Then the connection is started again.
  147. Do this for about 30 seconds.
  148. It is put to sleep for double the timeout amount.
  149. Then allow them all to connect for one last time.
  150. This version waits for connect and such in a loop, blocking execution so it is a blocking test.
  151. Good ideas for changes:
  152. After the last check run a eightpeers like test an add the conditions
  153. of that test as well.
  154. Success conditions:
  155. All connected normally.
  156. Failure conditions:
  157. Doesn't reconnect normally.
  158. During the very first connect loop any connect returns false.
  159. Connect function returns false and peer is not connected to anything,pending a connection, or disconnecting.
  160. GetTimeoutTime does not match the set timeout.
  161. RakPeerInterface Functions used, tested indirectly by its use:
  162. Startup
  163. Connect
  164. SetMaximumIncomingConnections
  165. Receive
  166. Send
  167. DeallocatePacket
  168. GetSystemList
  169. SetMaximumIncomingConnections
  170. RakPeerInterface Functions Explicitly Tested:
  171. GetTimeoutTime
  172. SetTimeoutTime
  173. Connect
  174. IsConnected
  175. */
  176. int ManyClientsOneServerDeallocateBlockingTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  177. {
  178. SystemAddress currentSystem;
  179. //Initializations of the arrays
  180. for (int i=0;i<clientNum;i++)
  181. {
  182. clientList[i]=RakPeerInterface::GetInstance();
  183. clientList[i]->Startup(1,&SocketDescriptor(), 1);
  184. }
  185. server=RakPeerInterface::GetInstance();
  186. server->Startup(clientNum, &SocketDescriptor(60000,0), 1);
  187. server->SetMaximumIncomingConnections(clientNum);
  188. const int timeoutTime=1000;
  189. server->SetTimeoutTime(timeoutTime,UNASSIGNED_SYSTEM_ADDRESS);
  190. int retTimeout=(int)server->GetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS);
  191. if (retTimeout!=timeoutTime)
  192. {
  193. if (isVerbose)
  194. DebugTools::ShowError("GetTimeoutTime did not match the timeout that was set. \n",!noPauses && isVerbose,__LINE__,__FILE__);
  195. return 3;
  196. }
  197. //Connect all the clients to the server
  198. for (int i=0;i<clientNum;i++)
  199. {
  200. if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  201. {
  202. if (isVerbose)
  203. DebugTools::ShowError("Problem while calling connect.\n",!noPauses && isVerbose,__LINE__,__FILE__);
  204. return 1;//This fails the test, don't bother going on.
  205. }
  206. }
  207. TimeMS entryTime=GetTimeMS();//Loop entry time
  208. DataStructures::List< SystemAddress > systemList;
  209. DataStructures::List< RakNetGUID > guidList;
  210. if (isVerbose)
  211. printf("Entering disconnect loop \n");
  212. while(GetTimeMS()-entryTime<30000)//Run for 30 Secoonds
  213. {
  214. //Deallocate client IF connected
  215. for (int i=0;i<clientNum;i++)
  216. {
  217. clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  218. int len=systemList.Size();
  219. if(len>=1)
  220. {
  221. RakPeerInterface::DestroyInstance(clientList[i]);
  222. clientList[i]=RakPeerInterface::GetInstance();
  223. clientList[i]->Startup(1,&SocketDescriptor(), 1);
  224. }
  225. }
  226. RakSleep(2000);//Allow connections to timeout.
  227. //Connect
  228. for (int i=0;i<clientNum;i++)
  229. {
  230. currentSystem.SetBinaryAddress("127.0.0.1");
  231. currentSystem.port=60000;
  232. if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  233. {
  234. if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  235. {
  236. if (isVerbose)
  237. DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__);
  238. return 1;//This fails the test, don't bother going on.
  239. }
  240. }
  241. }
  242. WaitAndPrintResults(clientList,clientNum,isVerbose,server);
  243. }
  244. WaitAndPrintResults(clientList,clientNum,isVerbose,server);
  245. printf("Connecting clients\n");
  246. RakSleep(2000);//Allow connections to timeout.
  247. //Connect
  248. for (int i=0;i<clientNum;i++)
  249. {
  250. currentSystem.SetBinaryAddress("127.0.0.1");
  251. currentSystem.port=60000;
  252. if(!CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true,true,true,true) )//Are we connected or is there a pending operation ?
  253. {
  254. printf("Calling Connect() for client %i.\n",i);
  255. if (clientList[i]->Connect("127.0.0.1", 60000, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  256. {
  257. clientList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  258. int len=systemList.Size();
  259. if (isVerbose)
  260. DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__);
  261. return 1;//This fails the test, don't bother going on.
  262. }
  263. }
  264. else
  265. {
  266. if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,false,false,false,true)==false)
  267. printf("Not calling Connect() for client %i because it is disconnecting.\n",i);
  268. else if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,false,true,true)==false)
  269. printf("Not calling Connect() for client %i because it is connecting.\n",i);
  270. else if (CommonFunctions::ConnectionStateMatchesOptions (clientList[i],currentSystem,true)==false)
  271. printf("Not calling Connect() for client %i because it is connected).\n",i);
  272. }
  273. }
  274. WaitAndPrintResults(clientList,clientNum,isVerbose,server);
  275. for (int i=0;i<clientNum;i++)
  276. {
  277. clientList[i]->GetSystemList(systemList,guidList);
  278. int connNum=guidList.Size();//Get the number of connections for the current peer
  279. if (connNum!=1)//Did we connect all?
  280. {
  281. if (isVerbose)
  282. {
  283. printf("Not all clients reconnected normally.\nFailed on client number %i\n",i);
  284. DebugTools::ShowError("",!noPauses && isVerbose,__LINE__,__FILE__);
  285. }
  286. return 2;
  287. }
  288. }
  289. if (isVerbose)
  290. printf("Pass\n");
  291. return 0;
  292. }
  293. RakString ManyClientsOneServerDeallocateBlockingTest::GetTestName()
  294. {
  295. return "ManyClientsOneServerDeallocateBlockingTest";
  296. }
  297. RakString ManyClientsOneServerDeallocateBlockingTest::ErrorCodeToString(int errorCode)
  298. {
  299. switch (errorCode)
  300. {
  301. case 0:
  302. return "No error";
  303. break;
  304. case 1:
  305. return "The connect function failed";
  306. break;
  307. case 2:
  308. return "Peers did not connect normally";
  309. break;
  310. case 3:
  311. return "GetTimeoutTime did not match the timeout that was set";
  312. break;
  313. default:
  314. return "Undefined Error";
  315. }
  316. }
  317. ManyClientsOneServerDeallocateBlockingTest::ManyClientsOneServerDeallocateBlockingTest(void)
  318. {
  319. }
  320. ManyClientsOneServerDeallocateBlockingTest::~ManyClientsOneServerDeallocateBlockingTest(void)
  321. {
  322. }
  323. void ManyClientsOneServerDeallocateBlockingTest::DestroyPeers()
  324. {
  325. for (int i=0; i < clientNum; i++)
  326. RakPeerInterface::DestroyInstance(clientList[i]);
  327. RakPeerInterface::DestroyInstance(server);
  328. }
粤ICP备19079148号