-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_udp.cpp
More file actions
64 lines (60 loc) · 1.54 KB
/
test_udp.cpp
File metadata and controls
64 lines (60 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Catch2/include/catch.hpp"
#define INET_USE_DEFAULT_SIGUSR1_HANDLER true
#include "../inetstream.hpp"
#include <thread>
#include <chrono>
TEST_CASE("test client -> server message") {
std::thread t {[] {
inet::server<inet::protocol::UDP> server {1337};
std::this_thread::sleep_for(std::chrono::milliseconds{3});
auto istr = server.get_inetstream();
istr.recv();
REQUIRE(istr.size() == 4);
int i {};
istr >> i;
REQUIRE(i == 42);
}};
try {
inet::client<inet::protocol::UDP> client {"127.0.0.1", 1337};
auto istr = client.get_inetstream();
istr << 42;
std::this_thread::sleep_for(std::chrono::milliseconds{50});
istr.send();
t.join();
}
catch (const std::exception& e)
{
std::cout << "caught exception: " << e.what() << std::endl;
}
}
// server cannot send to client, this test will loop endlessly
/*
TEST_CASE("server -> client") {
std::thread t {[] {
inet::client<inet::protocol::UDP> client {"127.0.0.1", 1337};
auto istr = client.get_inetstream();
do {
if (istr.select(std::chrono::milliseconds {100}))
istr.recv();
} while (istr.size() != 4);
REQUIRE(istr.size() == 4);
int i {};
istr >> i;
REQUIRE(i == 42);
}};
try {
inet::server<inet::protocol::UDP> server {1337};
auto istr = server.get_inetstream();
std::this_thread::sleep_for(std::chrono::milliseconds{10});
istr << 42;
istr.send();
std::this_thread::sleep_for(std::chrono::milliseconds{50});
istr.clear();
t.join();
}
catch (const std::exception& e)
{
std::cout << "caught exception: " << e.what() << std::endl;
}
}
*/