~mm-yuhu/gearmand/server-funcs

« back to all changes in this revision

Viewing changes to tests/round_robin.c

  • Committer: Brian Aker
  • Date: 2010-02-16 18:34:41 UTC
  • mfrom: (319.1.3 gearmand-newtrunk)
  • Revision ID: brian@gaz-20100216183441-b5jtofqgt9o3xyco
Adding tests for round robin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Gearman server and library
 
2
 * Copyright (C) 2008 Brian Aker, Eric Day
 
3
 * All rights reserved.
 
4
 *
 
5
 * Use and distribution licensed under the BSD license.  See
 
6
 * the COPYING file in the parent directory for full text.
 
7
 */
 
8
 
 
9
#include "config.h"
 
10
 
 
11
#if defined(NDEBUG)
 
12
# undef NDEBUG
 
13
#endif
 
14
 
 
15
#include <assert.h>
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <unistd.h>
 
20
 
 
21
#include <libgearman/gearman.h>
 
22
#include "test.h"
 
23
#include "test_gearmand.h"
 
24
 
 
25
#define WORKER_TEST_PORT 32123
 
26
 
 
27
typedef struct
 
28
{
 
29
  pid_t gearmand_pid;
 
30
  gearman_worker_st worker;
 
31
  bool run_worker;
 
32
} worker_test_st;
 
33
 
 
34
/* Prototypes */
 
35
test_return_t queue_add(void *object);
 
36
test_return_t queue_worker(void *object);
 
37
 
 
38
test_return_t pre(void *object);
 
39
test_return_t post(void *object);
 
40
 
 
41
void *world_create(test_return_t *error);
 
42
test_return_t world_destroy(void *object);
 
43
 
 
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)))
 
48
{
 
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);
 
53
  buf += strlen(buf);
 
54
  *buf = *work;
 
55
  *result_size= 0;
 
56
  return NULL;
 
57
}
 
58
 
 
59
test_return_t queue_add(void *object)
 
60
{
 
61
  worker_test_st *test= (worker_test_st *)object;
 
62
  gearman_client_st client;
 
63
  char job_handle[GEARMAN_JOB_HANDLE_SIZE];
 
64
 
 
65
  uint8_t * value= (uint8_t *)strdup("0");
 
66
  size_t value_length= 1;
 
67
  uint8_t i;
 
68
 
 
69
  test->run_worker= false;
 
70
 
 
71
  if (gearman_client_create(&client) == NULL)
 
72
    return TEST_FAILURE;
 
73
 
 
74
  if (gearman_client_add_server(&client, NULL,
 
75
                                WORKER_TEST_PORT) != GEARMAN_SUCCESS)
 
76
  {
 
77
    return TEST_FAILURE;
 
78
  }
 
79
 
 
80
  /* send strings "0", "1" ... "9" to alternating between 2 queues */
 
81
  /* queue1 = 1,3,5,7,9 */
 
82
  /* queue2 = 0,2,4,6,8 */
 
83
  for (i=0; i<10; i++) {
 
84
    if (gearman_client_do_background(&client, i % 2 ? "queue1" : "queue2", NULL, value,
 
85
                                     value_length, job_handle) != GEARMAN_SUCCESS)
 
86
    {
 
87
      return TEST_FAILURE;
 
88
    }
 
89
    *value = (uint8_t)(*value + 1);
 
90
  }
 
91
 
 
92
  gearman_client_free(&client);
 
93
  free(value);
 
94
 
 
95
  test->run_worker= true;
 
96
  return TEST_SUCCESS;
 
97
}
 
98
 
 
99
test_return_t queue_worker(void *object)
 
100
{
 
101
  worker_test_st *test= (worker_test_st *)object;
 
102
  gearman_worker_st *worker= &(test->worker);
 
103
  char buffer[11];
 
104
  int i;
 
105
  memset(buffer, 0, 11);
 
106
 
 
107
  if (!test->run_worker)
 
108
    return TEST_FAILURE;
 
109
 
 
110
  if (gearman_worker_add_function(worker, "queue1", 5, append_function,
 
111
                                  buffer) != GEARMAN_SUCCESS)
 
112
  {
 
113
    return TEST_FAILURE;
 
114
  }
 
115
 
 
116
  if (gearman_worker_add_function(worker, "queue2", 5, append_function,
 
117
                                  buffer) != GEARMAN_SUCCESS)
 
118
  {
 
119
    return TEST_FAILURE;
 
120
  }
 
121
 
 
122
  for (i=0; i<10; i++) {
 
123
    if (gearman_worker_work(worker) != GEARMAN_SUCCESS)
 
124
      return TEST_FAILURE;
 
125
  }
 
126
 
 
127
  // expect buffer to be reassembled in a predictable round robin order
 
128
  if( strcmp(buffer, "1032547698") ) 
 
129
  {
 
130
    fprintf(stderr, "\n\nexpecting 0123456789, got %s\n\n", buffer);
 
131
    return TEST_FAILURE;
 
132
  }
 
133
 
 
134
  return TEST_SUCCESS;
 
135
}
 
136
 
 
137
 
 
138
void *world_create(test_return_t *error)
 
139
{
 
140
  worker_test_st *test;
 
141
  const char *argv[2]= { "test_gearmand", "--round-robin"};
 
142
  pid_t gearmand_pid;
 
143
 
 
144
  gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, NULL, (char **)argv, 2);
 
145
 
 
146
  test= malloc(sizeof(worker_test_st));
 
147
  if (! test)
 
148
  {
 
149
    *error= TEST_MEMORY_ALLOCATION_FAILURE;
 
150
    return NULL;
 
151
  }
 
152
 
 
153
  memset(test, 0, sizeof(worker_test_st));
 
154
  if (gearman_worker_create(&(test->worker)) == NULL)
 
155
  {
 
156
    *error= TEST_FAILURE;
 
157
    return NULL;
 
158
  }
 
159
 
 
160
  if (gearman_worker_add_server(&(test->worker), NULL, WORKER_TEST_PORT) != GEARMAN_SUCCESS)
 
161
  {
 
162
    *error= TEST_FAILURE;
 
163
    return NULL;
 
164
  }
 
165
 
 
166
  test->gearmand_pid= gearmand_pid;
 
167
 
 
168
  *error= TEST_SUCCESS;
 
169
 
 
170
  return (void *)test;
 
171
}
 
172
 
 
173
test_return_t world_destroy(void *object)
 
174
{
 
175
  worker_test_st *test= (worker_test_st *)object;
 
176
  gearman_worker_free(&(test->worker));
 
177
  test_gearmand_stop(test->gearmand_pid);
 
178
  free(test);
 
179
 
 
180
  return TEST_SUCCESS;
 
181
}
 
182
 
 
183
test_st tests[] ={
 
184
  {"add", 0, queue_add },
 
185
  {"worker", 0, queue_worker },
 
186
  {0, 0, 0}
 
187
};
 
188
 
 
189
collection_st collection[] ={
 
190
  {"round_robin", 0, 0, tests},
 
191
  {0, 0, 0, 0}
 
192
};
 
193
 
 
194
void get_world(world_st *world)
 
195
{
 
196
  world->collections= collection;
 
197
  world->create= world_create;
 
198
  world->destroy= world_destroy;
 
199
}