NetworkTests.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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) {
  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. OutPacket out(50, 0);
  71. out.writeU8(0xF1);
  72. out.writeU16(0xF123);
  73. out.writeU32(0xF1234567);
  74. out.writeS8(-0x71);
  75. out.writeS16(-0x7123);
  76. out.writeS32(-0x71234567);
  77. out.writeS8(0x71);
  78. out.writeS16(0x7123);
  79. out.writeS32(0x71234567);
  80. StringBuffer<20> s("Hi there");
  81. out.writeString(s);
  82. client.send(out);
  83. for(int i = 0; i < 100; i++) {
  84. client.consumeEvents(clientConsumer);
  85. }
  86. test.checkEqual(true, serverConsumer.connected, "server has connection");
  87. test.checkEqual(static_cast<uint8>(0xF1), serverConsumer.data1,
  88. "correct value is sent 1");
  89. test.checkEqual(static_cast<uint16>(0xF123), serverConsumer.data2,
  90. "correct value is sent 2");
  91. test.checkEqual(0xF1234567u, serverConsumer.data3,
  92. "correct value is sent 3");
  93. test.checkEqual(static_cast<int8>(-0x71), serverConsumer.data4,
  94. "correct value is sent 4");
  95. test.checkEqual(static_cast<int16>(-0x7123), serverConsumer.data5,
  96. "correct value is sent 5");
  97. test.checkEqual(-0x71234567, serverConsumer.data6,
  98. "correct value is sent 6");
  99. test.checkEqual(static_cast<int8>(0x71), serverConsumer.data7,
  100. "correct value is sent 7");
  101. test.checkEqual(static_cast<int16>(0x7123), serverConsumer.data8,
  102. "correct value is sent 8");
  103. test.checkEqual(0x71234567, serverConsumer.data9,
  104. "correct value is sent 9");
  105. test.checkEqual(s, serverConsumer.data10, "correct value is sent 10");
  106. client.disconnect();
  107. sleep(100);
  108. test.checkEqual(true, serverConsumer.disconnect, "client has disconnected");
  109. running = false;
  110. listen.join();
  111. }
  112. void NetworkTests::test() {
  113. Test test("Network");
  114. ENet enet;
  115. if(enet.init()) {
  116. test.checkEqual(false, true, "enet init failed");
  117. } else {
  118. testConnect(test);
  119. }
  120. test.finalize();
  121. }