NetworkTests.cpp 3.6 KB

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