1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/* Gearman server and library
* Copyright (C) 2008 Brian Aker, Eric Day
* All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the COPYING file in the parent directory for full text.
*/
#include "config.h"
#if defined(NDEBUG)
# undef NDEBUG
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgearman/gearman.h>
#include "test.h"
#include "test_gearmand.h"
#define WORKER_TEST_PORT 32123
typedef struct
{
pid_t gearmand_pid;
gearman_worker_st worker;
bool run_worker;
} worker_test_st;
/* Prototypes */
test_return_t queue_add(void *object);
test_return_t queue_worker(void *object);
test_return_t pre(void *object);
test_return_t post(void *object);
void *world_create(test_return_t *error);
test_return_t world_destroy(void *object);
/* Counter test for worker */
static void *counter_function(gearman_job_st *job __attribute__((unused)),
void *context, size_t *result_size,
gearman_return_t *ret_ptr __attribute__((unused)))
{
uint32_t *counter= (uint32_t *)context;
*result_size= 0;
*counter= *counter + 1;
return NULL;
}
test_return_t queue_add(void *object)
{
worker_test_st *test= (worker_test_st *)object;
gearman_client_st client;
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
uint8_t *value= (uint8_t *)"background_test";
size_t value_length= strlen("background_test");
test->run_worker= false;
if (gearman_client_create(&client) == NULL)
return TEST_FAILURE;
if (gearman_client_add_server(&client, NULL,
WORKER_TEST_PORT) != GEARMAN_SUCCESS)
{
return TEST_FAILURE;
}
if (gearman_client_do_background(&client, "queue_test", NULL, value,
value_length, job_handle) != GEARMAN_SUCCESS)
{
return TEST_FAILURE;
}
gearman_client_free(&client);
test->run_worker= true;
return TEST_SUCCESS;
}
test_return_t queue_worker(void *object)
{
worker_test_st *test= (worker_test_st *)object;
gearman_worker_st *worker= &(test->worker);
uint32_t counter= 0;
if (!test->run_worker)
return TEST_FAILURE;
if (gearman_worker_add_function(worker, "queue_test", 5, counter_function,
&counter) != GEARMAN_SUCCESS)
{
return TEST_FAILURE;
}
if (gearman_worker_work(worker) != GEARMAN_SUCCESS)
return TEST_FAILURE;
if (counter == 0)
return TEST_FAILURE;
return TEST_SUCCESS;
}
void *world_create(test_return_t *error)
{
worker_test_st *test;
const char *argv[2]= { "test_gearmand", "--libsqlite3-db=tests/gearman.sql"};
pid_t gearmand_pid;
gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, "libsqlite3", (char **)argv, 2);
test= malloc(sizeof(worker_test_st));
if (! test)
{
*error= TEST_MEMORY_ALLOCATION_FAILURE;
return NULL;
}
memset(test, 0, sizeof(worker_test_st));
if (gearman_worker_create(&(test->worker)) == NULL)
{
*error= TEST_FAILURE;
return NULL;
}
if (gearman_worker_add_server(&(test->worker), NULL, WORKER_TEST_PORT) != GEARMAN_SUCCESS)
{
*error= TEST_FAILURE;
return NULL;
}
test->gearmand_pid= gearmand_pid;
*error= TEST_SUCCESS;
return (void *)test;
}
test_return_t world_destroy(void *object)
{
worker_test_st *test= (worker_test_st *)object;
gearman_worker_free(&(test->worker));
test_gearmand_stop(test->gearmand_pid);
free(test);
return TEST_SUCCESS;
}
test_st tests[] ={
{"add", 0, queue_add },
{"worker", 0, queue_worker },
{0, 0, 0}
};
collection_st collection[] ={
#ifdef HAVE_LIBSQLITE3
{"sqlite queue", 0, 0, tests},
#endif
{0, 0, 0, 0}
};
void get_world(world_st *world)
{
world->collections= collection;
world->create= world_create;
world->destroy= world_destroy;
}
|