~clint-fewbar/ubuntu/precise/gearmand/drop-unneeded-patches

« back to all changes in this revision

Viewing changes to tests/round_robin.c

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen, Michael Fladischer, Stig Sandbeck Mathisen
  • Date: 2012-01-23 11:31:08 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120123113108-wl1yhiba13q9jusb
Tags: 0.27-1
[Michael Fladischer]
* Patch: fix spelling
* Patch: remove dependency on googleanalytics
* Patch: fix tests
* Use non-authenticating URL for Vcs-Git.
* Add "status" action to init script.

[Stig Sandbeck Mathisen]
* New upstream release (Closes: #621486) (LP: #682680)
* Remove build dependency on drizzle
  (until it reaches testing again)
* Build with support for tokyocabinet
* Remove backported ipv6 patch
* Patch: disable hostile build tests, they take hours...
* Patch: workaround duplicate address issue for tests
* Do not build API documentation, the sources are not shipped in
  upstream tarball
* Update debian/copyright

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
 
  gearman_return_t rc;
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];
66
 
 
67
 
  uint8_t *value= (uint8_t *)strdup("0");
68
 
  size_t value_length= 1;
69
 
 
70
 
  test->run_worker= false;
71
 
 
72
 
  client_ptr= gearman_client_create(&client);
73
 
  test_truth(client_ptr);
74
 
 
75
 
  rc= gearman_client_add_server(&client, NULL, WORKER_TEST_PORT);
76
 
    test_truth(GEARMAN_SUCCESS == rc);
77
 
 
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++)
82
 
  {
83
 
    rc= gearman_client_do_background(&client, x % 2 ? "queue1" : "queue2", NULL,
84
 
                                     value, value_length, job_handle);
85
 
 
86
 
    test_truth(GEARMAN_SUCCESS == rc);
87
 
    *value = (uint8_t)(*value + 1);
88
 
  }
89
 
 
90
 
  gearman_client_free(&client);
91
 
  free(value);
92
 
 
93
 
  test->run_worker= true;
94
 
  return TEST_SUCCESS;
95
 
}
96
 
 
97
 
test_return_t queue_worker(void *object)
98
 
{
99
 
  worker_test_st *test= (worker_test_st *)object;
100
 
  gearman_worker_st *worker= &(test->worker);
101
 
  char buffer[11];
102
 
  memset(buffer, 0, sizeof(buffer));
103
 
 
104
 
  if (! test->run_worker)
105
 
    return TEST_FAILURE;
106
 
 
107
 
  if (gearman_worker_add_function(worker, "queue1", 5, append_function,
108
 
                                  buffer) != GEARMAN_SUCCESS)
109
 
  {
110
 
    return TEST_FAILURE;
111
 
  }
112
 
 
113
 
  if (gearman_worker_add_function(worker, "queue2", 5, append_function,
114
 
                                  buffer) != GEARMAN_SUCCESS)
115
 
  {
116
 
    return TEST_FAILURE;
117
 
  }
118
 
 
119
 
  for (uint32_t x= 0; x < 10; x++)
120
 
  {
121
 
    if (gearman_worker_work(worker) != GEARMAN_SUCCESS)
122
 
      return TEST_FAILURE;
123
 
  }
124
 
 
125
 
  // expect buffer to be reassembled in a predictable round robin order
126
 
  if( strcmp(buffer, "1032547698") ) 
127
 
  {
128
 
    fprintf(stderr, "\n\nexpecting 0123456789, got %s\n\n", buffer);
129
 
    return TEST_FAILURE;
130
 
  }
131
 
 
132
 
  return TEST_SUCCESS;
133
 
}
134
 
 
135
 
 
136
 
void *world_create(test_return_t *error)
137
 
{
138
 
  worker_test_st *test;
139
 
  const char *argv[2]= { "test_gearmand", "--round-robin"};
140
 
  pid_t gearmand_pid;
141
 
 
142
 
  gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, NULL, (char **)argv, 2);
143
 
 
144
 
  test= malloc(sizeof(worker_test_st));
145
 
  if (! test)
146
 
  {
147
 
    *error= TEST_MEMORY_ALLOCATION_FAILURE;
148
 
    return NULL;
149
 
  }
150
 
 
151
 
  memset(test, 0, sizeof(worker_test_st));
152
 
  if (gearman_worker_create(&(test->worker)) == NULL)
153
 
  {
154
 
    *error= TEST_FAILURE;
155
 
    return NULL;
156
 
  }
157
 
 
158
 
  if (gearman_worker_add_server(&(test->worker), NULL, WORKER_TEST_PORT) != GEARMAN_SUCCESS)
159
 
  {
160
 
    *error= TEST_FAILURE;
161
 
    return NULL;
162
 
  }
163
 
 
164
 
  test->gearmand_pid= gearmand_pid;
165
 
 
166
 
  *error= TEST_SUCCESS;
167
 
 
168
 
  return (void *)test;
169
 
}
170
 
 
171
 
test_return_t world_destroy(void *object)
172
 
{
173
 
  worker_test_st *test= (worker_test_st *)object;
174
 
  gearman_worker_free(&(test->worker));
175
 
  test_gearmand_stop(test->gearmand_pid);
176
 
  free(test);
177
 
 
178
 
  return TEST_SUCCESS;
179
 
}
180
 
 
181
 
test_st tests[] ={
182
 
  {"add", 0, queue_add },
183
 
  {"worker", 0, queue_worker },
184
 
  {0, 0, 0}
185
 
};
186
 
 
187
 
collection_st collection[] ={
188
 
  {"round_robin", 0, 0, tests},
189
 
  {0, 0, 0, 0}
190
 
};
191
 
 
192
 
void get_world(world_st *world)
193
 
{
194
 
  world->collections= collection;
195
 
  world->create= world_create;
196
 
  world->destroy= world_destroy;
197
 
}