PeerConnectDisconnectWithCancelPendingTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 "PeerConnectDisconnectWithCancelPendingTest.h"
  11. /*
  12. What is being done here is having 8 peers all connect to eachother, disconnect, connect again.
  13. Do this for about 10 seconds. Then allow them all to connect for one last time.
  14. This test also tests the cancelpendingconnections.
  15. Also tests nonblocking connects, the simpler one PeerConnectDisconnect tests without it
  16. Good ideas for changes:
  17. After the last check run a eightpeers like test an add the conditions
  18. of that test as well.
  19. Make sure that if we initiate the connection we get a proper message
  20. and if not we get a proper message. Add proper conditions.
  21. Randomize sending the disconnect notes
  22. Success conditions:
  23. All connected normally and pending requests get canceled normally.
  24. Failure conditions:
  25. Doesn't reconnect normally.
  26. During the very first connect loop any connect returns false.
  27. Connect function returns false and peer is not connected to anything.
  28. Pending request is not canceled.
  29. */
  30. int PeerConnectDisconnectWithCancelPendingTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  31. {
  32. const int peerNum= 8;
  33. const int maxConnections=peerNum*3;//Max allowed connections for test set to times 3 to eliminate problem variables
  34. RakPeerInterface *peerList[peerNum];//A list of 8 peers
  35. SystemAddress currentSystem;
  36. Packet *packet;
  37. destroyList.Clear(false,_FILE_AND_LINE_);
  38. //Initializations of the arrays
  39. for (int i=0;i<peerNum;i++)
  40. {
  41. peerList[i]=RakPeerInterface::GetInstance();
  42. destroyList.Push(peerList[i],_FILE_AND_LINE_);
  43. peerList[i]->Startup(maxConnections, &SocketDescriptor(60000+i,0), 1);
  44. peerList[i]->SetMaximumIncomingConnections(maxConnections);
  45. }
  46. //Connect all the peers together
  47. for (int i=0;i<peerNum;i++)
  48. {
  49. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  50. {
  51. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  52. {
  53. if (isVerbose)
  54. DebugTools::ShowError("Problem while calling connect.",!noPauses && isVerbose,__LINE__,__FILE__);
  55. return 1;//This fails the test, don't bother going on.
  56. }
  57. }
  58. }
  59. TimeMS entryTime=GetTimeMS();//Loop entry time
  60. DataStructures::List< SystemAddress > systemList;
  61. DataStructures::List< RakNetGUID > guidList;
  62. printf("Entering disconnect loop \n");
  63. bool printedYet;
  64. while(GetTimeMS()-entryTime<10000)//Run for 10 Secoonds
  65. {
  66. //Disconnect all peers IF connected to any
  67. for (int i=0;i<peerNum;i++)
  68. {
  69. peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  70. int len=systemList.Size();
  71. for (int j=0;j<len;j++)//Disconnect them all
  72. {
  73. peerList[i]->CloseConnection (systemList[j],true,0,LOW_PRIORITY);
  74. }
  75. }
  76. RakSleep(100);
  77. //Clear pending if not finished
  78. for (int i=0;i<peerNum;i++)
  79. {
  80. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  81. {
  82. currentSystem.SetBinaryAddress("127.0.0.1");
  83. currentSystem.port=60000+j;
  84. peerList[i]->CancelConnectionAttempt(currentSystem); //Make sure a connection is not pending before trying to connect.
  85. }
  86. }
  87. RakSleep(100);
  88. //Connect
  89. for (int i=0;i<peerNum;i++)
  90. {
  91. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  92. {
  93. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  94. {
  95. currentSystem.SetBinaryAddress("127.0.0.1");
  96. currentSystem.port=60000+j;
  97. peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  98. int len=systemList.Size();
  99. if(CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,false,true,true))//Did we drop the pending connection?
  100. {
  101. if (isVerbose)
  102. DebugTools::ShowError("Did not cancel the pending request \n",!noPauses && isVerbose,__LINE__,__FILE__);
  103. return 3;
  104. }
  105. if (!CommonFunctions::ConnectionStateMatchesOptions (peerList[i],currentSystem,true,true,true,true))
  106. {
  107. if (isVerbose)
  108. DebugTools::ShowError("Problem while calling connect. \n",!noPauses && isVerbose,__LINE__,__FILE__);
  109. return 1;//This fails the test, don't bother going on.
  110. }
  111. }
  112. }
  113. }
  114. for (int i=0;i<peerNum;i++)//Receive for all peers
  115. {
  116. printedYet=false;
  117. packet=peerList[i]->Receive();
  118. while(packet)
  119. {
  120. if (isVerbose&&!printedYet)
  121. {
  122. printf("For peer %i\n",i);
  123. printedYet=true;
  124. }
  125. switch (packet->data[0])
  126. {
  127. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  128. if (isVerbose)
  129. printf("Another client has disconnected.\n");
  130. break;
  131. case ID_REMOTE_CONNECTION_LOST:
  132. if (isVerbose)
  133. printf("Another client has lost the connection.\n");
  134. break;
  135. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  136. if (isVerbose)
  137. printf("Another client has connected.\n");
  138. break;
  139. case ID_CONNECTION_REQUEST_ACCEPTED:
  140. if (isVerbose)
  141. printf("Our connection request has been accepted.\n");
  142. break;
  143. case ID_CONNECTION_ATTEMPT_FAILED:
  144. if (isVerbose)
  145. printf("A connection has failed.\n");
  146. break;
  147. case ID_NEW_INCOMING_CONNECTION:
  148. if (isVerbose)
  149. printf("A connection is incoming.\n");
  150. break;
  151. case ID_NO_FREE_INCOMING_CONNECTIONS:
  152. if (isVerbose)
  153. printf("The server is full.\n");
  154. break;
  155. case ID_ALREADY_CONNECTED:
  156. if (isVerbose)
  157. printf("Already connected\n");
  158. break;
  159. case ID_DISCONNECTION_NOTIFICATION:
  160. if (isVerbose)
  161. printf("We have been disconnected.\n");
  162. break;
  163. case ID_CONNECTION_LOST:
  164. if (isVerbose)
  165. printf("Connection lost.\n");
  166. break;
  167. default:
  168. break;
  169. }
  170. peerList[i]->DeallocatePacket(packet);
  171. // Stay in the loop as long as there are more packets.
  172. packet = peerList[i]->Receive();
  173. }
  174. }
  175. RakSleep(0);//If needed for testing
  176. }
  177. while(GetTimeMS()-entryTime<2000)//Run for 2 Secoonds to process incoming disconnects
  178. {
  179. for (int i=0;i<peerNum;i++)//Receive for all peers
  180. {
  181. printedYet=false;
  182. packet=peerList[i]->Receive();
  183. while(packet)
  184. {
  185. if (isVerbose&&!printedYet)
  186. {
  187. printf("For peer %i\n",i);
  188. printedYet=true;
  189. }
  190. switch (packet->data[0])
  191. {
  192. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  193. if (isVerbose)
  194. printf("Another client has disconnected.\n");
  195. break;
  196. case ID_REMOTE_CONNECTION_LOST:
  197. if (isVerbose)
  198. printf("Another client has lost the connection.\n");
  199. break;
  200. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  201. if (isVerbose)
  202. printf("Another client has connected.\n");
  203. break;
  204. case ID_CONNECTION_REQUEST_ACCEPTED:
  205. if (isVerbose)
  206. printf("Our connection request has been accepted.\n");
  207. break;
  208. case ID_CONNECTION_ATTEMPT_FAILED:
  209. if (isVerbose)
  210. printf("A connection has failed.\n");
  211. break;
  212. case ID_NEW_INCOMING_CONNECTION:
  213. if (isVerbose)
  214. printf("A connection is incoming.\n");
  215. break;
  216. case ID_NO_FREE_INCOMING_CONNECTIONS:
  217. if (isVerbose)
  218. printf("The server is full.\n");
  219. break;
  220. case ID_ALREADY_CONNECTED:
  221. if (isVerbose)
  222. printf("Already connected\n");
  223. break;
  224. case ID_DISCONNECTION_NOTIFICATION:
  225. if (isVerbose)
  226. printf("We have been disconnected.\n");
  227. break;
  228. case ID_CONNECTION_LOST:
  229. if (isVerbose)
  230. printf("Connection lost.\n");
  231. break;
  232. default:
  233. break;
  234. }
  235. peerList[i]->DeallocatePacket(packet);
  236. // Stay in the loop as long as there are more packets.
  237. packet = peerList[i]->Receive();
  238. }
  239. }
  240. RakSleep(0);//If needed for testing
  241. }
  242. //Connect
  243. for (int i=0;i<peerNum;i++)
  244. {
  245. for (int j=i+1;j<peerNum;j++)//Start at i+1 so don't connect two of the same together.
  246. {
  247. currentSystem.SetBinaryAddress("127.0.0.1");
  248. currentSystem.port=60000+j;
  249. peerList[i]->CancelConnectionAttempt(currentSystem); //Make sure a connection is not pending before trying to connect.
  250. if (peerList[i]->Connect("127.0.0.1", 60000+j, 0,0)!=CONNECTION_ATTEMPT_STARTED)
  251. {
  252. peerList[i]->GetSystemList(systemList,guidList);//Get connectionlist
  253. int len=systemList.Size();
  254. if (len==0)//No connections, should not fail.
  255. {
  256. if (isVerbose)
  257. DebugTools::ShowError("Problem while calling connect \n",!noPauses && isVerbose,__LINE__,__FILE__);
  258. return 1;//This fails the test, don't bother going on.
  259. }
  260. }
  261. }
  262. }
  263. entryTime=GetTimeMS();
  264. while(GetTimeMS()-entryTime<5000)//Run for 5 Secoonds
  265. {
  266. for (int i=0;i<peerNum;i++)//Receive for all peers
  267. {
  268. printedYet=false;
  269. packet=peerList[i]->Receive();
  270. while(packet)
  271. {
  272. if (isVerbose&&!printedYet)
  273. {
  274. printf("For peer %i\n",i);
  275. printedYet=true;
  276. }
  277. switch (packet->data[0])
  278. {
  279. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  280. if (isVerbose)
  281. printf("Another client has disconnected.\n");
  282. break;
  283. case ID_REMOTE_CONNECTION_LOST:
  284. if (isVerbose)
  285. printf("Another client has lost the connection.\n");
  286. break;
  287. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  288. if (isVerbose)
  289. printf("Another client has connected.\n");
  290. break;
  291. case ID_CONNECTION_REQUEST_ACCEPTED:
  292. if (isVerbose)
  293. printf("Our connection request has been accepted.\n");
  294. break;
  295. case ID_CONNECTION_ATTEMPT_FAILED:
  296. if (isVerbose)
  297. printf("A connection has failed.\n");
  298. break;
  299. case ID_NEW_INCOMING_CONNECTION:
  300. if (isVerbose)
  301. printf("A connection is incoming.\n");
  302. break;
  303. case ID_NO_FREE_INCOMING_CONNECTIONS:
  304. if (isVerbose)
  305. printf("The server is full.\n");
  306. break;
  307. case ID_ALREADY_CONNECTED:
  308. if (isVerbose)
  309. printf("Already connected\n");
  310. break;
  311. case ID_DISCONNECTION_NOTIFICATION:
  312. if (isVerbose)
  313. printf("We have been disconnected.\n");
  314. break;
  315. case ID_CONNECTION_LOST:
  316. if (isVerbose)
  317. printf("Connection lost.\n");
  318. break;
  319. default:
  320. break;
  321. }
  322. peerList[i]->DeallocatePacket(packet);
  323. // Stay in the loop as long as there are more packets.
  324. packet = peerList[i]->Receive();
  325. }
  326. }
  327. RakSleep(0);//If needed for testing
  328. }
  329. for (int i=0;i<peerNum;i++)
  330. {
  331. peerList[i]->GetSystemList(systemList,guidList);
  332. int connNum=guidList.Size();//Get the number of connections for the current peer
  333. if (connNum!=peerNum-1)//Did we connect to all?
  334. {
  335. if (isVerbose)
  336. DebugTools::ShowError("Not all peers reconnected normally.\n ",!noPauses && isVerbose,__LINE__,__FILE__);
  337. return 2;
  338. }
  339. }
  340. if (isVerbose)
  341. printf("Pass\n");
  342. return 0;
  343. }
  344. RakString PeerConnectDisconnectWithCancelPendingTest::GetTestName()
  345. {
  346. return "PeerConnectDisconnectWithCancelPendingTest";
  347. }
  348. RakString PeerConnectDisconnectWithCancelPendingTest::ErrorCodeToString(int errorCode)
  349. {
  350. switch (errorCode)
  351. {
  352. case 0:
  353. return "No error";
  354. break;
  355. case 1:
  356. return "The connect function failed.";
  357. break;
  358. case 2:
  359. return "Peers did not connect normally.";
  360. break;
  361. case 3:
  362. return "Pending connection was not canceled.";
  363. break;
  364. default:
  365. return "Undefined Error";
  366. }
  367. }
  368. PeerConnectDisconnectWithCancelPendingTest::PeerConnectDisconnectWithCancelPendingTest(void)
  369. {
  370. }
  371. PeerConnectDisconnectWithCancelPendingTest::~PeerConnectDisconnectWithCancelPendingTest(void)
  372. {
  373. }
  374. void PeerConnectDisconnectWithCancelPendingTest::DestroyPeers()
  375. {
  376. int theSize=destroyList.Size();
  377. for (int i=0; i < theSize; i++)
  378. RakPeerInterface::DestroyInstance(destroyList[i]);
  379. }
粤ICP备19079148号