f526e015f4c0648690dd19c225bc8596029c96b4
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <libtest/common.h>
38 #include <libtest/gearmand.h>
40 #include "util/instance.hpp"
41 #include "util/operation.hpp"
43 using namespace datadifferential
;
44 using namespace libtest
;
53 #include <sys/types.h>
57 #include <libgearman/gearman.h>
59 #ifndef __INTEL_COMPILER
60 #pragma GCC diagnostic ignored "-Wold-style-cast"
63 class GetPid
: public util::Instance::Finish
79 bool call(const bool success
, const std::string
&response
)
83 if (success
and response
.size())
85 _pid
= atoi(response
.c_str());
98 using namespace libtest
;
100 class Gearmand
: public Server
104 Gearmand(const std::string
& host_arg
, in_port_t port_arg
) :
105 Server(host_arg
, port_arg
)
108 pid_t
get_pid(bool error_is_ok
)
110 if (not pid_file().empty())
112 Wait
wait(pid_file(), 0);
114 if (error_is_ok
and not wait
.successful())
116 Error
<< "Pidfile was not found:" << pid_file();
121 GetPid
*get_instance_pid
;
122 util::Instance
instance(hostname(), port());
123 instance
.set_finish(get_instance_pid
= new GetPid
);
125 instance
.push(new util::Operation(test_literal_param("getpid\r\n"), true));
127 if (error_is_ok
and instance
.run() == false)
129 Error
<< "Failed to obtain pid of server";
132 return get_instance_pid
->pid();
137 gearman_client_st
*client
= gearman_client_create(NULL
);
140 Error
<< "Could not allocate memory for gearman_client_create()";
143 gearman_client_set_timeout(client
, 1000);
145 if (gearman_success(gearman_client_add_server(client
, hostname().c_str(), port())))
147 gearman_return_t rc
= gearman_client_echo(client
, test_literal_param("This is my echo test"));
149 if (gearman_success(rc
))
151 gearman_client_free(client
);
156 gearman_client_free(client
);
166 const char *executable()
168 return GEARMAND_BINARY
;
171 const char *pid_file_option()
173 return "--pid-file=";
176 const char *daemon_file_option()
181 const char *log_file_option()
183 return "-vvvvv --log-file=";
186 const char *port_option()
196 bool build(int argc
, const char *argv
[]);
202 bool Gearmand::build(int argc
, const char *argv
[])
204 std::stringstream arg_buffer
;
206 if (getuid() == 0 or geteuid() == 0)
208 arg_buffer
<< " -u root ";
211 for (int x
= 1 ; x
< argc
; x
++)
213 arg_buffer
<< " " << argv
[x
] << " ";
216 set_extra_args(arg_buffer
.str());
223 Server
*build_gearmand(const char *hostname
, in_port_t try_port
)
225 return new Gearmand(hostname
, try_port
);