|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2024 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "event.h" |
|
30 |
|
31 #include "../daemon/event.h" |
|
32 |
|
33 typedef struct EVTest { |
|
34 EventHandler *h; |
|
35 pthread_mutex_t mutex; |
|
36 pthread_cond_t cond; |
|
37 void *data1; |
|
38 void *data2; |
|
39 int i1; |
|
40 int i2; |
|
41 } EVTest; |
|
42 |
|
43 UCX_TEST(test_evhandler_create) { |
|
44 EventHandlerConfig cfg1 = { .nthreads = 1}; |
|
45 |
|
46 EventHandlerConfig cfg4 = { .nthreads = 4}; |
|
47 |
|
48 UCX_TEST_BEGIN; |
|
49 |
|
50 EVHandler *ev1 = evhandler_create(&cfg1); |
|
51 UCX_TEST_ASSERT(ev1, "evhandler_create (1) failed"); |
|
52 UCX_TEST_ASSERT(ev1->numins == 1, "ev1 wrong number of instances"); |
|
53 |
|
54 EVHandler *ev2 = evhandler_create(&cfg4); |
|
55 UCX_TEST_ASSERT(ev2, "evhandler_create (2) failed"); |
|
56 UCX_TEST_ASSERT(ev2->numins == 4, "ev2 wrong number of instances"); |
|
57 |
|
58 evhandler_close(ev1); |
|
59 evhandler_close(ev2); |
|
60 |
|
61 UCX_TEST_END; |
|
62 } |
|
63 |
|
64 static int test_event_send_fn(EventHandler *h, Event *event) { |
|
65 EVTest *test = event->cookie; |
|
66 test->i1 = 1; |
|
67 test->i2 = h == test->h; |
|
68 |
|
69 pthread_mutex_lock(&test->mutex); |
|
70 pthread_cond_signal(&test->cond); |
|
71 pthread_mutex_unlock(&test->mutex); |
|
72 |
|
73 return 0; |
|
74 } |
|
75 |
|
76 UCX_TEST(test_event_send) { |
|
77 EventHandlerConfig cfg = { .nthreads = 1}; |
|
78 |
|
79 EVHandler *ev = evhandler_create(&cfg); |
|
80 EventHandler *h = ev_instance(ev); |
|
81 |
|
82 EVTest testdata; |
|
83 ZERO(&testdata, sizeof(EVTest)); |
|
84 testdata.h = h; |
|
85 pthread_mutex_init(&testdata.mutex, NULL); |
|
86 pthread_cond_init(&testdata.cond, NULL); |
|
87 |
|
88 UCX_TEST_BEGIN; |
|
89 |
|
90 Event evt; |
|
91 ZERO(&evt, sizeof(Event)); |
|
92 evt.fn = test_event_send_fn; |
|
93 evt.cookie = &testdata; |
|
94 |
|
95 int ret = event_send(h, &evt); |
|
96 |
|
97 // wait for event finish |
|
98 time_t tstart = time(NULL); |
|
99 while(!testdata.i1) { |
|
100 time_t t = time(NULL); |
|
101 if(t - tstart > 10) { |
|
102 break; |
|
103 } |
|
104 |
|
105 pthread_mutex_lock(&testdata.mutex); |
|
106 pthread_cond_wait(&testdata.cond, &testdata.mutex); |
|
107 pthread_mutex_unlock(&testdata.mutex); |
|
108 } |
|
109 |
|
110 UCX_TEST_ASSERT(!ret, "event_send failed"); |
|
111 UCX_TEST_ASSERT(testdata.i1, "event callback not called"); |
|
112 UCX_TEST_ASSERT(testdata.i2, "event callback received wrong event handler pointer"); |
|
113 |
|
114 UCX_TEST_END; |
|
115 |
|
116 pthread_mutex_destroy(&testdata.mutex); |
|
117 pthread_cond_destroy(&testdata.cond); |
|
118 |
|
119 evhandler_close(ev); |
|
120 } |