1 (***********************************************************************)
2 (* The OcamlEvent library *)
4 (* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
5 (* LICENCE for details. *)
6 (***********************************************************************)
8 (* $Id: unittest.ml,v 1.3 2009-11-26 09:10:37 maas Exp $ *)
13 (* Tests the creation of new events *)
14 let test_create_event () =
17 "Events should be different" @?
(e1 <> e2)
22 let all = [READ
;WRITE
;SIGNAL
;TIMEOUT
] in
23 "Fresh event is not pending" @?
(false = pending
e all);
24 set_timer
base e ~persist
:true (fun () -> assert false);
25 "Still not pending" @?
(false = pending
e all);
27 "Pending on some type" @?
(pending
e all);
28 "Pending on TIMEOUT" @?
(pending
e [TIMEOUT
]);
29 "Not pending on READ" @?
(false = pending
e [READ
]);
30 "Not pending on empty flags" @?
(false = pending
e []);
33 "Not pending on any type" @?
(false = pending
e all);
36 (* Test eof on a read callback *)
37 let test_read_eof () =
38 let test_string = "This is a test string\n\n\n" in
40 let buf = String.create
buflen in
41 let read_count = ref 0 in
42 let evt = create
() in
43 let read_cb fd event_type
=
44 (* read data from the fd *)
45 let len = Unix.read fd
buf 0 buflen in
46 (* when 0 bytes are read this is the EOF, and we are done. *)
49 read_count := !read_count + len;
54 (* Create a socket pair for testing *)
55 let s1, s2
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
56 let _ = Unix.write
s1 test_string 0 (String.length
test_string) in
58 (* A shutdown_send will cause an EOF on the reading end *)
59 Unix.shutdown
s1 Unix.SHUTDOWN_SEND
;
62 Global.set
evt s2
[READ
] false read_cb;
66 (* Now its time to check some things *)
67 assert_equal
(String.length
test_string) ! read_count
69 (* This is not really a test (yet) *)
75 Global.set
e1 Unix.stderr
[WRITE
] false do_nothing;
76 Global.set
e1 Unix.stdout
[WRITE
] false do_nothing;
77 Global.set
e1 Unix.stdin
[READ
] false do_nothing;
85 set_timer
base ev ~persist
:true (fun () -> incr
called);
90 "reached end with callback called once as expected" @?
(!called = 1)
92 (* Construct the test suite *)
93 let suite = "event" >:::
94 ["create_event" >:: test_create_event;
95 "test_pending" >:: test_pending;
96 "test_read_eof" >:: test_read_eof;
97 "call_set" >:: call_set;
98 "event_base_free" >:: test_free;
101 (* Run the tests in the test suite *)
103 run_test_tt_main
suite