~boulabiar/geis/geis2-add-dbus-backend

« back to all changes in this revision

Viewing changes to testsuite/libutouch-geis/check_backend_multiplexor.c

  • Committer: Stephen M. Webb
  • Date: 2010-12-06 12:10:40 UTC
  • mfrom: (87.1.25 geis2)
  • Revision ID: stephen.webb@canonical.com-20101206121040-y9dnfcl89kdot63t
Added a back end base and test fixture.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * internal unit tests for the uTouch GEIS v2.0 backend_multiplexor module
 
3
 */
 
4
#include <check.h>
 
5
 
 
6
#include "geis/geis.h"
 
7
#include "libutouch-geis/geis_backend_multiplexor.h"
 
8
#include <stdio.h>
 
9
#include <sys/select.h>
 
10
#include <unistd.h>
 
11
 
 
12
 
 
13
/* fixtures */
 
14
static GeisBackendMultiplexor g_mx;
 
15
 
 
16
/* fixture setup */
 
17
static void
 
18
construct_mx()
 
19
{
 
20
  g_mx = geis_backend_multiplexor_new();
 
21
}
 
22
 
 
23
/* fixture teardown */
 
24
static void
 
25
destroy_mx()
 
26
{
 
27
  geis_backend_multiplexor_delete(g_mx);
 
28
}
 
29
 
 
30
static void
 
31
testcase_event_callback(int fd, GeisBackendMultiplexorEvent event, void *context)
 
32
{
 
33
  char buf[2];
 
34
  (void)read(fd, buf, 1);
 
35
  *(int *)context += 1; 
 
36
}
 
37
 
 
38
 
 
39
/* verify bag construction/destruction */
 
40
START_TEST(construction)
 
41
{
 
42
  construct_mx();
 
43
  fail_unless(g_mx != NULL, "failed to create backend_multiplexor");
 
44
  fail_unless(geis_backend_multiplexor_fd(g_mx) >= 0, "invalid MX fd");
 
45
  fail_unless(geis_backend_multiplexor_max_events_per_pump(g_mx) == GEIS_BE_MX_DEFAULT_EVENTS_PER_PUMP, "unexpected max fd per pump value");
 
46
  destroy_mx();
 
47
}
 
48
END_TEST
 
49
 
 
50
 
 
51
/* verify multiplexor wait */
 
52
START_TEST(mx_wait)
 
53
{
 
54
  int pfd[2];
 
55
  int mx_fd = geis_backend_multiplexor_fd(g_mx);
 
56
  int called = 0;
 
57
  int status = 0;
 
58
  int first_time = 1;
 
59
 
 
60
  (void)pipe(pfd);
 
61
  geis_backend_multiplexor_add_fd(g_mx, pfd[0], testcase_event_callback, &called);
 
62
 
 
63
  while (1)
 
64
  {
 
65
    fd_set fds;
 
66
    FD_ZERO(&fds);
 
67
    FD_SET(mx_fd, &fds);
 
68
 
 
69
    struct timeval tm;
 
70
    tm.tv_sec = 0;
 
71
    tm.tv_usec = 5000;
 
72
 
 
73
    status = select(mx_fd+1, &fds, NULL, NULL, &tm);
 
74
    fail_if(status < 0, "error in select");
 
75
    if (status < 0)
 
76
    {
 
77
      break;
 
78
    }
 
79
    else if (0 == status)
 
80
    {
 
81
      fprintf(stderr, "select timeout\n");
 
82
      fail_unless(first_time, "select timed out before read");
 
83
      if (!first_time)
 
84
      {
 
85
        break;
 
86
      }
 
87
      first_time = 0;
 
88
      (void)write(pfd[1], "1", 1);
 
89
    }
 
90
    else
 
91
    {
 
92
      geis_backend_multiplexor_pump(g_mx);
 
93
      break;
 
94
    }
 
95
  }
 
96
 
 
97
  fail_if(called == 0, "MX event callback not called");
 
98
  fail_if(called >  1, "MX event callback called too many times");
 
99
}
 
100
END_TEST
 
101
 
 
102
START_TEST(mx_config)
 
103
{
 
104
  geis_backend_multiplexor_set_max_events_per_pump(g_mx, 24);
 
105
  fail_unless(geis_backend_multiplexor_max_events_per_pump(g_mx) == 24,
 
106
              "unexpected max fd per pump value");
 
107
}
 
108
END_TEST
 
109
 
 
110
/* boilerplate */
 
111
Suite *
 
112
make_backend_multiplexor_suite()
 
113
{
 
114
  Suite *s = suite_create("utouch-geis2-backend-multiplexor");
 
115
 
 
116
  TCase *create = tcase_create("backend-multiplexor-creation");
 
117
  tcase_add_test(create, construction);
 
118
  suite_add_tcase(s, create);
 
119
 
 
120
  TCase *usage = tcase_create("backend-multiplexor-usage");
 
121
  tcase_add_checked_fixture(usage, construct_mx, destroy_mx);
 
122
  tcase_add_test(usage, mx_wait);
 
123
  suite_add_tcase(s, usage);
 
124
 
 
125
  return s;
 
126
}
 
127