1 (***********************************************************************)
2 (* The OcamlEvent library *)
4 (* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
5 (* LICENCE for details. *)
6 (***********************************************************************)
11 (* Tests the creation of new events *)
12 let test_create_event () =
15 "Events should be different" @?
(e1 <> e2)
20 let all = [READ
;WRITE
;SIGNAL
;TIMEOUT
] in
21 "Fresh event is not pending" @?
(false = pending
e all);
22 set_timer
base e ~persist
:true (fun () -> assert false);
23 "Still not pending" @?
(false = pending
e all);
25 "Pending on some type" @?
(pending
e all);
26 "Pending on TIMEOUT" @?
(pending
e [TIMEOUT
]);
27 "Not pending on READ" @?
(false = pending
e [READ
]);
28 "Not pending on empty flags" @?
(false = pending
e []);
31 "Not pending on any type" @?
(false = pending
e all);
34 (* Test eof on a read callback *)
35 let test_read_eof () =
36 let test_string = "This is a test string\n\n\n" in
38 let buf = String.create
buflen in
39 let read_count = ref 0 in
40 let evt = create
() in
41 let read_cb fd event_type
=
42 (* read data from the fd *)
43 let len = Unix.read fd
buf 0 buflen in
44 (* when 0 bytes are read this is the EOF, and we are done. *)
47 read_count := !read_count + len;
52 (* Create a socket pair for testing *)
53 let s1, s2
= Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
54 let _ = Unix.write
s1 test_string 0 (String.length
test_string) in
56 (* A shutdown_send will cause an EOF on the reading end *)
57 Unix.shutdown
s1 Unix.SHUTDOWN_SEND
;
60 Global.set
evt s2
[READ
] false read_cb;
64 (* Now its time to check some things *)
65 assert_equal
(String.length
test_string) ! read_count
67 (* This is not really a test (yet) *)
73 Global.set
e1 Unix.stderr
[WRITE
] false do_nothing;
74 Global.set
e1 Unix.stdout
[WRITE
] false do_nothing;
75 Global.set
e1 Unix.stdin
[READ
] false do_nothing;
83 set_timer
base ev ~persist
:true (fun () -> incr
called);
88 "reached end with callback called once as expected" @?
(!called = 1)
90 (* Construct the test suite *)
91 let suite = "event" >:::
92 ["create_event" >:: test_create_event;
93 "test_pending" >:: test_pending;
94 "test_read_eof" >:: test_read_eof;
95 "call_set" >:: call_set;
96 "event_base_free" >:: test_free;
99 (* Run the tests in the test suite *)
101 run_test_tt_main
suite