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