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
;
41 #include <sys/types.h>
45 #include <libgearman/gearman.h>
47 #ifndef __INTEL_COMPILER
48 #pragma GCC diagnostic ignored "-Wold-style-cast"
51 class GetPid
: public util::Instance::Finish
67 bool call(const bool success
, const std::string
&response
)
70 if (success
and response
.size())
72 _pid
= atoi(response
.c_str());
75 if (is_pid_valid(_pid
) == false)
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
)
97 pid_t
get_pid(bool error_is_ok
)
99 if (not pid_file().empty())
101 Wait
wait(pid_file(), 0);
103 if (error_is_ok
and not wait
.successful())
105 Error
<< "Pidfile was not found:" << pid_file();
110 GetPid
*get_instance_pid
;
111 util::Instance
instance(hostname(), port());
112 instance
.set_finish(get_instance_pid
= new GetPid
);
114 instance
.push(new util::Operation(test_literal_param("getpid\r\n"), true));
116 if (error_is_ok
and instance
.run() == false)
118 Error
<< "Failed to obtain pid of server";
121 return get_instance_pid
->pid();
126 gearman_client_st
*client
= gearman_client_create(NULL
);
129 Error
<< "Could not allocate memory for gearman_client_create()";
132 gearman_client_set_timeout(client
, 1000);
134 if (gearman_success(gearman_client_add_server(client
, hostname().c_str(), port())))
136 gearman_return_t rc
= gearman_client_echo(client
, test_literal_param("This is my echo test"));
138 if (gearman_success(rc
))
140 gearman_client_free(client
);
145 gearman_client_free(client
);
155 const char *executable()
157 return GEARMAND_BINARY
;
160 const char *pid_file_option()
162 return "--pid-file=";
165 const char *daemon_file_option()
170 const char *log_file_option()
172 return "-vvvvv --log-file=";
175 const char *port_option()
185 bool build(int argc
, const char *argv
[]);
188 bool Gearmand::build(int argc
, const char *argv
[])
190 std::stringstream arg_buffer
;
192 if (getuid() == 0 or geteuid() == 0)
194 arg_buffer
<< " -u root ";
197 arg_buffer
<< " --listen=127.0.0.1 ";
199 for (int x
= 1 ; x
< argc
; x
++)
201 arg_buffer
<< " " << argv
[x
] << " ";
204 set_extra_args(arg_buffer
.str());
211 libtest::Server
*build_gearmand(const char *hostname
, in_port_t try_port
)
213 return new Gearmand(hostname
, try_port
);