-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestRunner.hpp
79 lines (61 loc) · 2.11 KB
/
TestRunner.hpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include <list>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <thread>
template <class Srvr, class Clnt>
class TestRunner {
public:
/**
* Start server, execute code block passed as lambda, stop server.
* @tparam Lambda
* @param lambda
* @param timeout
*/
template<typename Lambda>
static void run(
const Lambda &lambda,
const std::chrono::duration<v_int64, std::micro> &timeout = std::chrono::hours(12)
) {
oatpp::base::Environment::init();
auto startTime = std::chrono::system_clock::now();
bool running = true;
std::mutex timeoutMutex;
std::condition_variable timeoutCondition;
std::shared_ptr<Srvr> srvr = std::make_shared<Srvr>();
std::thread serverThread([srvr] {
srvr->run();
});
std::thread clientThread([srvr, &lambda] {
auto client = std::make_shared<Clnt>();
/* additional scope here */
/* delete response object to free the connection */
/* once connection is closed, connection processing coroutine of the server will exit */
/* once all coroutines done we can properly stop the executor */
{
lambda(client);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Stop server and unblock accepting thread
client->stop();
///////////////////////////////////////////////////////////////////////////////////////////////////////
srvr->stop();
});
std::thread timerThread([&timeout, &startTime, &running, &timeoutMutex, &timeoutCondition]{
auto end = startTime + timeout;
std::unique_lock<std::mutex> lock(timeoutMutex);
while(running) {
timeoutCondition.wait_for(lock, std::chrono::seconds(1));
auto elapsed = std::chrono::system_clock::now() - startTime;
OATPP_ASSERT("ClientServerTestRunner: Error. Timeout." && elapsed < timeout);
}
});
serverThread.join();
clientThread.join();
running = false;
timeoutCondition.notify_one();
timerThread.join();
oatpp::base::Environment::destroy();
}
};