NetworkTests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <atomic>
  2. #include <thread>
  3. #include "network/Client.h"
  4. #include "network/Server.h"
  5. #include "tests/NetworkTests.h"
  6. #include "tests/Test.h"
  7. static void sleep(int millis) {
  8. std::this_thread::sleep_for(std::chrono::milliseconds(millis));
  9. }
  10. struct ServerConsumer {
  11. bool connected = false;
  12. bool disconnect = false;
  13. uint8 data1 = 0;
  14. uint16 data2 = 0;
  15. uint32 data3 = 0;
  16. int8 data4 = 0;
  17. int16 data5 = 0;
  18. int32 data6 = 0;
  19. int8 data7 = 0;
  20. int16 data8 = 0;
  21. int32 data9 = 0;
  22. StringBuffer<20> data10;
  23. void onConnection(Server::Client& client) {
  24. (void)client;
  25. connected = true;
  26. }
  27. void onDisconnect(Server::Client& client) {
  28. (void)client;
  29. disconnect = true;
  30. }
  31. void onPackage(Server::Client& client, InPacket& in) {
  32. (void)client;
  33. in.readU8(data1);
  34. in.readU16(data2);
  35. in.readU32(data3);
  36. in.readS8(data4);
  37. in.readS16(data5);
  38. in.readS32(data6);
  39. in.readS8(data7);
  40. in.readS16(data8);
  41. in.readS32(data9);
  42. in.readString(data10);
  43. }
  44. };
  45. struct ClientConsumer {};
  46. static void testConnect(Test& test, OutPacket out) {
  47. Server server(54321, 5);
  48. if(server.hasError()) {
  49. test.checkEqual(false, true, "server can initialize");
  50. return;
  51. }
  52. Client client;
  53. if(client.hasError()) {
  54. test.checkEqual(false, true, "client can initialize");
  55. return;
  56. }
  57. std::atomic_bool running(true);
  58. ServerConsumer serverConsumer;
  59. std::thread listen([&running, &server, &serverConsumer]() {
  60. while(running) {
  61. server.consumeEvents(serverConsumer);
  62. }
  63. });
  64. test.checkEqual(false, client.connect("127.0.0.1", 54321, 5),
  65. "connection failed");
  66. ClientConsumer clientConsumer;
  67. for(int i = 0; i < 100; i++) {
  68. client.consumeEvents(clientConsumer);
  69. }
  70. out.writeU8(0xF1);
  71. out.writeU16(0xF123);
  72. out.writeU32(0xF1234567);
  73. out.writeS8(-0x71);
  74. out.writeS16(-0x7123);
  75. out.writeS32(-0x71234567);
  76. out.writeS8(0x71);
  77. out.writeS16(0x7123);
  78. out.writeS32(0x71234567);
  79. StringBuffer<20> s("Hi there");
  80. out.writeString(s);
  81. client.send(out);
  82. for(int i = 0; i < 100; i++) {
  83. client.consumeEvents(clientConsumer);
  84. }
  85. test.checkEqual(true, serverConsumer.connected, "server has connection");
  86. test.checkEqual(static_cast<uint8>(0xF1), serverConsumer.data1,
  87. "correct value is sent 1");
  88. test.checkEqual(static_cast<uint16>(0xF123), serverConsumer.data2,
  89. "correct value is sent 2");
  90. test.checkEqual(0xF1234567u, serverConsumer.data3,
  91. "correct value is sent 3");
  92. test.checkEqual(static_cast<int8>(-0x71), serverConsumer.data4,
  93. "correct value is sent 4");
  94. test.checkEqual(static_cast<int16>(-0x7123), serverConsumer.data5,
  95. "correct value is sent 5");
  96. test.checkEqual(-0x71234567, serverConsumer.data6,
  97. "correct value is sent 6");
  98. test.checkEqual(static_cast<int8>(0x71), serverConsumer.data7,
  99. "correct value is sent 7");
  100. test.checkEqual(static_cast<int16>(0x7123), serverConsumer.data8,
  101. "correct value is sent 8");
  102. test.checkEqual(0x71234567, serverConsumer.data9,
  103. "correct value is sent 9");
  104. test.checkEqual(s, serverConsumer.data10, "correct value is sent 10");
  105. client.disconnect();
  106. sleep(100);
  107. test.checkEqual(true, serverConsumer.disconnect, "client has disconnected");
  108. running = false;
  109. listen.join();
  110. }
  111. void NetworkTests::test() {
  112. Test test("Network");
  113. ENet enet;
  114. if(enet.init()) {
  115. test.checkEqual(false, true, "enet init failed");
  116. } else {
  117. testConnect(test, OutPacket::unsequenced(50));
  118. testConnect(test, OutPacket::reliable(50));
  119. testConnect(test, OutPacket::sequenced(50));
  120. }
  121. test.finalize();
  122. }