Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[drain] would loop infinitely if no bytes were currently pending. #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/faraday.ml
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ let drain =
let len = IOVec.lengthv iovecs in
shift t len;
loop t (len + acc)
| `Close -> acc
| `Yield -> loop t acc
| `Close | `Yield -> acc
in
fun t -> loop t 0
33 changes: 31 additions & 2 deletions lib_test/test_faraday.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ let write =
[ "char" , `Quick, char
; "single w/ room" , `Quick, (write : unit -> unit)
; "single w/o room", `Quick, write ~buf_size:1
; "multiple" , `Quick, write_multiple
; "multiple" , `Quick, write_multiple
]

let schedule () =
Expand Down Expand Up @@ -217,11 +217,40 @@ let interleaved serialize =
[`Write_char 't'; `Write_bytes "t"; `Write_string "t"])
end ]

module Test_drain = struct
let normal () =
let t = create 0x100 in
write_string t "hello";
close t;
Alcotest.(check' int) ~msg:"drain" ~expected:5 ~actual:(drain t)
;;

let before_close () =
let t = create 0x100 in
write_string t "hello";
close t;
Alcotest.(check' int) ~msg:"drain" ~expected:5 ~actual:(drain t)
;;

let nothing_pending () =
let t = create 0x100 in
Alcotest.(check' int) ~msg:"drain" ~expected:0 ~actual:(drain t)
;;

let tests =
[ "normal", `Quick, normal
; "before close", `Quick, before_close
; "nothing pending", `Quick, nothing_pending
]
end

let () =
Alcotest.run "test suite"
[ "empty output" , empty
; "endianness" , endian
; "write" , write
; "single schedule" , schedule
; "interleaved calls (string)" , interleaved serialize_to_string
; "interleaved calls (bigstring)" , interleaved serialize_to_bigstring']
; "interleaved calls (bigstring)" , interleaved serialize_to_bigstring'
; "drain" , Test_drain.tests
]