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.
10
#include <libtest/test.hpp>
12
using namespace libtest;
21
#include <libgearman/gearman.h>
23
#include <tests/ports.h>
27
gearman_worker_st worker;
36
#ifndef __INTEL_COMPILER
37
#pragma GCC diagnostic ignored "-Wold-style-cast"
40
/* append test for worker */
41
static void *append_function(gearman_job_st *job,
42
void *context, size_t *result_size,
43
gearman_return_t *ret_ptr)
45
/* this will will set the last char in the context (buffer) to the */
46
/* first char of the work */
47
char *buf = (char *)context;
50
char *work = (char *)gearman_job_workload(job);
54
*ret_ptr= GEARMAN_SUCCESS;
59
static 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
test_compare(GEARMAN_SUCCESS,
76
gearman_client_add_server(&client, NULL, ROUND_ROBIN_WORKER_TEST_PORT));
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
static test_return_t queue_worker(void *object)
99
worker_test_st *test= (worker_test_st *)object;
102
gearman_worker_st *worker= &(test->worker);
106
memset(buffer, 0, sizeof(buffer));
108
test_truth(test->run_worker);
110
test_compare_got(GEARMAN_SUCCESS,
111
gearman_worker_add_function(worker, "queue1", 5, append_function, buffer),
112
gearman_worker_error(worker));
114
test_compare_got(GEARMAN_SUCCESS,
115
gearman_worker_add_function(worker, "queue2", 5, append_function, buffer),
116
gearman_worker_error(worker));
118
for (uint32_t x= 0; x < 10; x++)
120
test_compare(GEARMAN_SUCCESS, gearman_worker_work(worker));
123
// expect buffer to be reassembled in a predictable round robin order
124
test_strcmp("1032547698", buffer);
130
static void *world_create(server_startup_st& servers, test_return_t& error)
132
const char *argv[2]= { "test_gearmand", "--round-robin"};
133
if (server_startup(servers, "gearmand", ROUND_ROBIN_WORKER_TEST_PORT, 2, argv) == false)
139
worker_test_st *test= new (std::nothrow) worker_test_st;;
142
error= TEST_MEMORY_ALLOCATION_FAILURE;
146
if (gearman_worker_create(&(test->worker)) == NULL)
152
if (gearman_failed(gearman_worker_add_server(&(test->worker), NULL, ROUND_ROBIN_WORKER_TEST_PORT)))
161
static bool world_destroy(void *object)
163
worker_test_st *test= (worker_test_st *)object;
164
gearman_worker_free(&(test->worker));
171
{"add", 0, queue_add },
172
{"worker", 0, queue_worker },
176
collection_st collection[] ={
177
{"round_robin", 0, 0, tests},
181
void get_world(Framework *world)
183
world->collections= collection;
184
world->_create= world_create;
185
world->_destroy= world_destroy;