7a33b4df058d383984b8af4f211ded631b635b40
[m6w6/libmemcached] / libtest / gearmand.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24 #include <libtest/common.h>
25
26 #include <libtest/gearmand.h>
27
28 #include "util/instance.hpp"
29 #include "util/operation.hpp"
30
31 using namespace datadifferential;
32 using namespace libtest;
33
34 #include <cassert>
35 #include <cerrno>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <iostream>
40 #include <signal.h>
41 #include <sstream>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45
46 #include <libgearman/gearman.h>
47
48 #ifndef __INTEL_COMPILER
49 #pragma GCC diagnostic ignored "-Wold-style-cast"
50 #endif
51
52 using namespace libtest;
53
54 class Gearmand : public libtest::Server
55 {
56 private:
57 public:
58 Gearmand(const std::string& host_arg, in_port_t port_arg) :
59 libtest::Server(host_arg, port_arg, GEARMAND_BINARY, true)
60 {
61 set_pid_file();
62 }
63
64 bool ping()
65 {
66 gearman_client_st *client= gearman_client_create(NULL);
67 if (client == NULL)
68 {
69 Error << "Could not allocate memory for gearman_client_create()";
70 return false;
71 }
72 gearman_client_set_timeout(client, 3000);
73
74 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
75 {
76 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
77
78 if (gearman_success(rc))
79 {
80 gearman_client_free(client);
81 return true;
82 }
83 #if 0
84 Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
85 #endif
86 }
87 else
88 {
89 Error << "gearman_client_add_server() " << gearman_client_error(client);
90 }
91
92 gearman_client_free(client);
93
94 return false;;
95 }
96
97 const char *name()
98 {
99 return "gearmand";
100 };
101
102 void log_file_option(Application& app, const std::string& arg)
103 {
104 if (arg.empty() == false)
105 {
106 std::string buffer("--log-file=");
107 buffer+= arg;
108 app.add_option("--verbose=DEBUG");
109 app.add_option(buffer);
110 }
111 }
112
113 bool has_log_file_option() const
114 {
115 return true;
116 }
117
118 bool is_libtool()
119 {
120 return true;
121 }
122
123 bool has_syslog() const
124 {
125 return false; // --syslog.errmsg-enable
126 }
127
128 bool has_port_option() const
129 {
130 return true;
131 }
132
133 bool build(size_t argc, const char *argv[]);
134 };
135
136 bool Gearmand::build(size_t argc, const char *argv[])
137 {
138 if (getuid() == 0 or geteuid() == 0)
139 {
140 add_option("-u", "root");
141 }
142
143 add_option("--listen=localhost");
144
145 for (size_t x= 0 ; x < argc ; x++)
146 {
147 add_option(argv[x]);
148 }
149
150 return true;
151 }
152
153 namespace libtest {
154
155 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
156 {
157 return new Gearmand(hostname, try_port);
158 }
159
160 }