f37c79a5e734d6a63297864b4ea50e3d987ab6c0
[awesomized/libmemcached] / test / lib / Server.hpp
1 #pragma once
2
3 #include "common.hpp"
4
5 #include <csignal>
6 #include <unordered_map>
7
8 class Server {
9 public:
10
11 friend class Cluster;
12
13 using arg_func_t = function<string(string)>;
14 using arg_t = variant<string, arg_func_t>;
15 using arg_pair_t = pair<arg_t, arg_t>;
16 using argv_t = vector<variant<arg_t, arg_pair_t>>;
17
18 explicit
19 Server(string binary_ = "false", argv_t args_ = {});
20
21 ~Server();
22
23 Server(const Server &s);
24 Server &operator = (const Server &s);
25
26 Server(Server &&s) {
27 *this = move(s);
28 };
29 Server &operator = (Server &&s) {
30 binary = exchange(s.binary, "false");
31 args = exchange(s.args, {});
32 pid = exchange(s.pid, 0);
33 pipe = exchange(s.pipe, -1);
34 status = exchange(s.status, 0);
35 signalled = exchange(s.signalled, {});
36 socket_or_port = exchange(s.socket_or_port, {});
37 output = exchange(s.output, {});
38 return *this;
39 };
40
41 pid_t getPid() const;
42 int getPipe() const;
43 const string &getBinary() const;
44 const argv_t &getArgs() const;
45 const socket_or_port_t &getSocketOrPort() const;
46
47 struct ChildProc {
48 pid_t pid;
49 int pipe;
50 ChildProc(pid_t pid_, int pipe_)
51 : pid{pid_}
52 , pipe{pipe_}
53 {
54 }
55 };
56 optional<ChildProc> start();
57 bool stop();
58
59 bool signal(int signo = SIGTERM);
60 bool check();
61 bool isListening();
62 bool ensureListening();
63
64 bool wait(int flags = 0);
65 bool tryWait();
66 string &drain();
67
68
69 private:
70 string binary;
71 argv_t args;
72 bool sasl = false;
73 pid_t pid = 0;
74 int pipe = -1;
75 int status = 0;
76 unordered_map<int, unsigned> signalled;
77 socket_or_port_t socket_or_port = 11211;
78 string output;
79
80 [[nodiscard]]
81 vector<char *> createArgv();
82 optional<string> handleArg(vector<char *> &arr, const string &arg, const arg_func_t &next_arg);
83 };
84
85 inline ostream &operator << (ostream &out, const socket_or_port_t sop) {
86 if (holds_alternative<string>(sop)) {
87 out << get<string>(sop);
88 } else {
89 out << ":" << get<int>(sop);
90 }
91 return out;
92 }
93
94 inline ostream &operator << (ostream &out, const Server &server) {
95 out << "Server{binary=" << server.getBinary() << ",pid=" << server.getPid() << ",conn=" << server.getSocketOrPort() << "}";
96 return out;
97 }