2 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "event2/event.h"
30 #include "event2/thread.h"
31 #include "event2/buffer.h"
32 #include "event2/buffer_compat.h"
33 #include "event2/bufferevent.h"
40 #include "tinytest_macros.h"
42 #define WIN32_LEAN_AND_MEAN
45 #undef WIN32_LEAN_AND_MEAN
47 #include "iocp-internal.h"
48 #include "evbuffer-internal.h"
49 #include "evthread-internal.h"
51 /* FIXME remove these ones */
52 #include <sys/queue.h>
53 #include "event2/event_struct.h"
54 #include "event-internal.h"
58 static void *count_lock
= NULL
, *count_cond
= NULL
;
64 EVTHREAD_ALLOC_LOCK(count_lock
, 0);
65 EVTHREAD_ALLOC_COND(count_cond
);
67 tt_assert(count_lock
);
68 tt_assert(count_cond
);
77 EVTHREAD_FREE_LOCK(count_lock
, 0);
78 EVTHREAD_FREE_COND(count_cond
);
84 EVLOCK_LOCK(count_lock
, 0);
86 EVTHREAD_COND_BROADCAST(count_cond
);
87 EVLOCK_UNLOCK(count_lock
, 0);
91 count_wait_for(int i
, int ms
)
97 EVLOCK_LOCK(count_lock
, 0);
98 while (ms
> 0 && count
!= i
) {
100 tv
.tv_usec
= ms
* 1000;
101 elapsed
= GetTickCount();
102 EVTHREAD_COND_WAIT_TIMED(count_cond
, count_lock
, &tv
);
103 elapsed
= GetTickCount() - elapsed
;
108 EVLOCK_UNLOCK(count_lock
, 0);
113 struct dummy_overlapped
{
114 struct event_overlapped eo
;
117 uintptr_t keys
[MAX_CALLS
];
118 ev_ssize_t sizes
[MAX_CALLS
];
122 dummy_cb(struct event_overlapped
*o
, uintptr_t key
, ev_ssize_t n
, int ok
)
124 struct dummy_overlapped
*d_o
=
125 EVUTIL_UPCAST(o
, struct dummy_overlapped
, eo
);
127 EVLOCK_LOCK(d_o
->lock
, 0);
128 if (d_o
->call_count
< MAX_CALLS
) {
129 d_o
->keys
[d_o
->call_count
] = key
;
130 d_o
->sizes
[d_o
->call_count
] = n
;
133 EVLOCK_UNLOCK(d_o
->lock
, 0);
139 pair_is_in(struct dummy_overlapped
*o
, uintptr_t key
, ev_ssize_t n
)
143 EVLOCK_LOCK(o
->lock
, 0);
144 for (i
=0; i
< o
->call_count
; ++i
) {
145 if (o
->keys
[i
] == key
&& o
->sizes
[i
] == n
) {
150 EVLOCK_UNLOCK(o
->lock
, 0);
155 test_iocp_port(void *ptr
)
157 struct event_iocp_port
*port
= NULL
;
158 struct dummy_overlapped o1
, o2
;
160 memset(&o1
, 0, sizeof(o1
));
161 memset(&o2
, 0, sizeof(o2
));
164 EVTHREAD_ALLOC_LOCK(o1
.lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
165 EVTHREAD_ALLOC_LOCK(o2
.lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
170 event_overlapped_init(&o1
.eo
, dummy_cb
);
171 event_overlapped_init(&o2
.eo
, dummy_cb
);
173 port
= event_iocp_port_launch(0);
176 tt_assert(!event_iocp_activate_overlapped(port
, &o1
.eo
, 10, 100));
177 tt_assert(!event_iocp_activate_overlapped(port
, &o2
.eo
, 20, 200));
179 tt_assert(!event_iocp_activate_overlapped(port
, &o1
.eo
, 11, 101));
180 tt_assert(!event_iocp_activate_overlapped(port
, &o2
.eo
, 21, 201));
182 tt_assert(!event_iocp_activate_overlapped(port
, &o1
.eo
, 12, 102));
183 tt_assert(!event_iocp_activate_overlapped(port
, &o2
.eo
, 22, 202));
185 tt_assert(!event_iocp_activate_overlapped(port
, &o1
.eo
, 13, 103));
186 tt_assert(!event_iocp_activate_overlapped(port
, &o2
.eo
, 23, 203));
188 tt_int_op(count_wait_for(8, 2000), ==, 0);
190 tt_want(!event_iocp_shutdown(port
, 2000));
192 tt_int_op(o1
.call_count
, ==, 4);
193 tt_int_op(o2
.call_count
, ==, 4);
195 tt_want(pair_is_in(&o1
, 10, 100));
196 tt_want(pair_is_in(&o1
, 11, 101));
197 tt_want(pair_is_in(&o1
, 12, 102));
198 tt_want(pair_is_in(&o1
, 13, 103));
200 tt_want(pair_is_in(&o2
, 20, 200));
201 tt_want(pair_is_in(&o2
, 21, 201));
202 tt_want(pair_is_in(&o2
, 22, 202));
203 tt_want(pair_is_in(&o2
, 23, 203));
206 EVTHREAD_FREE_LOCK(o1
.lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
207 EVTHREAD_FREE_LOCK(o2
.lock
, EVTHREAD_LOCKTYPE_RECURSIVE
);
211 static struct evbuffer
*rbuf
= NULL
, *wbuf
= NULL
;
214 read_complete(struct event_overlapped
*eo
, uintptr_t key
,
215 ev_ssize_t nbytes
, int ok
)
218 evbuffer_commit_read(rbuf
, nbytes
);
225 write_complete(struct event_overlapped
*eo
, uintptr_t key
,
226 ev_ssize_t nbytes
, int ok
)
229 evbuffer_commit_write(wbuf
, nbytes
);
236 test_iocp_evbuffer(void *ptr
)
238 struct event_overlapped rol
, wol
;
239 struct basic_test_data
*data
= ptr
;
240 struct event_iocp_port
*port
= NULL
;
241 struct evbuffer
*buf
=NULL
;
242 struct evbuffer_chain
*chain
;
247 event_overlapped_init(&rol
, read_complete
);
248 event_overlapped_init(&wol
, write_complete
);
250 for (i
= 0; i
< (int)sizeof(junk
); ++i
)
253 rbuf
= evbuffer_overlapped_new(data
->pair
[0]);
254 wbuf
= evbuffer_overlapped_new(data
->pair
[1]);
255 evbuffer_enable_locking(rbuf
, NULL
);
256 evbuffer_enable_locking(wbuf
, NULL
);
258 port
= event_iocp_port_launch(0);
263 tt_assert(!event_iocp_port_associate(port
, data
->pair
[0], 100));
264 tt_assert(!event_iocp_port_associate(port
, data
->pair
[1], 100));
267 evbuffer_add(wbuf
, junk
, sizeof(junk
));
269 buf
= evbuffer_new();
270 tt_assert(buf
!= NULL
);
271 evbuffer_add(rbuf
, junk
, sizeof(junk
));
272 tt_assert(!evbuffer_launch_read(rbuf
, 2048, &rol
));
273 evbuffer_add_buffer(buf
, rbuf
);
274 tt_int_op(evbuffer_get_length(buf
), ==, sizeof(junk
));
275 for (chain
= buf
->first
; chain
; chain
= chain
->next
)
276 tt_int_op(chain
->flags
& EVBUFFER_MEM_PINNED_ANY
, ==, 0);
277 tt_assert(!evbuffer_get_length(rbuf
));
278 tt_assert(!evbuffer_launch_write(wbuf
, 512, &wol
));
280 tt_int_op(count_wait_for(2, 2000), ==, 0);
282 tt_int_op(evbuffer_get_length(rbuf
),==,512);
284 /* FIXME Actually test some stuff here. */
286 tt_want(!event_iocp_shutdown(port
, 2000));
291 if (buf
) evbuffer_free(buf
);
294 static int got_readcb
= 0;
297 async_readcb(struct bufferevent
*bev
, void *arg
)
299 /* Disabling read should cause the loop to quit */
300 bufferevent_disable(bev
, EV_READ
);
305 test_iocp_bufferevent_async(void *ptr
)
307 struct basic_test_data
*data
= ptr
;
308 struct event_iocp_port
*port
= NULL
;
309 struct bufferevent
*bea1
=NULL
, *bea2
=NULL
;
313 event_base_start_iocp(data
->base
, 0);
314 port
= event_base_get_iocp(data
->base
);
317 bea1
= bufferevent_async_new(data
->base
, data
->pair
[0],
318 BEV_OPT_DEFER_CALLBACKS
);
319 bea2
= bufferevent_async_new(data
->base
, data
->pair
[1],
320 BEV_OPT_DEFER_CALLBACKS
);
324 bufferevent_setcb(bea2
, async_readcb
, NULL
, NULL
, NULL
);
325 bufferevent_enable(bea1
, EV_WRITE
);
326 bufferevent_enable(bea2
, EV_READ
);
328 bufferevent_write(bea1
, "Hello world", strlen("Hello world")+1);
330 event_base_dispatch(data
->base
);
332 tt_int_op(got_readcb
, ==, 1);
333 n
= bufferevent_read(bea2
, buf
, sizeof(buf
)-1);
335 tt_str_op(buf
, ==, "Hello world");
338 bufferevent_free(bea1
);
339 bufferevent_free(bea2
);
343 struct testcase_t iocp_testcases
[] = {
344 { "port", test_iocp_port
, TT_FORK
|TT_NEED_THREADS
, &basic_setup
, NULL
},
345 { "evbuffer", test_iocp_evbuffer
,
346 TT_FORK
|TT_NEED_SOCKETPAIR
|TT_NEED_THREADS
,
347 &basic_setup
, NULL
},
348 { "bufferevent_async", test_iocp_bufferevent_async
,
349 TT_FORK
|TT_NEED_SOCKETPAIR
|TT_NEED_THREADS
|TT_NEED_BASE
,
350 &basic_setup
, NULL
},