testing: fix osx
[m6w6/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
63 bool wait(int flags = 0);
64 bool tryWait();
65 string &drain();
66
67
68 private:
69 string binary;
70 argv_t args;
71 bool sasl = false;
72 pid_t pid = 0;
73 int pipe = -1;
74 int status = 0;
75 unordered_map<int, unsigned> signalled;
76 socket_or_port_t socket_or_port = 11211;
77 string output;
78
79 [[nodiscard]]
80 vector<char *> createArgv();
81 optional<string> handleArg(vector<char *> &arr, const string &arg, const arg_func_t &next_arg);
82 };
83
84 inline ostream &operator << (ostream &out, const socket_or_port_t sop) {
85 if (holds_alternative<string>(sop)) {
86 out << get<string>(sop);
87 } else {
88 out << ":" << get<int>(sop);
89 }
90 return out;
91 }
92
93 inline ostream &operator << (ostream &out, const Server &server) {
94 out << "Server{binary=" << server.getBinary() << ",pid=" << server.getPid() << ",conn=" << server.getSocketOrPort() << "}";
95 return out;
96 }