From c1d83191fe57ad8e276c8022e9af46c7a2ced670 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 11 Mar 2008 17:48:16 -0600 Subject: [PATCH] Fixed epoll_wrappers.c:event_list_of_int to lists with >1 item --- epoll_wrapper.c | 23 +++++++----- tests.ml | 97 +++++++++++++++++++++++++++++++------------------ 2 files changed, 76 insertions(+), 44 deletions(-) diff --git a/epoll_wrapper.c b/epoll_wrapper.c index 0b3f9b0..79693d2 100644 --- a/epoll_wrapper.c +++ b/epoll_wrapper.c @@ -1,6 +1,6 @@ /** OCaml poll() interface * - * Time-stamp: <2008-03-10 23:56:36 neale> + * Time-stamp: <2008-03-11 17:44:00 neale> * * Copyright (C) 2008 Neale Pickett * @@ -129,16 +129,21 @@ event_list_of_int(int events) result = Val_int(0); + /* Do these in reverse order since we're prepending to the list */ + if (events & EPOLLHUP) { + result = cons(Val_int(caml_POLLHUP), result); + } + if (events & EPOLLERR) { + result = cons(Val_int(caml_POLLERR), result); + } + if (events & EPOLLOUT) { + result = cons(Val_int(caml_POLLOUT), result); + } + if (events & EPOLLPRI) { + result = cons(Val_int(caml_POLLPRI), result); + } if (events & EPOLLIN) { result = cons(Val_int(caml_POLLIN), result); - } else if (events & EPOLLPRI) { - result = cons(Val_int(caml_POLLPRI), result); - } else if (events & EPOLLOUT) { - result = cons(Val_int(caml_POLLOUT), result); - } else if (events & EPOLLERR) { - result = cons(Val_int(caml_POLLERR), result); - } else if (events & EPOLLHUP) { - result = cons(Val_int(caml_POLLHUP), result); } CAMLreturn(result); } diff --git a/tests.ml b/tests.ml index 60e6a13..8dc734a 100644 --- a/tests.ml +++ b/tests.ml @@ -42,41 +42,33 @@ let unit_tests = (fun () -> let a,b = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in let e = Epoll.create 1 in + let expect ?(n=3) what = + assert_equal + ~printer:epollfds_as_string + (List.sort compare what) + (List.sort compare (Epoll.wait e n 0)) + in Epoll.ctl e Epoll.Add (a, [Epoll.Out; Epoll.In]); - assert_equal - ~printer:epollfds_as_string - [(a, [Epoll.Out])] - (Epoll.wait e 1 0); + expect [(a, [Epoll.Out])]; Epoll.ctl e Epoll.Modify (a, [Epoll.In; Epoll.Priority]); - assert_equal - ~printer:epollfds_as_string - [] - (Epoll.wait e 1 0); + expect []; Epoll.ctl e Epoll.Add (b, [Epoll.Out; Epoll.In]); - assert_equal - ~printer:epollfds_as_string - [(b, [Epoll.Out])] - (Epoll.wait e 2 0); + expect [(b, [Epoll.Out])]; Epoll.ctl e Epoll.Modify (a, [Epoll.Out; Epoll.In]); - assert_equal - ~printer:epollfds_as_string - [(a, [Epoll.Out]); (b, [Epoll.Out])] - (Epoll.wait e 2 0); + expect [(a, [Epoll.Out]); (b, [Epoll.Out])]; + assert_equal + ~printer:string_of_int + 1 + (List.length (Epoll.wait e 1 0)); - Epoll.ctl e Epoll.Modify (a, [Epoll.Out; Epoll.In]); - assert_equal - ~printer:epollfds_as_string - [(b, [Epoll.Out])] - (Epoll.wait e 1 0); + ignore(Unix.write a "arfbarf" 0 7); + expect [(a, [Epoll.Out]); (b, [Epoll.In; Epoll.Out])]; Epoll.ctl e Epoll.Delete (a, []); - assert_equal - ~printer:epollfds_as_string - [(b, [Epoll.Out])] - (Epoll.wait e 2 0); + expect [(b, [Epoll.In; Epoll.Out])]; assert_raises (Failure "ocaml_epoll_ctl: No such file or directory") (fun () -> @@ -85,25 +77,60 @@ let unit_tests = (Failure "ocaml_epoll_ctl: File exists") (fun () -> Epoll.ctl e Epoll.Add (b, [Epoll.In; Epoll.Priority])); - assert_equal - ~printer:epollfds_as_string - [(b, [Epoll.Out])] - (Epoll.wait e 2 0); + expect [(b, [Epoll.In; Epoll.Out])]; Unix.close b; - assert_equal - ~printer:epollfds_as_string - [] - (Epoll.wait e 2 0); + expect []; assert_raises (Failure "ocaml_epoll_ctl: Bad file descriptor") (fun () -> Epoll.ctl e Epoll.Modify (b, [Epoll.In; Epoll.Priority])); - Epoll.destroy e; - Unix.close a + Unix.close a; + Epoll.destroy e ); + "epoll accept" >:: + (fun () -> + let fn = "/tmp/narfblatt" in + let _ = + try + Unix.unlink fn + with _ -> + () + in + let addr = Unix.ADDR_UNIX fn in + let e = Epoll.create 3 in + let s = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in + let cli = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in + let expect what = + assert_equal + ~printer:epollfds_as_string + (List.sort compare what) + (List.sort compare (Epoll.wait e 3 0)) + in + Unix.bind s addr; + Unix.listen s 4; + Epoll.ctl e Epoll.Add (s, [Epoll.In]); + expect []; + + Unix.connect cli addr; + expect [(s, [Epoll.In])]; + + let srv, srv_addr = Unix.accept s in + Epoll.ctl e Epoll.Add (cli, [Epoll.In; Epoll.Out]); + Epoll.ctl e Epoll.Add (srv, [Epoll.In; Epoll.Out]); + expect [(cli, [Epoll.Out]); (srv, [Epoll.Out])]; + + ignore(Unix.single_write srv "Hello" 0 5); + expect [(cli, [Epoll.In; Epoll.Out]); (srv, [Epoll.Out])]; + + Unix.close cli; + Unix.close srv; + Unix.close s; + Epoll.destroy e; + ); + "command_of_string" >:: (fun () -> assert_equal