Fixed epoll_wrappers.c:event_list_of_int to lists with >1 item

This commit is contained in:
Neale Pickett 2008-03-11 17:48:16 -06:00
parent b032eef23a
commit c1d83191fe
2 changed files with 76 additions and 44 deletions

View File

@ -1,6 +1,6 @@
/** OCaml poll() interface /** 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 * Copyright (C) 2008 Neale Pickett
* *
@ -129,16 +129,21 @@ event_list_of_int(int events)
result = Val_int(0); 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) { if (events & EPOLLIN) {
result = cons(Val_int(caml_POLLIN), result); 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); CAMLreturn(result);
} }

View File

@ -42,41 +42,33 @@ let unit_tests =
(fun () -> (fun () ->
let a,b = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in let a,b = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
let e = Epoll.create 1 in let e = Epoll.create 1 in
Epoll.ctl e Epoll.Add (a, [Epoll.Out; Epoll.In]); let expect ?(n=3) what =
assert_equal assert_equal
~printer:epollfds_as_string ~printer:epollfds_as_string
[(a, [Epoll.Out])] (List.sort compare what)
(Epoll.wait e 1 0); (List.sort compare (Epoll.wait e n 0))
in
Epoll.ctl e Epoll.Add (a, [Epoll.Out; Epoll.In]);
expect [(a, [Epoll.Out])];
Epoll.ctl e Epoll.Modify (a, [Epoll.In; Epoll.Priority]); Epoll.ctl e Epoll.Modify (a, [Epoll.In; Epoll.Priority]);
assert_equal expect [];
~printer:epollfds_as_string
[]
(Epoll.wait e 1 0);
Epoll.ctl e Epoll.Add (b, [Epoll.Out; Epoll.In]); Epoll.ctl e Epoll.Add (b, [Epoll.Out; Epoll.In]);
assert_equal expect [(b, [Epoll.Out])];
~printer:epollfds_as_string
[(b, [Epoll.Out])]
(Epoll.wait e 2 0);
Epoll.ctl e Epoll.Modify (a, [Epoll.Out; Epoll.In]); Epoll.ctl e Epoll.Modify (a, [Epoll.Out; Epoll.In]);
expect [(a, [Epoll.Out]); (b, [Epoll.Out])];
assert_equal assert_equal
~printer:epollfds_as_string ~printer:string_of_int
[(a, [Epoll.Out]); (b, [Epoll.Out])] 1
(Epoll.wait e 2 0); (List.length (Epoll.wait e 1 0));
Epoll.ctl e Epoll.Modify (a, [Epoll.Out; Epoll.In]); ignore(Unix.write a "arfbarf" 0 7);
assert_equal expect [(a, [Epoll.Out]); (b, [Epoll.In; Epoll.Out])];
~printer:epollfds_as_string
[(b, [Epoll.Out])]
(Epoll.wait e 1 0);
Epoll.ctl e Epoll.Delete (a, []); Epoll.ctl e Epoll.Delete (a, []);
assert_equal expect [(b, [Epoll.In; Epoll.Out])];
~printer:epollfds_as_string
[(b, [Epoll.Out])]
(Epoll.wait e 2 0);
assert_raises assert_raises
(Failure "ocaml_epoll_ctl: No such file or directory") (Failure "ocaml_epoll_ctl: No such file or directory")
(fun () -> (fun () ->
@ -85,23 +77,58 @@ let unit_tests =
(Failure "ocaml_epoll_ctl: File exists") (Failure "ocaml_epoll_ctl: File exists")
(fun () -> (fun () ->
Epoll.ctl e Epoll.Add (b, [Epoll.In; Epoll.Priority])); Epoll.ctl e Epoll.Add (b, [Epoll.In; Epoll.Priority]));
assert_equal expect [(b, [Epoll.In; Epoll.Out])];
~printer:epollfds_as_string
[(b, [Epoll.Out])]
(Epoll.wait e 2 0);
Unix.close b; Unix.close b;
assert_equal expect [];
~printer:epollfds_as_string
[]
(Epoll.wait e 2 0);
assert_raises assert_raises
(Failure "ocaml_epoll_ctl: Bad file descriptor") (Failure "ocaml_epoll_ctl: Bad file descriptor")
(fun () -> (fun () ->
Epoll.ctl e Epoll.Modify (b, [Epoll.In; Epoll.Priority])); Epoll.ctl e Epoll.Modify (b, [Epoll.In; Epoll.Priority]));
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; Epoll.destroy e;
Unix.close a
); );
"command_of_string" >:: "command_of_string" >::