~brianaker/libmemcached/1164440

929.1.207 by Brian Aker
Merge in libtest updates.
1
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2
 * 
3
 *  libtest
4
 *
5
 *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
6
 *
929.1.208 by Brian Aker
Update license for libtest.
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.
11
 *
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.
16
 *
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
929.1.207 by Brian Aker
Merge in libtest updates.
20
 */
21
929.1.208 by Brian Aker
Update license for libtest.
22
929.2.154 by Brian Aker
Update of libtest
23
#include <config.h>
929.1.207 by Brian Aker
Merge in libtest updates.
24
#include <libtest/common.h>
929.1.226 by Brian Aker
Merge in version/etc libtest
25
929.1.207 by Brian Aker
Merge in libtest updates.
26
#include <libtest/blobslap_worker.h>
27
28
#include <cassert>
29
#include <cerrno>
30
#include <cstdio>
31
#include <cstdlib>
32
#include <cstring>
33
#include <iostream>
34
#include <signal.h>
35
#include <sys/types.h>
36
#include <sys/wait.h>
37
#include <unistd.h>
38
39
#include <libgearman/gearman.h>
40
41
#ifndef __INTEL_COMPILER
42
#pragma GCC diagnostic ignored "-Wold-style-cast"
43
#endif
44
929.1.246 by Brian Aker
Update for OSX Lion build.
45
namespace libtest {
929.1.207 by Brian Aker
Merge in libtest updates.
46
47
class BlobslapWorker : public Server
48
{
49
private:
50
public:
51
  BlobslapWorker(in_port_t port_arg) :
929.2.162 by Brian Aker
Update
52
    Server("localhost", port_arg, "benchmark/blobslap_worker", true)
929.1.228 by Brian Aker
Merge in fixes for SASL.
53
  { 
54
    set_pid_file();
55
  }
929.1.207 by Brian Aker
Merge in libtest updates.
56
57
  pid_t get_pid(bool error_is_ok)
58
  {
929.1.228 by Brian Aker
Merge in fixes for SASL.
59
    if (pid_file().empty())
60
    {
61
      Error << "pid_file was empty";
62
      return -1;
63
    }
64
65
    Wait wait(pid_file(), 0);
66
67
    if (error_is_ok and not wait.successful())
68
    {
69
      Error << "Pidfile was not found:" << pid_file();
70
      return -1;
71
    }
72
73
    std::stringstream error_message;
74
    pid_t ret= get_pid_from_file(pid_file(), error_message);
75
76
    if (error_is_ok and is_pid_valid(ret) == false)
77
    {
78
      Error << error_message.str();
79
    }
80
81
    return ret;
929.1.207 by Brian Aker
Merge in libtest updates.
82
  }
83
84
  bool ping()
85
  {
86
    if (pid_file().empty())
87
    {
88
      Error << "No pid file available";
89
      return false;
90
    }
91
92
    Wait wait(pid_file(), 0);
93
    if (not wait.successful())
94
    {
95
      Error << "Pidfile was not found:" << pid_file();
96
      return false;
97
    }
98
929.1.228 by Brian Aker
Merge in fixes for SASL.
99
    std::stringstream error_message;
100
    pid_t local_pid= get_pid_from_file(pid_file(), error_message);
101
    if (is_pid_valid(local_pid) == false)
929.1.207 by Brian Aker
Merge in libtest updates.
102
    {
929.1.228 by Brian Aker
Merge in fixes for SASL.
103
      Error << error_message.str();
929.1.207 by Brian Aker
Merge in libtest updates.
104
      return false;
105
    }
106
929.1.228 by Brian Aker
Merge in fixes for SASL.
107
    // Use kill to determine is the process exist
929.1.207 by Brian Aker
Merge in libtest updates.
108
    if (::kill(local_pid, 0) == 0)
109
    {
110
      return true;
111
    }
112
113
    return false;
114
  }
115
116
  const char *name()
117
  {
118
    return "blobslap_worker";
119
  };
120
929.2.135 by Brian Aker
Fix valgrind issues.
121
  bool has_port_option() const
929.1.207 by Brian Aker
Merge in libtest updates.
122
  {
929.2.132 by Brian Aker
Additions to testing to better check return values/etc for servers.
123
    return true;
929.1.207 by Brian Aker
Merge in libtest updates.
124
  }
125
929.2.132 by Brian Aker
Additions to testing to better check return values/etc for servers.
126
  bool has_log_file_option() const
929.1.207 by Brian Aker
Merge in libtest updates.
127
  {
929.2.132 by Brian Aker
Additions to testing to better check return values/etc for servers.
128
    return true;
929.1.207 by Brian Aker
Merge in libtest updates.
129
  }
130
131
  bool is_libtool()
132
  {
133
    return true;
134
  }
135
929.2.137 by Brian Aker
Update for logic for server_create.
136
  bool build(size_t argc, const char *argv[]);
929.1.207 by Brian Aker
Merge in libtest updates.
137
};
138
139
140
#include <sstream>
141
929.2.137 by Brian Aker
Update for logic for server_create.
142
bool BlobslapWorker::build(size_t argc, const char *argv[])
929.1.207 by Brian Aker
Merge in libtest updates.
143
{
144
  std::stringstream arg_buffer;
145
929.2.137 by Brian Aker
Update for logic for server_create.
146
  for (size_t x= 0 ; x < argc ; x++)
929.1.207 by Brian Aker
Merge in libtest updates.
147
  {
929.2.132 by Brian Aker
Additions to testing to better check return values/etc for servers.
148
    add_option(argv[x]);
929.1.207 by Brian Aker
Merge in libtest updates.
149
  }
150
151
  return true;
152
}
153
154
Server *build_blobslap_worker(in_port_t try_port)
155
{
156
  return new BlobslapWorker(try_port);
157
}
158
929.1.246 by Brian Aker
Update for OSX Lion build.
159
} // namespace libtest