6149b3c6c6470f1b4b049bcefbb97e7148d9cf0d
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
23 #include <libtest/common.h>
25 #include <libtest/gearmand.h>
27 #include "util/instance.hpp"
28 #include "util/operation.hpp"
30 using namespace datadifferential
;
31 using namespace libtest
;
40 #include <sys/types.h>
44 #include <libgearman/gearman.h>
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
50 class GetPid
: public util::Instance::Finish
66 bool call(const bool success
, const std::string
&response
)
70 if (success
and response
.size())
72 _pid
= atoi(response
.c_str());
85 using namespace libtest
;
87 class Gearmand
: public libtest::Server
91 Gearmand(const std::string
& host_arg
, in_port_t port_arg
) :
92 libtest::Server(host_arg
, port_arg
)
95 pid_t
get_pid(bool error_is_ok
)
97 if (not pid_file().empty())
99 Wait
wait(pid_file(), 0);
101 if (error_is_ok
and not wait
.successful())
103 Error
<< "Pidfile was not found:" << pid_file();
108 GetPid
*get_instance_pid
;
109 util::Instance
instance(hostname(), port());
110 instance
.set_finish(get_instance_pid
= new GetPid
);
112 instance
.push(new util::Operation(test_literal_param("getpid\r\n"), true));
114 if (error_is_ok
and instance
.run() == false)
116 Error
<< "Failed to obtain pid of server";
119 return get_instance_pid
->pid();
124 gearman_client_st
*client
= gearman_client_create(NULL
);
127 Error
<< "Could not allocate memory for gearman_client_create()";
130 gearman_client_set_timeout(client
, 1000);
132 if (gearman_success(gearman_client_add_server(client
, hostname().c_str(), port())))
134 gearman_return_t rc
= gearman_client_echo(client
, test_literal_param("This is my echo test"));
136 if (gearman_success(rc
))
138 gearman_client_free(client
);
143 gearman_client_free(client
);
153 const char *executable()
155 return GEARMAND_BINARY
;
158 const char *pid_file_option()
160 return "--pid-file=";
163 const char *daemon_file_option()
168 const char *log_file_option()
170 return "-vvvvv --log-file=";
173 const char *port_option()
183 bool build(int argc
, const char *argv
[]);
189 bool Gearmand::build(int argc
, const char *argv
[])
191 std::stringstream arg_buffer
;
193 if (getuid() == 0 or geteuid() == 0)
195 arg_buffer
<< " -u root ";
198 arg_buffer
<< " --listen=127.0.0.1 ";
200 for (int x
= 1 ; x
< argc
; x
++)
202 arg_buffer
<< " " << argv
[x
] << " ";
205 set_extra_args(arg_buffer
.str());
212 libtest::Server
*build_gearmand(const char *hostname
, in_port_t try_port
)
214 return new Gearmand(hostname
, try_port
);