]> git.localhorst.tv Git - blank.git/blob - tst/integration/TestServer.cpp
cb5819de585d2ea51d4801cffd718bcbd74419c8
[blank.git] / tst / integration / TestServer.cpp
1 #include "TestServer.hpp"
2
3 #include <iostream>
4
5
6 namespace blank {
7 namespace test {
8
9 TestServer::TestServer()
10 : dir()
11 , proc(
12         "./blank" BLANK_SUFFIX,
13         { "blank", "--server", "--save-path", dir.Path(), "--cmd-port", "12354" },
14         { })
15 , conn()
16 , serv_buf()
17 , sock_buf() {
18         // wait for command service startup
19         // TODO: timeouts for reading from process
20         WaitOutputLine("listening on TCP port 12354");
21         // connect to command service
22         conn = tcp::Socket("localhost", 12354);
23 }
24
25 TestServer::~TestServer() {
26         proc.Terminate();
27 }
28
29
30 void TestServer::WaitOutputLine(const std::string &expected) {
31         std::string line;
32         while (true) {
33                 if (!serv_buf.Extract(line)) {
34                         // buffer exhausted, fetch more data
35                         serv_buf.Update(proc.ReadOut(serv_buf.WriteHead(), serv_buf.Remain()));
36                         continue;
37                 }
38                 if (line == expected) {
39                         return;
40                 } else {
41                         std::cerr << "ignoring line: " << line << std::endl;
42                 }
43         }
44 }
45
46 void TestServer::WaitCommandMessage(const std::string &line) {
47         WaitCommandLine(" > " + line);
48 }
49
50 void TestServer::WaitCommandError(const std::string &line) {
51         WaitCommandLine(" ! " + line);
52 }
53
54 void TestServer::WaitCommandBroadcast(const std::string &line) {
55         WaitCommandLine(" @ " + line);
56 }
57
58 void TestServer::WaitCommandLine(const std::string &expected) {
59         std::string line;
60         while (true) {
61                 if (!serv_buf.Extract(line)) {
62                         // buffer exhausted, fetch more data
63                         serv_buf.Update(conn.Recv(serv_buf.WriteHead(), serv_buf.Remain()));
64                         continue;
65                 }
66                 if (line == expected) {
67                         return;
68                 }
69         }
70 }
71
72 }
73 }