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

« back to all changes in this revision

Viewing changes to tests/round_robin.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-08-11 10:06:22 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20090811100622-6ig4iknanc73olum
ImportĀ upstreamĀ versionĀ 0.9

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
 
#include <libtest/test.hpp>
11
 
 
12
 
using namespace libtest;
13
 
 
14
 
#include <cassert>
15
 
#include <cstdio>
16
 
#include <cstdlib>
17
 
#include <cstring>
18
 
#include <memory>
19
 
#include <unistd.h>
20
 
 
21
 
#include <libgearman/gearman.h>
22
 
 
23
 
#include <tests/ports.h>
24
 
 
25
 
struct worker_test_st
26
 
{
27
 
  gearman_worker_st worker;
28
 
  bool run_worker;
29
 
 
30
 
  worker_test_st() :
31
 
    worker(),
32
 
    run_worker(false)
33
 
    { }
34
 
};
35
 
 
36
 
#ifndef __INTEL_COMPILER
37
 
#pragma GCC diagnostic ignored "-Wold-style-cast"
38
 
#endif
39
 
 
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)
44
 
{
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;
48
 
  assert(buf);
49
 
 
50
 
  char *work = (char *)gearman_job_workload(job);
51
 
  buf += strlen(buf);
52
 
  *buf= *work;
53
 
  *result_size= 0;
54
 
  *ret_ptr= GEARMAN_SUCCESS;
55
 
 
56
 
  return NULL;
57
 
}
58
 
 
59
 
static 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
 
  test_compare(GEARMAN_SUCCESS,
76
 
               gearman_client_add_server(&client, NULL, ROUND_ROBIN_WORKER_TEST_PORT));
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
 
static test_return_t queue_worker(void *object)
98
 
{
99
 
  worker_test_st *test= (worker_test_st *)object;
100
 
  test_truth(test);
101
 
 
102
 
  gearman_worker_st *worker= &(test->worker);
103
 
  test_truth(worker);
104
 
 
105
 
  char buffer[11];
106
 
  memset(buffer, 0, sizeof(buffer));
107
 
 
108
 
  test_truth(test->run_worker);
109
 
 
110
 
  test_compare_got(GEARMAN_SUCCESS,
111
 
                   gearman_worker_add_function(worker, "queue1", 5, append_function, buffer),
112
 
                   gearman_worker_error(worker));
113
 
 
114
 
  test_compare_got(GEARMAN_SUCCESS,
115
 
                   gearman_worker_add_function(worker, "queue2", 5, append_function, buffer),
116
 
                   gearman_worker_error(worker));
117
 
 
118
 
  for (uint32_t x= 0; x < 10; x++)
119
 
  {
120
 
    test_compare(GEARMAN_SUCCESS, gearman_worker_work(worker));
121
 
  }
122
 
 
123
 
  // expect buffer to be reassembled in a predictable round robin order
124
 
  test_strcmp("1032547698", buffer);
125
 
 
126
 
  return TEST_SUCCESS;
127
 
}
128
 
 
129
 
 
130
 
static void *world_create(server_startup_st& servers, test_return_t& error)
131
 
{
132
 
  const char *argv[2]= { "test_gearmand", "--round-robin"};
133
 
  if (server_startup(servers, "gearmand", ROUND_ROBIN_WORKER_TEST_PORT, 2, argv) == false)
134
 
  {
135
 
    error= TEST_FAILURE;
136
 
    return NULL;
137
 
  }
138
 
 
139
 
  worker_test_st *test= new (std::nothrow) worker_test_st;;
140
 
  if (not test)
141
 
  {
142
 
    error= TEST_MEMORY_ALLOCATION_FAILURE;
143
 
    return NULL;
144
 
  }
145
 
 
146
 
  if (gearman_worker_create(&(test->worker)) == NULL)
147
 
  {
148
 
    error= TEST_FAILURE;
149
 
    return NULL;
150
 
  }
151
 
 
152
 
  if (gearman_failed(gearman_worker_add_server(&(test->worker), NULL, ROUND_ROBIN_WORKER_TEST_PORT)))
153
 
  {
154
 
    error= TEST_FAILURE;
155
 
    return NULL;
156
 
  }
157
 
 
158
 
  return test;
159
 
}
160
 
 
161
 
static bool world_destroy(void *object)
162
 
{
163
 
  worker_test_st *test= (worker_test_st *)object;
164
 
  gearman_worker_free(&(test->worker));
165
 
  delete test;
166
 
 
167
 
  return TEST_SUCCESS;
168
 
}
169
 
 
170
 
test_st tests[] ={
171
 
  {"add", 0, queue_add },
172
 
  {"worker", 0, queue_worker },
173
 
  {0, 0, 0}
174
 
};
175
 
 
176
 
collection_st collection[] ={
177
 
  {"round_robin", 0, 0, tests},
178
 
  {0, 0, 0, 0}
179
 
};
180
 
 
181
 
void get_world(Framework *world)
182
 
{
183
 
  world->collections= collection;
184
 
  world->_create= world_create;
185
 
  world->_destroy= world_destroy;
186
 
}