fix build with clang
[ocaml-event.git] / unittest.ml
blob091a9ad49953e939489f4951a7335070400971ce
1 (***********************************************************************)
2 (* The ocaml-libevent library *)
3 (* *)
4 (* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See *)
5 (* LICENCE for details. *)
6 (***********************************************************************)
8 open OUnit
9 open Libevent
11 (* Tests the creation of new events *)
12 let test_create_event () =
13 let e1 = create () in
14 let e2 = create () in
15 "Events should be different" @? (e1 <> e2)
17 let test_pending () =
18 let base = init () in
19 let e = create () in
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);
24 add e (Some 120.);
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 []);
29 del e;
30 "Not pending on any type" @? (false = pending e all);
31 free base;
32 (** cannot access events after base is freed. TODO add safety test in stubs? *)
35 (* Test eof on a read callback *)
36 let test_read_eof () =
37 let test_string = "This is a test string\n\n\n" in
38 let buflen = 512 in
39 let buf = String.create buflen in
40 let read_count = ref 0 in
41 let evt = create () in
42 let read_cb fd event_type =
43 (* read data from the fd *)
44 let len = Unix.read fd buf 0 buflen in
45 (* when 0 bytes are read this is the EOF, and we are done. *)
46 if len <> 0 then
47 begin
48 read_count := !read_count + len;
49 add evt None
50 end
53 (* Create a socket pair for testing *)
54 let s1, s2 = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
55 let _ = Unix.write s1 test_string 0 (String.length test_string) in
57 (* A shutdown_send will cause an EOF on the reading end *)
58 Unix.shutdown s1 Unix.SHUTDOWN_SEND;
60 (* Setup the event *)
61 Global.set evt s2 [READ] false read_cb;
62 add evt None;
63 Global.dispatch ();
65 (* Now its time to check some things *)
66 assert_equal (String.length test_string) ! read_count
68 (* This is not really a test (yet) *)
69 let call_set () =
70 let do_nothing _ _ =
73 let e1 = create () in
74 Global.set e1 Unix.stderr [WRITE] false do_nothing;
75 Global.set e1 Unix.stdout [WRITE] false do_nothing;
76 Global.set e1 Unix.stdin [READ] false do_nothing;
77 add e1 (Some 0.1);
78 Global.loop ONCE
80 let test_free () =
81 let base = init () in
82 let ev = create () in
83 let called = ref 0 in
84 set_timer base ev ~persist:true (fun () -> incr called);
85 add ev (Some 1.);
86 loop base ONCE;
87 "callback called once as expected" @? (!called = 1);
88 Gc.compact (); (* make sure all events are gone before freeing base *)
89 free base;
90 Gc.compact ();
91 "reached end with callback called once as expected" @? (!called = 1)
93 (* Construct the test suite *)
94 let suite = "event" >:::
95 ["create_event" >:: test_create_event;
96 "test_pending" >:: test_pending;
97 "test_read_eof" >:: test_read_eof;
98 "call_set" >:: call_set;
99 "event_base_free" >:: test_free;
102 (* Run the tests in the test suite *)
103 let _ =
104 run_test_tt_main suite