85307fcf0825b60a056fc41c46ba47d72fb2a77d
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
24 #include <libtest/common.h>
26 #include <libtest/gearmand.h>
28 #include "util/instance.hpp"
29 #include "util/operation.hpp"
31 using namespace datadifferential
;
32 using namespace libtest
;
42 #include <sys/types.h>
46 #include <libgearman/gearman.h>
48 #ifndef __INTEL_COMPILER
49 #pragma GCC diagnostic ignored "-Wold-style-cast"
52 class GetPid
: public util::Instance::Finish
68 bool call(const bool success
, const std::string
&response
)
71 if (success
and response
.size())
73 _pid
= atoi(response
.c_str());
76 if (is_pid_valid(_pid
) == false)
86 using namespace libtest
;
88 class Gearmand
: public libtest::Server
92 Gearmand(const std::string
& host_arg
, in_port_t port_arg
) :
93 libtest::Server(host_arg
, port_arg
)
98 pid_t
get_pid(bool error_is_ok
)
100 if (pid_file().empty() == false)
102 Wait
wait(pid_file(), 0);
104 if (error_is_ok
and not wait
.successful())
106 Error
<< "Pidfile was not found:" << pid_file();
111 GetPid
*get_instance_pid
;
112 util::Instance
instance(hostname(), port());
113 instance
.set_finish(get_instance_pid
= new GetPid
);
115 instance
.push(new util::Operation(test_literal_param("getpid\r\n"), true));
117 if (error_is_ok
and instance
.run() == false)
119 Error
<< "Failed to obtain pid of server";
122 return get_instance_pid
->pid();
127 gearman_client_st
*client
= gearman_client_create(NULL
);
130 Error
<< "Could not allocate memory for gearman_client_create()";
133 gearman_client_set_timeout(client
, 3000);
135 if (gearman_success(gearman_client_add_server(client
, hostname().c_str(), port())))
137 gearman_return_t rc
= gearman_client_echo(client
, test_literal_param("This is my echo test"));
139 if (gearman_success(rc
))
141 gearman_client_free(client
);
145 Error
<< hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc
) << " extended: " << gearman_client_error(client
);
150 Error
<< "gearman_client_add_server() " << gearman_client_error(client
);
153 gearman_client_free(client
);
163 const char *executable()
165 return GEARMAND_BINARY
;
168 const char *daemon_file_option()
173 void log_file_option(Application
& app
, const std::string
& arg
)
175 if (arg
.empty() == false)
177 std::string
buffer("--log-file=");
179 app
.add_option("--verbose=DEBUG");
180 app
.add_option(buffer
);
184 bool has_log_file_option() const
194 bool has_syslog() const
199 bool has_port_option() const
204 bool build(size_t argc
, const char *argv
[]);
207 bool Gearmand::build(size_t argc
, const char *argv
[])
209 std::stringstream arg_buffer
;
211 if (getuid() == 0 or geteuid() == 0)
213 add_option("-u", "root");
216 add_option("--listen=localhost");
218 for (size_t x
= 0 ; x
< argc
; x
++)
228 libtest::Server
*build_gearmand(const char *hostname
, in_port_t try_port
)
230 return new Gearmand(hostname
, try_port
);