Skip to content

Commit

Permalink
tests: fix usage of sendfile
Browse files Browse the repository at this point in the history
  • Loading branch information
fdopen committed Oct 11, 2016
1 parent 806c3ba commit 212e09d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
12 changes: 10 additions & 2 deletions examples/copy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ let copy_ba ~src ~dst =
let copy_sendfile ~src ~dst =
with_file ~mode:[ O_RDONLY ] src @@ fun fd_read ->
with_file ~mode:[ O_WRONLY ; O_CREAT ; O_TRUNC ] dst @@ fun fd_write ->
Uwt.Fs.sendfile ~dst:fd_write ~src:fd_read () >>= fun _i ->
Lwt.return_unit
Uwt.Fs.fstat fd_read >>= fun x ->
let total_length = x.Uwt.Fs.st_size in
let rec iter pos =
if Int64.sub total_length pos <= Int64.zero then
Lwt.return_unit
else
Uwt.Fs.sendfile ~pos ~dst:fd_write ~src:fd_read () >>= fun i ->
iter @@ Int64.add pos @@ Int64.of_nativeint i
in
iter Int64.zero

let () =
let files = ref []
Expand Down
11 changes: 9 additions & 2 deletions examples/copy_sync.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ let copy_sendfile ~src ~dst =
rexn ("open " ^ dst)
in
try_finally ( fun fdo ->
ignore (US.sendfile ~dst:fdo ~src:fdi () |> rexn "sendfile");
) fdo ( fun fdo -> US.close fdo |> rexn ("close " ^ dst) ) fdo
let total_length = US.(((fstat fdi) |> rexn "fstat").st_size) in
let rec iter pos =
if Int64.sub total_length pos <= Int64.zero then () else
let i = US.sendfile ~pos ~dst:fdo ~src:fdi ()
|> rexn "sendfile" in
iter @@ Int64.add pos @@ Int64.of_nativeint i
in
iter Int64.zero
) fdo (fun fdo -> US.close fdo |> rexn ("close " ^ dst)) fdo
)
fdi ( fun fdi -> US.close fdi |> rexn ("close " ^ src) ) fdi

Expand Down
12 changes: 10 additions & 2 deletions test/t_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ let copy_ba ~src ~dst =
let copy_sendfile ~src ~dst =
with_file ~mode:[ O_RDONLY ] src @@ fun fd_read ->
with_file ~mode:[ O_WRONLY ; O_CREAT ; O_TRUNC ] dst @@ fun fd_write ->
Uwt.Fs.sendfile ~dst:fd_write ~src:fd_read () >>= fun _i ->
Lwt.return_unit
Uwt.Fs.fstat fd_read >>= fun x ->
let total_length = x.Uwt.Fs.st_size in
let rec iter pos =
if Int64.sub total_length pos <= Int64.zero then
Lwt.return_unit
else
Uwt.Fs.sendfile ~pos ~dst:fd_write ~src:fd_read () >>= fun i ->
iter @@ Int64.add pos @@ Int64.of_nativeint i
in
iter Int64.zero

let random_bytes_length = 228_409
let random_bytes = rbytes_create random_bytes_length
Expand Down
12 changes: 10 additions & 2 deletions test/t_fs_sync.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,16 @@ let copy_ba ~src ~dst =
let copy_sendfile ~src ~dst =
with_file ~mode:[ O_RDONLY ] src @@ fun fd_read ->
with_file ~mode:[ O_WRONLY ; O_CREAT ; O_TRUNC ] dst @@ fun fd_write ->
US.sendfile ~dst:fd_write ~src:fd_read () >>= fun _i ->
Ok ()
US.fstat fd_read >>= fun x ->
let total_length = x.US.st_size in
let rec iter pos =
if Int64.sub total_length pos <= Int64.zero then
Ok ()
else
US.sendfile ~pos ~dst:fd_write ~src:fd_read () >>= fun i ->
iter @@ Int64.add pos @@ Int64.of_nativeint i
in
iter Int64.zero

let random_bytes_length = 88_411
let random_bytes = Common.rbytes_create random_bytes_length
Expand Down

0 comments on commit 212e09d

Please sign in to comment.