Fri, 20 Sep 2024 20:04:35 +0200
fix trace log use after free
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2024 Olaf Wintermann. All rights reserved. |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "event.h" |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include "../daemon/event.h" |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | typedef struct EVTest { |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | EventHandler *h; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | pthread_mutex_t mutex; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | pthread_cond_t cond; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | void *data1; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | void *data2; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | int i1; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | int i2; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | } EVTest; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | UCX_TEST(test_evhandler_create) { |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | EventHandlerConfig cfg1 = { .nthreads = 1}; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | EventHandlerConfig cfg4 = { .nthreads = 4}; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | UCX_TEST_BEGIN; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | EVHandler *ev1 = evhandler_create(&cfg1); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | UCX_TEST_ASSERT(ev1, "evhandler_create (1) failed"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | UCX_TEST_ASSERT(ev1->numins == 1, "ev1 wrong number of instances"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | EVHandler *ev2 = evhandler_create(&cfg4); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | UCX_TEST_ASSERT(ev2, "evhandler_create (2) failed"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | UCX_TEST_ASSERT(ev2->numins == 4, "ev2 wrong number of instances"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
58 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
59 | evhandler_shutdown(ev1); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
60 | evhandler_shutdown(ev2); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
61 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
62 | evhandler_wait_and_destroy(ev1); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
63 | evhandler_wait_and_destroy(ev2); |
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | UCX_TEST_END; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | } |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | static int test_event_send_fn(EventHandler *h, Event *event) { |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | EVTest *test = event->cookie; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | test->i1 = 1; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | test->i2 = h == test->h; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | pthread_mutex_lock(&test->mutex); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | pthread_cond_signal(&test->cond); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | pthread_mutex_unlock(&test->mutex); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | return 0; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | } |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
80 | static void testdata_wait_for_completion(EVTest *testdata) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
81 | time_t tstart = time(NULL); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
82 | while(!testdata->i1) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
83 | time_t t = time(NULL); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
84 | if(t - tstart > 10) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
85 | break; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
86 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
87 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
88 | pthread_mutex_lock(&testdata->mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
89 | pthread_cond_wait(&testdata->cond, &testdata->mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
90 | pthread_mutex_unlock(&testdata->mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
91 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
92 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
93 | |
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | UCX_TEST(test_event_send) { |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | EventHandlerConfig cfg = { .nthreads = 1}; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | EVHandler *ev = evhandler_create(&cfg); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | EventHandler *h = ev_instance(ev); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | EVTest testdata; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | ZERO(&testdata, sizeof(EVTest)); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | testdata.h = h; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | pthread_mutex_init(&testdata.mutex, NULL); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | pthread_cond_init(&testdata.cond, NULL); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | UCX_TEST_BEGIN; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
108 | // test sending a single event |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
109 | // the event signals completion in the testdata object |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
110 | // wait up to 10 seconds for completion (it should be instantly) |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
111 | |
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | Event evt; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | ZERO(&evt, sizeof(Event)); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | evt.fn = test_event_send_fn; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | evt.cookie = &testdata; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | int ret = event_send(h, &evt); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | // wait for event finish |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
120 | testdata_wait_for_completion(&testdata); |
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | UCX_TEST_ASSERT(!ret, "event_send failed"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | UCX_TEST_ASSERT(testdata.i1, "event callback not called"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | UCX_TEST_ASSERT(testdata.i2, "event callback received wrong event handler pointer"); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | UCX_TEST_END; |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | pthread_mutex_destroy(&testdata.mutex); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | pthread_cond_destroy(&testdata.cond); |
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
131 | evhandler_shutdown(ev); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
132 | evhandler_wait_and_destroy(ev); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
133 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
134 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
135 | #define EV_TEST_NUM_EVENTS 2000 |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
136 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
137 | static int test_event_send_multi_fn_count(EventHandler *h, Event *event) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
138 | EVTest *test = event->cookie; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
139 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
140 | test->i2++; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
141 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
142 | return 0; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
143 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
144 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
145 | static int test_event_send_multi_fn_end(EventHandler *h, Event *event) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
146 | EVTest *test = event->cookie; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
147 | test->i1 = 1; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
148 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
149 | pthread_mutex_lock(&test->mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
150 | pthread_cond_signal(&test->cond); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
151 | pthread_mutex_unlock(&test->mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
152 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
153 | return 0; |
551
97039494764b
add first eventhandler tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | } |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
155 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
156 | static int test_event_send_multi_fn1(EventHandler *h, Event *event) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
157 | EVTest *test = event->cookie; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
158 | test->i2 = 0; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
159 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
160 | for(int i=0;i<EV_TEST_NUM_EVENTS;i++) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
161 | Event *newevent = malloc(sizeof(Event)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
162 | ZERO(newevent, sizeof(Event)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
163 | newevent->fn = test_event_send_multi_fn_count; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
164 | newevent->finish = ev_free_event; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
165 | newevent->cookie = test; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
166 | event_send(h, newevent); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
167 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
168 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
169 | Event *finish_event = malloc(sizeof(Event)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
170 | ZERO(finish_event, sizeof(Event)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
171 | finish_event->fn = test_event_send_multi_fn_end; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
172 | finish_event->finish = ev_free_event; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
173 | finish_event->cookie = test; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
174 | event_send(h, finish_event); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
175 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
176 | return 0; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
177 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
178 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
179 | UCX_TEST(test_event_send_multi) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
180 | EventHandlerConfig cfg = { .nthreads = 1}; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
181 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
182 | EVHandler *ev = evhandler_create(&cfg); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
183 | EventHandler *h = ev_instance(ev); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
184 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
185 | EVTest testdata; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
186 | ZERO(&testdata, sizeof(EVTest)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
187 | testdata.h = h; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
188 | pthread_mutex_init(&testdata.mutex, NULL); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
189 | pthread_cond_init(&testdata.cond, NULL); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
190 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
191 | UCX_TEST_BEGIN; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
192 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
193 | // test sending multiple events |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
194 | // the first callback test_event_send_multi_fn1 adds additional |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
195 | // EV_TEST_NUM_EVENTS events to the handler + an additional |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
196 | // finishing event, that notifies completion |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
197 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
198 | Event evt; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
199 | ZERO(&evt, sizeof(Event)); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
200 | evt.fn = test_event_send_multi_fn1; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
201 | evt.cookie = &testdata; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
202 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
203 | int ret = event_send(h, &evt); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
204 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
205 | // wait for event finish |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
206 | testdata_wait_for_completion(&testdata); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
207 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
208 | UCX_TEST_ASSERT(!ret, "event_send failed"); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
209 | UCX_TEST_ASSERT(testdata.i1, "event callback not called"); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
210 | UCX_TEST_ASSERT(testdata.i2 == EV_TEST_NUM_EVENTS, "event callback received wrong event handler pointer"); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
211 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
212 | UCX_TEST_END; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
213 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
214 | pthread_mutex_destroy(&testdata.mutex); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
215 | pthread_cond_destroy(&testdata.cond); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
216 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
217 | evhandler_shutdown(ev); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
218 | evhandler_wait_and_destroy(ev); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
551
diff
changeset
|
219 | } |