1 /*
2  * timedereg test case for the libxl event system
3  *
4  * To run this test:
5  *    ./test_timedereg
6  * Success:
7  *    program takes a few seconds, prints some debugging output and exits 0
8  * Failure:
9  *    crash
10  *
11  * set up [0]-group timeouts 0 1 2
12  * wait for timeout 1 to occur
13  * deregister 0 and 2.  1 is supposed to be deregistered already
14  * register [1]-group 0 1 2
15  * deregister 1 (should be a no-op)
16  * wait for [1]-group 0 1 2 in turn
17  * on final callback assert that all have been deregistered
18  */
19 
20 #include "libxl_internal.h"
21 
22 #include "libxl_test_timedereg.h"
23 
24 #define NTIMES 3
25 static const int ms[2][NTIMES] = { { 2000,1000,2000 }, { 1000,2000,3000 } };
26 static libxl__ev_time et[2][NTIMES];
27 static libxl__ao *tao;
28 static int seq;
29 
30 static void occurs(libxl__egc *egc, libxl__ev_time *ev,
31                    const struct timeval *requested_abs, int rc);
32 
regs(libxl__ao * ao,int j)33 static void regs(libxl__ao *ao, int j)
34 {
35     AO_GC;
36     int rc, i;
37     LOG(DEBUG,"regs(%d)", j);
38     for (i=0; i<NTIMES; i++) {
39         rc = libxl__ev_time_register_rel(ao, &et[j][i], occurs, ms[j][i]);
40         assert(!rc);
41     }
42 }
43 
libxl_test_timedereg(libxl_ctx * ctx,libxl_asyncop_how * ao_how)44 int libxl_test_timedereg(libxl_ctx *ctx, libxl_asyncop_how *ao_how)
45 {
46     int i;
47     AO_CREATE(ctx, 0, ao_how);
48 
49     tao = ao;
50 
51     for (i=0; i<NTIMES; i++) {
52         libxl__ev_time_init(&et[0][i]);
53         libxl__ev_time_init(&et[1][i]);
54     }
55 
56     regs(ao, 0);
57 
58     return AO_INPROGRESS;
59 }
60 
occurs(libxl__egc * egc,libxl__ev_time * ev,const struct timeval * requested_abs,int rc)61 static void occurs(libxl__egc *egc, libxl__ev_time *ev,
62                    const struct timeval *requested_abs, int rc)
63 {
64     EGC_GC;
65     int i;
66 
67     int off = ev - &et[0][0];
68     LOG(DEBUG,"occurs[%d][%d] seq=%d rc=%d", off/NTIMES, off%NTIMES, seq, rc);
69 
70     assert(rc == ERROR_TIMEDOUT);
71 
72     switch (seq) {
73     case 0:
74         assert(ev == &et[0][1]);
75         libxl__ev_time_deregister(gc, &et[0][0]);
76         libxl__ev_time_deregister(gc, &et[0][2]);
77         regs(tao, 1);
78         libxl__ev_time_deregister(gc, &et[0][1]);
79         break;
80 
81     case 1:
82     case 2:
83         assert(ev == &et[1][seq-1]);
84         break;
85 
86     case 3:
87         assert(ev == &et[1][2]);
88         for (i=0; i<NTIMES; i++) {
89             assert(!libxl__ev_time_isregistered(&et[0][i]));
90             assert(!libxl__ev_time_isregistered(&et[1][i]));
91         }
92         libxl__ao_complete(egc, tao, 0);
93         return;
94 
95     default:
96         abort();
97     }
98 
99     seq++;
100 }
101