Catch up with Gearman's libtest
[m6w6/libmemcached] / example / t / memcached_light.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Test memcat
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
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.
34 *
35 */
36
37
38 /*
39 Test that we are cycling the servers we are creating during testing.
40 */
41
42 #include <config.h>
43
44 #include <libtest/test.hpp>
45 #include <libmemcached/memcached.h>
46
47 #include "tests/libmemcached-1.0/memcached_get.h"
48
49 using namespace libtest;
50
51 #ifndef __INTEL_COMPILER
52 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
53 #endif
54
55 static std::string executable("example/memcached_light");
56
57 static test_return_t help_TEST(void *)
58 {
59 const char *args[]= { "--help", 0 };
60
61 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
62
63 return TEST_SUCCESS;
64 }
65
66 static test_return_t verbose_TEST(void *)
67 {
68 const char *args[]= { "--help", "--verbose", 0 };
69
70 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
71
72 return TEST_SUCCESS;
73 }
74
75 static test_return_t daemon_TEST(void *)
76 {
77 const char *args[]= { "--help", "--daemon", 0 };
78
79 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
80
81 return TEST_SUCCESS;
82 }
83
84 static test_return_t protocol_TEST(void *)
85 {
86 const char *args[]= { "--help", "--protocol", 0 };
87
88 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
89
90 return TEST_SUCCESS;
91 }
92
93 static test_return_t version_TEST(void *)
94 {
95 const char *args[]= { "--help", "--version", 0 };
96
97 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
98
99 return TEST_SUCCESS;
100 }
101
102 static test_return_t port_TEST(void *)
103 {
104 const char *args[]= { "--help", "--port=9090", 0 };
105
106 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
107
108 return TEST_SUCCESS;
109 }
110
111 static test_return_t pid_file_TEST(void *)
112 {
113 const char *args[]= { "--help", "--pid-file=/tmp/foo.pid", 0 };
114
115 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
116
117 return TEST_SUCCESS;
118 }
119
120 static test_return_t log_file_TEST(void *)
121 {
122 const char *args[]= { "--help", "--log-file=/tmp/foo.log", 0 };
123
124 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
125
126 return TEST_SUCCESS;
127 }
128
129 static test_return_t max_connections_file_TEST(void *)
130 {
131 const char *args[]= { "--help", "--max-connections=/tmp/foo.max_connections", 0 };
132
133 test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true));
134
135 return TEST_SUCCESS;
136 }
137
138 typedef test_return_t (*libmemcached_test_callback_fn)(memcached_st *);
139
140 static test_return_t _runner_default(libmemcached_test_callback_fn func, void *object)
141 {
142 if (func)
143 {
144 test_true(object);
145 test_return_t ret;
146 try {
147 ret= func((memcached_st*)object);
148 }
149 catch (std::exception& e)
150 {
151 libtest::Error << e.what();
152 return TEST_FAILURE;
153 }
154
155 return ret;
156 }
157
158 return TEST_SUCCESS;
159 }
160
161 class MemcachedLightRunner : public libtest::Runner {
162 public:
163 test_return_t run(test_callback_fn* func, void *object)
164 {
165 return _runner_default(libmemcached_test_callback_fn(func), object);
166 }
167 };
168
169 test_st cmdline_option_TESTS[] ={
170 {"--help", true, help_TEST },
171 {"--verbose", true, verbose_TEST },
172 {"--daemon", true, daemon_TEST },
173 {"--protocol", true, protocol_TEST },
174 {"--version", true, version_TEST },
175 {"--port", true, port_TEST },
176 {"--pid-file", true, pid_file_TEST },
177 {"--log-file", true, log_file_TEST },
178 {"--max-connections", true, max_connections_file_TEST },
179 {0, 0, 0}
180 };
181
182 /* Clean the server before beginning testing */
183 test_st basic_TESTS[] ={
184 #if 0
185 {"memcached_get()", true, (test_callback_fn*)get_test },
186 {"memcached_get() test 2", false, (test_callback_fn*)get_test2 },
187 {"memcached_get() test 3", false, (test_callback_fn*)get_test3 },
188 {"memcached_get() test 4", false, (test_callback_fn*)get_test4 },
189 {"memcached_get() test 5", false, (test_callback_fn*)get_test5 },
190 #endif
191 {0, 0, 0}
192 };
193
194 collection_st collection[] ={
195 {"command line options", 0, 0, cmdline_option_TESTS },
196 {"basic", 0, 0, basic_TESTS },
197 {0, 0, 0, 0}
198 };
199
200 static void *world_create(server_startup_st& servers, test_return_t& error)
201 {
202 if (HAVE_MEMCACHED_LIGHT_BINARY == 0)
203 {
204 error= TEST_SKIPPED;
205 return NULL;
206 }
207
208 if (server_startup(servers, "memcached-light", libtest::default_port(), 0, NULL) == 0)
209 {
210 error= TEST_FAILURE;
211 }
212
213
214 char buffer[1024];
215 int length= snprintf(buffer, sizeof(buffer), "--server=localhost:%d", int(libtest::default_port()));
216 fatal_assert(length > 0);
217
218 memcached_st *memc= memcached(buffer, length);
219
220 fatal_assert(memc);
221
222 return (void*)memc;
223 }
224
225 static bool world_destroy(void *object)
226 {
227 memcached_st *memc= (memcached_st*)object;
228 memcached_free(memc);
229
230 return TEST_SUCCESS;
231 }
232
233
234 void get_world(Framework *world)
235 {
236 world->create(world_create);
237 world->destroy(world_destroy);
238 world->collections(collection);
239 world->set_runner(new MemcachedLightRunner);
240 }
241