1
/* Gearman server and library
2
* Copyright (C) 2008 Brian Aker, Eric Day
5
* Use and distribution licensed under the BSD license. See
6
* the COPYING file in the parent directory for full text.
21
#include <libgearman/gearman.h>
23
#include "test_gearmand.h"
25
#define WORKER_TEST_PORT 32123
30
gearman_worker_st worker;
35
test_return_t queue_add(void *object);
36
test_return_t queue_worker(void *object);
38
test_return_t pre(void *object);
39
test_return_t post(void *object);
41
void *world_create(test_return_t *error);
42
test_return_t world_destroy(void *object);
44
/* append test for worker */
45
static void *append_function(gearman_job_st *job __attribute__((unused)),
46
void *context, size_t *result_size,
47
gearman_return_t *ret_ptr __attribute__((unused)))
49
/* this will will set the last char in the context (buffer) to the */
50
/* first char of the work */
51
char * buf = (char *)context;
52
char * work = (char *)gearman_job_workload(job);
59
test_return_t queue_add(void *object)
62
worker_test_st *test= (worker_test_st *)object;
63
gearman_client_st client;
64
gearman_client_st *client_ptr;
65
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
67
uint8_t *value= (uint8_t *)strdup("0");
68
size_t value_length= 1;
70
test->run_worker= false;
72
client_ptr= gearman_client_create(&client);
73
test_truth(client_ptr);
75
rc= gearman_client_add_server(&client, NULL, WORKER_TEST_PORT);
76
test_truth(GEARMAN_SUCCESS == rc);
78
/* send strings "0", "1" ... "9" to alternating between 2 queues */
79
/* queue1 = 1,3,5,7,9 */
80
/* queue2 = 0,2,4,6,8 */
81
for (uint32_t x= 0; x < 10; x++)
83
rc= gearman_client_do_background(&client, x % 2 ? "queue1" : "queue2", NULL,
84
value, value_length, job_handle);
86
test_truth(GEARMAN_SUCCESS == rc);
87
*value = (uint8_t)(*value + 1);
90
gearman_client_free(&client);
93
test->run_worker= true;
97
test_return_t queue_worker(void *object)
99
worker_test_st *test= (worker_test_st *)object;
100
gearman_worker_st *worker= &(test->worker);
102
memset(buffer, 0, sizeof(buffer));
104
if (! test->run_worker)
107
if (gearman_worker_add_function(worker, "queue1", 5, append_function,
108
buffer) != GEARMAN_SUCCESS)
113
if (gearman_worker_add_function(worker, "queue2", 5, append_function,
114
buffer) != GEARMAN_SUCCESS)
119
for (uint32_t x= 0; x < 10; x++)
121
if (gearman_worker_work(worker) != GEARMAN_SUCCESS)
125
// expect buffer to be reassembled in a predictable round robin order
126
if( strcmp(buffer, "1032547698") )
128
fprintf(stderr, "\n\nexpecting 0123456789, got %s\n\n", buffer);
136
void *world_create(test_return_t *error)
138
worker_test_st *test;
139
const char *argv[2]= { "test_gearmand", "--round-robin"};
142
gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, NULL, (char **)argv, 2);
144
test= malloc(sizeof(worker_test_st));
147
*error= TEST_MEMORY_ALLOCATION_FAILURE;
151
memset(test, 0, sizeof(worker_test_st));
152
if (gearman_worker_create(&(test->worker)) == NULL)
154
*error= TEST_FAILURE;
158
if (gearman_worker_add_server(&(test->worker), NULL, WORKER_TEST_PORT) != GEARMAN_SUCCESS)
160
*error= TEST_FAILURE;
164
test->gearmand_pid= gearmand_pid;
166
*error= TEST_SUCCESS;
171
test_return_t world_destroy(void *object)
173
worker_test_st *test= (worker_test_st *)object;
174
gearman_worker_free(&(test->worker));
175
test_gearmand_stop(test->gearmand_pid);
182
{"add", 0, queue_add },
183
{"worker", 0, queue_worker },
187
collection_st collection[] ={
188
{"round_robin", 0, 0, tests},
192
void get_world(world_st *world)
194
world->collections= collection;
195
world->create= world_create;
196
world->destroy= world_destroy;