Merge lp:~clint-fewbar/gearmand/round-robin into lp:gearmand/1.0

Proposed by Clint Byrum on 2010-02-16
Status: Merged
Merged at revision: not available
Proposed branch: lp:~clint-fewbar/gearmand/round-robin
Merge into: lp:gearmand/1.0
Diff against target: 355 lines (+253/-1) (has conflicts)
4 files modified
.bzrignore (+2/-0)
tests/include.am (+26/-1)
tests/round_robin.c (+199/-0)
tests/test_gearmand.c (+26/-0)
Text conflict in tests/include.am
To merge this branch: bzr merge lp:~clint-fewbar/gearmand/round-robin
Reviewer Review Type Date Requested Status
Gearman-developers 2010-02-16 Pending
Review via email: mp+19424@code.launchpad.net
To post a comment you must log in.
Clint Byrum (clint-fewbar) wrote :

Adds tests for round-robin behavior

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2010-01-26 05:53:44 +0000
3+++ .bzrignore 2010-02-16 17:23:15 +0000
4@@ -72,3 +72,5 @@
5 tests/worker_test.res
6 tests/tokyocabinet_test
7 tests/tokyocabinet_test.res
8+tests/round_robin_test
9+tests/round_robin_test.res
10
11=== modified file 'tests/include.am'
12--- tests/include.am 2010-02-10 01:48:52 +0000
13+++ tests/include.am 2010-02-16 17:23:16 +0000
14@@ -55,11 +55,20 @@
15 tests_tokyocabinet_test_LDADD= ${TEST_LDADD}
16
17 noinst_PROGRAMS+= \
18+<<<<<<< TREE
19 tests/client_test \
20 tests/cpp_test \
21 tests/internals_test \
22 tests/regression_test \
23 tests/worker_test
24+=======
25+ tests/client_test \
26+ tests/cpp_test \
27+ tests/internals_test \
28+ tests/regression_test \
29+ tests/round_robin_test \
30+ tests/worker_test
31+>>>>>>> MERGE-SOURCE
32
33 noinst_HEADERS+= \
34 tests/test.h \
35@@ -75,6 +84,9 @@
36 tests_regression_test_SOURCES= tests/regression.c
37 tests_regression_test_LDADD= ${TEST_LDADD} libgearman/libgearmancore.la
38
39+tests_round_robin_test_SOURCES= tests/round_robin.c
40+tests_round_robin_test_LDADD= ${TEST_LDADD} libgearman/libgearmancore.la
41+
42 tests_worker_test_SOURCES= tests/worker_test.c
43 tests_worker_test_LDADD= ${TEST_LDADD}
44
45@@ -85,6 +97,9 @@
46 test-client:
47 @tests/client_test $(ARG1) $(ARG2)
48
49+test-round-robin:
50+ tests/round_robin_test $(ARG1) $(ARG2)
51+
52 test-worker:
53 @tests/worker_test $(ARG1) $(ARG2)
54
55@@ -104,11 +119,14 @@
56 $(TOKYOCABINET_TEST) $(ARG1) $(ARG2)
57 $(TOKYOCABINET_RM)
58
59-check-local: test-client test-worker test-internals test-libmemcached test-sqlite test-tokyocabinet
60+check-local: test-client test-round-robin test-worker test-internals test-libmemcached test-sqlite test-tokyocabinet
61
62 gdb-client: ${noinst_PROGRAMS}
63 $(LIBTOOL) --mode=execute gdb tests/client_test
64
65+gdb-round-robin: ${noinst_PROGRAMS}
66+ $(LIBTOOL) --mode=execute gdb tests/round_robin_test
67+
68 gdb-worker: ${noinst_PROGRAMS}
69 $(LIBTOOL) --mode=execute gdb tests/worker_test
70
71@@ -118,6 +136,9 @@
72 valgrind-client:
73 $(VALGRIND_COMMAND) tests/client_test $(ARG1) $(ARG2)
74
75+valgrind-round-robin:
76+ $(VALGRIND_COMMAND) tests/round_robin_test $(ARG1) $(ARG2)
77+
78 valgrind-worker:
79 $(VALGRIND_COMMAND) tests/worker_test $(ARG1) $(ARG2)
80
81@@ -137,6 +158,7 @@
82 $(VALGRIND_COMMAND) $(TOKYOCABINET_TEST) $(ARG1) $(ARG2)
83 $(TOKYOCABINET_RM)
84
85+<<<<<<< TREE
86 GEARMAN_CLIENT_TEST= bin/gearman
87 GEARMAN_PIDFILE = ${abs_top_builddir}/tests/Xugear.pid
88
89@@ -148,3 +170,6 @@
90
91 valgrind: ${noinst_PROGRAMS} valgrind-client valgrind-worker valgrind-internals valgrind-sqlite valgrind-tokyocabinetn
92
93+=======
94+valgrind: ${noinst_PROGRAMS} valgrind-client valgrind-round-robin valgrind-worker valgrind-internals valgrind-sqlite valgrind-tokyocabinetn
95+>>>>>>> MERGE-SOURCE
96
97=== added file 'tests/round_robin.c'
98--- tests/round_robin.c 1970-01-01 00:00:00 +0000
99+++ tests/round_robin.c 2010-02-16 17:23:16 +0000
100@@ -0,0 +1,199 @@
101+/* Gearman server and library
102+ * Copyright (C) 2008 Brian Aker, Eric Day
103+ * All rights reserved.
104+ *
105+ * Use and distribution licensed under the BSD license. See
106+ * the COPYING file in the parent directory for full text.
107+ */
108+
109+#include "config.h"
110+
111+#if defined(NDEBUG)
112+# undef NDEBUG
113+#endif
114+
115+#include <assert.h>
116+#include <stdio.h>
117+#include <stdlib.h>
118+#include <string.h>
119+#include <unistd.h>
120+
121+#include <libgearman/gearman.h>
122+#include "test.h"
123+#include "test_gearmand.h"
124+
125+#define WORKER_TEST_PORT 32123
126+
127+typedef struct
128+{
129+ pid_t gearmand_pid;
130+ gearman_worker_st worker;
131+ bool run_worker;
132+} worker_test_st;
133+
134+/* Prototypes */
135+test_return_t queue_add(void *object);
136+test_return_t queue_worker(void *object);
137+
138+test_return_t pre(void *object);
139+test_return_t post(void *object);
140+
141+void *world_create(test_return_t *error);
142+test_return_t world_destroy(void *object);
143+
144+/* append test for worker */
145+static void *append_function(gearman_job_st *job __attribute__((unused)),
146+ void *context, size_t *result_size,
147+ gearman_return_t *ret_ptr __attribute__((unused)))
148+{
149+ /* this will will set the last char in the context (buffer) to the */
150+ /* first char of the work */
151+ char * buf = (char *)context;
152+ char * work = (char *)gearman_job_workload(job);
153+ buf += strlen(buf);
154+ *buf = *work;
155+ *result_size= 0;
156+ return NULL;
157+}
158+
159+test_return_t queue_add(void *object)
160+{
161+ worker_test_st *test= (worker_test_st *)object;
162+ gearman_client_st client;
163+ char job_handle[GEARMAN_JOB_HANDLE_SIZE];
164+
165+ uint8_t * value= (uint8_t *)strdup("0");
166+ size_t value_length= 1;
167+ uint8_t i;
168+
169+ test->run_worker= false;
170+
171+ if (gearman_client_create(&client) == NULL)
172+ return TEST_FAILURE;
173+
174+ if (gearman_client_add_server(&client, NULL,
175+ WORKER_TEST_PORT) != GEARMAN_SUCCESS)
176+ {
177+ return TEST_FAILURE;
178+ }
179+
180+ /* send strings "0", "1" ... "9" to alternating between 2 queues */
181+ /* queue1 = 1,3,5,7,9 */
182+ /* queue2 = 0,2,4,6,8 */
183+ for (i=0; i<10; i++) {
184+ if (gearman_client_do_background(&client, i % 2 ? "queue1" : "queue2", NULL, value,
185+ value_length, job_handle) != GEARMAN_SUCCESS)
186+ {
187+ return TEST_FAILURE;
188+ }
189+ *value = (uint8_t)(*value + 1);
190+ }
191+
192+ gearman_client_free(&client);
193+ free(value);
194+
195+ test->run_worker= true;
196+ return TEST_SUCCESS;
197+}
198+
199+test_return_t queue_worker(void *object)
200+{
201+ worker_test_st *test= (worker_test_st *)object;
202+ gearman_worker_st *worker= &(test->worker);
203+ char buffer[11];
204+ int i;
205+ memset(buffer, 0, 11);
206+
207+ if (!test->run_worker)
208+ return TEST_FAILURE;
209+
210+ if (gearman_worker_add_function(worker, "queue1", 5, append_function,
211+ buffer) != GEARMAN_SUCCESS)
212+ {
213+ return TEST_FAILURE;
214+ }
215+
216+ if (gearman_worker_add_function(worker, "queue2", 5, append_function,
217+ buffer) != GEARMAN_SUCCESS)
218+ {
219+ return TEST_FAILURE;
220+ }
221+
222+ for (i=0; i<10; i++) {
223+ if (gearman_worker_work(worker) != GEARMAN_SUCCESS)
224+ return TEST_FAILURE;
225+ }
226+
227+ // expect buffer to be reassembled in a predictable round robin order
228+ if( strcmp(buffer, "1032547698") )
229+ {
230+ fprintf(stderr, "\n\nexpecting 0123456789, got %s\n\n", buffer);
231+ return TEST_FAILURE;
232+ }
233+
234+ return TEST_SUCCESS;
235+}
236+
237+
238+void *world_create(test_return_t *error)
239+{
240+ worker_test_st *test;
241+ const char *argv[2]= { "test_gearmand", "--round-robin"};
242+ pid_t gearmand_pid;
243+
244+ gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, NULL, (char **)argv, 2);
245+
246+ test= malloc(sizeof(worker_test_st));
247+ if (! test)
248+ {
249+ *error= TEST_MEMORY_ALLOCATION_FAILURE;
250+ return NULL;
251+ }
252+
253+ memset(test, 0, sizeof(worker_test_st));
254+ if (gearman_worker_create(&(test->worker)) == NULL)
255+ {
256+ *error= TEST_FAILURE;
257+ return NULL;
258+ }
259+
260+ if (gearman_worker_add_server(&(test->worker), NULL, WORKER_TEST_PORT) != GEARMAN_SUCCESS)
261+ {
262+ *error= TEST_FAILURE;
263+ return NULL;
264+ }
265+
266+ test->gearmand_pid= gearmand_pid;
267+
268+ *error= TEST_SUCCESS;
269+
270+ return (void *)test;
271+}
272+
273+test_return_t world_destroy(void *object)
274+{
275+ worker_test_st *test= (worker_test_st *)object;
276+ gearman_worker_free(&(test->worker));
277+ test_gearmand_stop(test->gearmand_pid);
278+ free(test);
279+
280+ return TEST_SUCCESS;
281+}
282+
283+test_st tests[] ={
284+ {"add", 0, queue_add },
285+ {"worker", 0, queue_worker },
286+ {0, 0, 0}
287+};
288+
289+collection_st collection[] ={
290+ {"round_robin", 0, 0, tests},
291+ {0, 0, 0, 0}
292+};
293+
294+void get_world(world_st *world)
295+{
296+ world->collections= collection;
297+ world->create= world_create;
298+ world->destroy= world_destroy;
299+}
300
301=== modified file 'tests/test_gearmand.c'
302--- tests/test_gearmand.c 2010-01-28 21:45:14 +0000
303+++ tests/test_gearmand.c 2010-02-16 17:23:16 +0000
304@@ -46,6 +46,10 @@
305 pid_t gearmand_pid;
306 gearmand_st *gearmand;
307 gearman_conf_st conf;
308+ gearman_conf_module_st module;
309+ const char *name;
310+ const char *value;
311+ bool round_robin= false;
312
313 gearmand_pid= fork();
314 assert(gearmand_pid != -1);
315@@ -74,12 +78,32 @@
316 assert(ret == GEARMAN_SUCCESS);
317 #endif
318
319+ (void)gearman_conf_module_create(&conf, &module, NULL);
320+
321+#ifndef MCO
322+#define MCO(__name, __short, __value, __help) \
323+ gearman_conf_module_add_option(&module, __name, __short, __value, __help);
324+#endif
325+
326+ MCO("round-robin", 'R', NULL, "Assign work in round-robin order per worker"
327+ "connection. The default is to assign work in the order of functions "
328+ "added by the worker.")
329+
330 ret= gearman_conf_parse_args(&conf, argc, argv);
331 assert(ret == GEARMAN_SUCCESS);
332
333+ /* Check for option values that were given. */
334+ while (gearman_conf_module_value(&module, &name, &value))
335+ {
336+ if (!strcmp(name, "round-robin"))
337+ round_robin++;
338+ }
339+
340 gearmand= gearmand_create(NULL, port);
341 assert(gearmand != NULL);
342
343+ gearmand_set_round_robin(gearmand, round_robin);
344+
345 if (queue_type != NULL)
346 {
347 assert(argc);
348@@ -121,6 +145,8 @@
349 }
350 }
351
352+
353+
354 ret= gearmand_run(gearmand);
355 assert(ret != GEARMAN_SUCCESS);
356

Subscribers

People subscribed via source and target branches