PingTestsTest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "PingTestsTest.h"
  11. /*
  12. Description:
  13. Tests out:
  14. virtual int GetAveragePing (const SystemAddress systemAddress)=0
  15. virtual int GetLastPing (const SystemAddress systemAddress) const =0
  16. virtual int GetLowestPing (const SystemAddress systemAddress) const =0
  17. virtual void SetOccasionalPing (bool doPing)=0
  18. Ping is tested in CrossConnectionConvertTest,SetOfflinePingResponse and GetOfflinePingResponse tested in OfflineMessagesConvertTest
  19. Success conditions:
  20. Currently is that GetAveragePing and SetOccasionalPing works
  21. Failure conditions:
  22. RakPeerInterface Functions used, tested indirectly by its use, not all encompassing list:
  23. Startup
  24. SetMaximumIncomingConnections
  25. Receive
  26. DeallocatePacket
  27. RakPeerInterface Functions Explicitly Tested:
  28. GetAveragePing
  29. GetLastPing
  30. GetLowestPing
  31. SetOccasionalPing
  32. */
  33. int PingTestsTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  34. {
  35. RakPeerInterface *sender,*sender2, *receiver;
  36. destroyList.Clear(false,_FILE_AND_LINE_);
  37. TestHelpers::StandardClientPrep(sender,destroyList);
  38. TestHelpers::StandardClientPrep(sender2,destroyList);
  39. receiver=RakPeerInterface::GetInstance();
  40. destroyList.Push(receiver,_FILE_AND_LINE_);
  41. receiver->Startup(2, &SocketDescriptor(60000,0), 1);
  42. receiver->SetMaximumIncomingConnections(2);
  43. Packet * packet;
  44. SystemAddress currentSystem;
  45. currentSystem.SetBinaryAddress("127.0.0.1");
  46. currentSystem.port=60000;
  47. printf("Connecting sender2\n");
  48. if (!TestHelpers::WaitAndConnectTwoPeersLocally(sender2,receiver,5000))
  49. {
  50. if (isVerbose)
  51. DebugTools::ShowError("Could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__);
  52. return 2;
  53. }
  54. printf("Getting ping data for lastping and lowestping\n");
  55. sender2->SetOccasionalPing(false);//Test the lowest ping and such without occassionalping,occasional ping comes later
  56. RakTimer timer(1500);
  57. int lastPing=0;
  58. int lowestPing=0;
  59. TimeMS nextPing=0;
  60. while(!timer.IsExpired())
  61. {
  62. for (packet=receiver->Receive();packet;receiver->DeallocatePacket(packet),packet=receiver->Receive())
  63. {
  64. if (isVerbose)
  65. printf("Receive packet id %i\n",packet->data[0]);
  66. }
  67. for (packet=sender2->Receive();packet;sender2->DeallocatePacket(packet),packet=sender2->Receive())
  68. {
  69. if (isVerbose)
  70. printf("Send packet id %i\n",packet->data[0]);
  71. }
  72. if (GetTimeMS()>nextPing)
  73. {
  74. sender2->Ping(currentSystem);
  75. nextPing=GetTimeMS()+30;
  76. }
  77. RakSleep(3);
  78. }
  79. int averagePing=sender2->GetAveragePing(currentSystem);
  80. if (isVerbose)
  81. printf("Average Ping time %i\n",averagePing);
  82. lastPing=sender2->GetLastPing(currentSystem);
  83. lowestPing=sender2->GetLowestPing(currentSystem);
  84. if (isVerbose)
  85. printf("Last Ping time %i\n",lastPing);
  86. if (isVerbose)
  87. printf("Lowest Ping time %i\n",lowestPing);
  88. int returnVal=TestAverageValue(averagePing,__LINE__, noPauses, isVerbose);
  89. if (returnVal!=0)
  90. {
  91. return returnVal;
  92. }
  93. if (lastPing>100)//100 MS for localhost?
  94. {
  95. if (isVerbose)
  96. DebugTools::ShowError("Problem with the last ping time,greater then 100MS for localhost\n",!noPauses && isVerbose,__LINE__,__FILE__);
  97. return 3;
  98. }
  99. if (lowestPing>10)//The lowest ping for localhost should drop below 10MS at least once
  100. {
  101. if (isVerbose)
  102. DebugTools::ShowError("The lowest ping for localhost should drop below 10MS at least once\n",!noPauses && isVerbose,__LINE__,__FILE__);
  103. return 4;
  104. }
  105. if (lastPing<lowestPing)
  106. {
  107. if (isVerbose)
  108. DebugTools::ShowError("There is a problem if the lastping is lower than the lowestping stat\n",!noPauses && isVerbose,__LINE__,__FILE__);
  109. return 5;
  110. }
  111. CommonFunctions::DisconnectAndWait(sender2,"127.0.0.1",60000);//Eliminate variables.
  112. printf("Connecting sender\n");
  113. if (!TestHelpers::WaitAndConnectTwoPeersLocally(sender,receiver,5000))
  114. {
  115. if (isVerbose)
  116. DebugTools::ShowError("Could not connect after 5 seconds\n",!noPauses && isVerbose,__LINE__,__FILE__);
  117. return 2;
  118. }
  119. lastPing=0;
  120. lowestPing=0;
  121. sender->SetOccasionalPing(true);
  122. printf("Testing SetOccasionalPing\n");
  123. timer.Start();
  124. while(!timer.IsExpired())
  125. {
  126. for (packet=receiver->Receive();packet;receiver->DeallocatePacket(packet),packet=receiver->Receive())
  127. {
  128. if (isVerbose)
  129. printf("Receive packet id %i\n",packet->data[0]);
  130. }
  131. for (packet=sender->Receive();packet;sender->DeallocatePacket(packet),packet=sender->Receive())
  132. {
  133. if (isVerbose)
  134. printf("Send packet id %i\n",packet->data[0]);
  135. }
  136. RakSleep(3);
  137. }
  138. averagePing=sender->GetAveragePing(currentSystem);
  139. if (isVerbose)
  140. printf("Average Ping time %i\n",averagePing);
  141. returnVal=TestAverageValue(averagePing,__LINE__, noPauses, isVerbose);
  142. if (returnVal!=0)
  143. {
  144. return returnVal;
  145. }
  146. return 0;
  147. }
  148. int PingTestsTest::TestAverageValue(int averagePing,int line,bool noPauses,bool isVerbose)
  149. {
  150. if (averagePing<0)
  151. {
  152. if (isVerbose)
  153. DebugTools::ShowError("Problem with the average ping time,should never be less than zero in this test\n",!noPauses && isVerbose,line,__FILE__);
  154. return 1;
  155. }
  156. if (averagePing>10)//Average Ping should not be greater than 10MS for localhost. Command line pings typically give < 1ms
  157. {
  158. if (isVerbose)
  159. DebugTools::ShowError("Average Ping should not be greater than 10MS for localhost. Command line pings typically give < 1ms\n",!noPauses && isVerbose,line,__FILE__);
  160. return 5;
  161. }
  162. return 0;
  163. }
  164. RakString PingTestsTest::GetTestName()
  165. {
  166. return "PingTestsTest";
  167. }
  168. RakString PingTestsTest::ErrorCodeToString(int errorCode)
  169. {
  170. switch (errorCode)
  171. {
  172. case 0:
  173. return "No error";
  174. break;
  175. case 1:
  176. return "Problem with the average ping time,should never be less than 0 in this test";
  177. break;
  178. case 2:
  179. return "Could not connect after 5 seconds";
  180. break;
  181. case 3:
  182. return "Problem with the last ping time,greater then 100MS for localhost";
  183. break;
  184. case 4:
  185. return "The lowest ping for localhost should drop below 10MS at least once";
  186. break;
  187. case 5:
  188. return "There is a problem if the lastping is lower than the lowestping stat";
  189. break;
  190. case 6:
  191. return "Average Ping should not be greater than 10MS for localhost. Command line pings typically give < 1ms";
  192. break;
  193. default:
  194. return "Undefined Error";
  195. }
  196. }
  197. PingTestsTest::PingTestsTest(void)
  198. {
  199. }
  200. PingTestsTest::~PingTestsTest(void)
  201. {
  202. }
  203. void PingTestsTest::DestroyPeers()
  204. {
  205. int theSize=destroyList.Size();
  206. for (int i=0; i < theSize; i++)
  207. RakPeerInterface::DestroyInstance(destroyList[i]);
  208. }
粤ICP备19079148号