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.h"
41 #include "util/operation.h"
43 using namespace gearman_util
;
44 using namespace libtest
;
53 #include <sys/types.h>
57 #include <libtest/server.h>
58 #include <libtest/wait.h>
60 #include <libgearman/gearman.h>
62 #ifndef __INTEL_COMPILER
63 #pragma GCC diagnostic ignored "-Wold-style-cast"
66 class GetPid
: public Instance::Finish
82 bool call(const bool success
, const std::string
&response
)
86 if (success
and response
.size())
88 _pid
= atoi(response
.c_str());
101 using namespace libtest
;
103 class Gearmand
: public Server
107 Gearmand(const std::string
& host_arg
, in_port_t port_arg
) :
108 Server(host_arg
, port_arg
)
111 pid_t
get_pid(bool error_is_ok
)
113 GetPid
*get_instance_pid
;
114 Instance
instance(hostname(), port());
115 instance
.set_finish(get_instance_pid
= new GetPid
);
117 instance
.push(new Operation(test_literal_param("getpid\r\n"), true));
119 if (not instance
.run() and not error_is_ok
)
121 Error
<< "Failed to obtain pid of server";
124 _pid
= get_instance_pid
->pid();
131 gearman_client_st
*client
= gearman_client_create(NULL
);
134 Error
<< "Could not allocate memory for gearman_client_create()";
137 gearman_client_set_timeout(client
, 1000);
139 if (gearman_success(gearman_client_add_server(client
, hostname().c_str(), port())))
141 gearman_return_t rc
= gearman_client_echo(client
, gearman_literal_param("This is my echo test"));
143 if (gearman_success(rc
))
145 gearman_client_free(client
);
150 gearman_client_free(client
);
160 const char *executable()
162 return GEARMAND_BINARY
;
165 const char *pid_file_option()
167 return "--pid-file=";
170 const char *daemon_file_option()
175 const char *log_file_option()
177 return "-vvvvv --log-file=";
180 const char *port_option()
190 bool build(int argc
, const char *argv
[]);
196 bool Gearmand::build(int argc
, const char *argv
[])
198 std::stringstream arg_buffer
;
200 if (getuid() == 0 or geteuid() == 0)
202 arg_buffer
<< " -u root ";
205 for (int x
= 1 ; x
< argc
; x
++)
207 arg_buffer
<< " " << argv
[x
] << " ";
210 set_extra_args(arg_buffer
.str());
217 Server
*build_gearmand(const char *hostname
, in_port_t try_port
)
219 return new Gearmand(hostname
, try_port
);