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

« back to all changes in this revision

Viewing changes to tests/test_worker.c

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-09-28 21:43:31 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20090928214331-9bku0d3v1b1ypgp4
ImportĀ upstreamĀ versionĀ 0.10

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
#include <assert.h>
 
12
#include <signal.h>
 
13
#include <stdlib.h>
 
14
#include <sys/wait.h>
 
15
#include <unistd.h>
 
16
 
 
17
#include "test_worker.h"
 
18
 
 
19
pid_t test_worker_start(in_port_t port, const char *function_name,
 
20
                        gearman_worker_fn *function, const void *function_arg)
 
21
{
 
22
  pid_t worker_pid;
 
23
  gearman_worker_st worker;
 
24
 
 
25
  worker_pid= fork();
 
26
  assert(worker_pid != -1);
 
27
 
 
28
  if (worker_pid == 0)
 
29
  {
 
30
    assert(gearman_worker_create(&worker) != NULL);
 
31
    assert(gearman_worker_add_server(&worker, NULL, port) == GEARMAN_SUCCESS);
 
32
    assert(gearman_worker_add_function(&worker, function_name, 0, function,
 
33
           function_arg) == GEARMAN_SUCCESS);
 
34
    while (1)
 
35
    {
 
36
      gearman_return_t ret= gearman_worker_work(&worker);
 
37
      assert(ret == GEARMAN_SUCCESS);
 
38
    }
 
39
 
 
40
    /* TODO: unreachable - the only way out of the loop above is the assert 
 
41
     * gearman_worker_free(&worker);
 
42
     * exit(0);
 
43
     */
 
44
  }
 
45
  else
 
46
  {
 
47
    /* Wait for the server to start and bind the port. */
 
48
    sleep(1);
 
49
  }
 
50
 
 
51
  return worker_pid;
 
52
}
 
53
 
 
54
void test_worker_stop(pid_t worker_pid)
 
55
{
 
56
  assert(kill(worker_pid, SIGKILL) == 0);
 
57
  assert(waitpid(worker_pid, NULL, 0) == worker_pid);
 
58
}