Skip to content

Commit

Permalink
more/better tests
Browse files Browse the repository at this point in the history
# Conflicts:
#	test/fairness_prio.ml
  • Loading branch information
gares committed Nov 7, 2024
1 parent 36a4c1a commit d3ac99a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
21 changes: 19 additions & 2 deletions test/fairness_prio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let%test_unit "sel.loop" =
write "Content-Length: 3\n\n123\n";
let x = Sel.now ~priority:2 (Ok "bad") in
let todo = Todo.add Todo.empty [e;x] in
let rec loop todo =
let loop todo =
let ready, _todo = Sel.pop todo in
match ready with
| Ok "bad" -> [%test_eq: string] "" "bad1"
Expand All @@ -77,11 +77,28 @@ let%test_unit "sel.loop2" =
write "Content-Length: 3\n\n12";
let x = Sel.now ~priority:2 (Ok "bad") in
let todo = Todo.add Todo.empty [e;x] in
let rec loop todo =
let loop todo =
let ready, _todo = Sel.pop todo in
match ready with
| Ok "bad" -> ()
| Ok _ -> [%test_eq: string] "" "bad2"
| Error e -> [%test_eq: string] "" (Stdlib.Printexc.to_string e) in
loop todo

(* pop_opt terminates *)
let%test_unit "sel.loop3" =
let read, write = pipe () in
let e = On.line ~priority:1 read (fun x -> x) in
write "aa\nbb\ncc\n";
let read2, write2 = pipe () in
let x = On.bytes ~priority:2 read2 2 (function Error e -> Error e | Ok s -> Error (Failure (Stdlib.Format.asprintf "lower priority event triggered: '%s'" (Bytes.to_string s)))) in
let todo = Todo.add Todo.empty [e;x] in
let rec loop todo =
let ready, todo = Sel.pop todo in
match ready with
| Ok "cc" -> ()
| Ok s -> write2 s; loop (Todo.add todo [e])
| Error End_of_file -> ()
| Error e -> [%test_eq: string] "" (Stdlib.Printexc.to_string e) in
loop todo

30 changes: 14 additions & 16 deletions test/perf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ let read_leftover read n =

(*****************************************************************************)

(* pop_opt terminates *)
let%test_unit "sel.loop" =
let %test_unit "sel.event.http_cle" =
let read, write = pipe () in
let e = On.line ~priority:1 read (fun x -> x) in
write "aa\nbb\ncc\n";
let read2, write2 = pipe () in
let x = On.bytes ~priority:2 read2 2 (function Error e -> Error e | Ok s -> Error (Failure (Stdlib.Format.asprintf "lower priority event triggered: '%s'" (Bytes.to_string s)))) in
let todo = Todo.add Todo.empty [e;x] in
let rec loop todo =
let ready, todo = Sel.pop todo in
match ready with
| Ok "cc" -> ()
| Ok s -> write2 s; loop (Todo.add todo [e])
| Error End_of_file -> ()
| Error e -> [%test_eq: string] "" (Stdlib.Printexc.to_string e) in
loop todo

let e = On.httpcle read b2s in
let t0 = Unix.gettimeofday () in
let n = 99999 in
for _i = 1 to n do
let todo = Todo.add Todo.empty [e] in
write "content-Length: 4\n\n1\n3.";
let ready, todo = pop_opt todo in
[%test_eq: bool] (Todo.is_empty todo) true;
[%test_eq: string option] ready (Some "1\n3.");
done;
let t1 = Unix.gettimeofday () in
Stdlib.Printf.eprintf "time to pop %d httpcle events: %f\n" n (t1 -. t0)
;;
6 changes: 6 additions & 0 deletions test/test1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ let %test_unit "sel.event.http_cle.recurring.err" =
write "a\n";
let ready, todo = pop_opt todo in
[%test_eq: bool] (Todo.is_empty todo) true;
(* depending on how eager the event is to read data *)
[%test_eq: bool] (osmatch "\\(.*Scan_failure.*\\|End_of_file\\)" ready) true;
let todo = Todo.add todo [e] in
write "content-Lengtx: 2\n";
let ready, todo = pop_opt todo in
[%test_eq: bool] (Todo.is_empty todo) true;
[%test_eq: bool] (osmatch ".*Scan_failure.*" ready) true;
let todo = Todo.add todo [e] in
write "content-Length: 4\n\n4\n6.";
Expand Down

0 comments on commit d3ac99a

Please sign in to comment.