File: test_lwt_io.ml

package info (click to toggle)
lwt 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: ml: 22,030; ansic: 7,167; makefile: 92; python: 62
file content (675 lines) | stat: -rw-r--r-- 22,390 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)



(* [Lwt_sequence] is deprecated – we don't want users outside Lwt using it.
   However, it is still used internally by Lwt. So, briefly disable warning 3
   ("deprecated"), and create a local, non-deprecated alias for
   [Lwt_sequence] that can be referred to by the rest of the code in this
   module without triggering any more warnings. *)
module Lwt_sequence = Lwt_sequence

open Test
open Lwt.Infix

exception Dummy_error

let local =
  let last_port = ref 4321 in
  fun () ->
    incr last_port;
    Unix.ADDR_INET (Unix.inet_addr_loopback, !last_port)

(* Helpers for [establish_server] tests. *)
module Establish_server =
struct
  let with_client f =
    let local = local () in

    let handler_finished, notify_handler_finished = Lwt.wait () in

    Lwt_io.establish_server_with_client_address
      local
      (fun _client_address channels ->
        Lwt.finalize
          (fun () -> f channels)
          (fun () ->
            Lwt.wakeup notify_handler_finished ();
            Lwt.return_unit))

    >>= fun server ->

    let client_finished =
      Lwt_io.with_connection
        local
        (fun (_, out_channel) ->
          Lwt_io.write out_channel "hello world" >>= fun () ->
          handler_finished)
    in

    client_finished >>= fun () ->
    Lwt_io.shutdown_server server

  (* Hacky is_closed functions that attempt to read from/write to the channels
     to see if they are closed. *)
  let is_closed_in channel =
    Lwt.catch
      (fun () -> Lwt_io.read_char channel >|= fun _ -> false)
      (function
      | Lwt_io.Channel_closed _ -> Lwt.return_true
      | _ -> Lwt.return_false)

  let is_closed_out channel =
    Lwt.catch
      (fun () -> Lwt_io.write_char channel 'a' >|= fun () -> false)
      (function
      | Lwt_io.Channel_closed _ -> Lwt.return_true
      | _ -> Lwt.return_false)
end

let suite = suite "lwt_io" [
  test "auto-flush" ~sequential:true
    (fun () ->
      let sent = ref [] in
      let oc =
        Lwt_io.make
          ~mode:Lwt_io.output
          (fun buf ofs len ->
            let bytes = Bytes.create len in
            Lwt_bytes.blit_to_bytes buf ofs bytes 0 len;
            sent := bytes :: !sent;
            Lwt.return len)
      in
      Lwt_io.write oc "foo" >>= fun () ->
      Lwt_io.write oc "bar" >>= fun () ->
      if !sent <> [] then begin
        prerr_endline "auto-flush: !sent not empty";
        Lwt.return_false
      end
      else
        Lwt_unix.sleep 0.1 >>= fun () ->
        let test_result = !sent = [Bytes.of_string "foobar"] in
        if not test_result then
          !sent
          |> List.map Bytes.to_string
          |> List.map (Printf.sprintf "'%s'")
          |> String.concat ","
          |> Printf.eprintf "auto-flush: !sent = %s";
        Lwt.return test_result);

  test "auto-flush in atomic" ~sequential:true
    (fun () ->
      let sent = ref [] in
      let oc =
        Lwt_io.make
          ~mode:Lwt_io.output
          (fun buf ofs len ->
            let bytes = Bytes.create len in
            Lwt_bytes.blit_to_bytes buf ofs bytes 0 len;
            sent := bytes :: !sent;
            Lwt.return len)
      in
      Lwt_io.atomic
        (fun oc ->
          Lwt_io.write oc "foo" >>= fun () ->
          Lwt_io.write oc "bar" >>= fun () ->
          if !sent <> [] then begin
            prerr_endline "auto-flush atomic: !sent not empty";
            Lwt.return_false
          end
          else
            Lwt_unix.sleep 0.1 >>= fun () ->
            let test_result = !sent = [Bytes.of_string "foobar"] in
            if not test_result then
              !sent
              |> List.map Bytes.to_string
              |> List.map (Printf.sprintf "'%s'")
              |> String.concat ","
              |> Printf.eprintf "auto-flush atomic: !sent = %s";
            Lwt.return test_result)
        oc);

  (* Without the corresponding bugfix, which is to handle ENOTCONN from
     Lwt_unix.shutdown, this test raises an exception from the handler's calls
     to close. *)
  test "establish_server_1: shutdown: client closes first"
    ~only_if:(fun () ->
      not (Lwt_config._HAVE_LIBEV && Lwt_config.libev_default))
  (* Note: this test is currently flaky on Linux with libev enabled, so we skip
     it in that case. *)
    (fun () ->
      let wait_for_client, client_finished = Lwt.wait () in

      let handler_wait, run_handler = Lwt.wait () in
      let handler =
        handler_wait >>= fun (in_channel, out_channel) ->
        wait_for_client >>= fun () ->
        Lwt_io.close in_channel >>= fun () ->
        Lwt_io.close out_channel >>= fun () ->
        Lwt.return_true
      in

      let local = local () in

      let server =
        (Lwt_io.Versioned.establish_server_1 [@ocaml.warning "-3"])
          local (fun channels -> Lwt.wakeup run_handler channels)
      in

      Lwt_io.with_connection local (fun _ -> Lwt.return_unit) >>= fun () ->
      Lwt.wakeup client_finished ();
      Lwt_io.shutdown_server server >>= fun () ->
      handler);

  (* Counterpart to establish_server: shutdown test. Confirms that shutdown is
     implemented correctly in open_connection. *)
  test "open_connection: shutdown: server closes first"
    (fun () ->
      let wait_for_server, server_finished = Lwt.wait () in

      let local = local () in

      let server =
        (Lwt_io.Versioned.establish_server_1 [@ocaml.warning "-3"])
          local (fun (in_channel, out_channel) ->
            Lwt.async (fun () ->
              Lwt_io.close in_channel >>= fun () ->
              Lwt_io.close out_channel >|= fun () ->
              Lwt.wakeup server_finished ()))
      in

      Lwt_io.with_connection local (fun _ ->
        wait_for_server >>= fun () ->
        Lwt.return_true)

      >>= fun result ->

      Lwt_io.shutdown_server server >|= fun () ->
      result);

  test "establish_server: implicit close"
    (fun () ->
      let open Establish_server in

      let in_channel' = ref Lwt_io.stdin in
      let out_channel' = ref Lwt_io.stdout in

      let in_open_in_handler = ref false in
      let out_open_in_handler = ref false in

      let run =
        Establish_server.with_client
          (fun (in_channel, out_channel) ->
            in_channel' := in_channel;
            out_channel' := out_channel;

            is_closed_out out_channel >>= fun yes ->
            out_open_in_handler := not yes;

            is_closed_in in_channel >|= fun yes ->
            in_open_in_handler := not yes)
      in

      run >>= fun () ->
      (* Give a little time for the close system calls on the connection sockets
         to complete. The Lwt_io and Lwt_unix APIs do not currently allow
         binding on the implicit closes of these sockets, so resorting to a
         delay. *)
      Lwt_unix.sleep 0.05 >>= fun () ->

      is_closed_in !in_channel' >>= fun in_closed_after_handler ->
      is_closed_out !out_channel' >|= fun out_closed_after_handler ->

      !out_open_in_handler &&
      !in_open_in_handler &&
      in_closed_after_handler &&
      out_closed_after_handler);

  test ~sequential:true "establish_server: implicit close on exception"
    (fun () ->
      let open Establish_server in

      let in_channel' = ref Lwt_io.stdin in
      let out_channel' = ref Lwt_io.stdout in
      let exit_raised = ref false in

      let run () =
        Establish_server.with_client
          (fun (in_channel, out_channel) ->
            in_channel' := in_channel;
            out_channel' := out_channel;
            raise Exit)
      in

      with_async_exception_hook
        (function
        | Exit -> exit_raised := true;
        | _ -> ())
        run

      >>= fun () ->
      (* See comment in other implicit close test. *)
      Lwt_unix.sleep 0.05 >>= fun () ->

      is_closed_in !in_channel' >>= fun in_closed_after_handler ->
      is_closed_out !out_channel' >|= fun out_closed_after_handler ->

      in_closed_after_handler && out_closed_after_handler);

  (* This does a simple double close of the channels (second close is implicit).
     If something breaks, the test will finish with an exception, or
     Lwt.async_exception_hook will kill the process. *)
  test "establish_server: explicit close"
    (fun () ->
      let open Establish_server in

      let closed_explicitly = ref false in

      let run =
        Establish_server.with_client
          (fun (in_channel, out_channel) ->
            Lwt_io.close in_channel >>= fun () ->
            Lwt_io.close out_channel >>= fun () ->
            is_closed_in in_channel >>= fun in_closed_in_handler ->
            is_closed_out out_channel >|= fun out_closed_in_handler ->
            closed_explicitly := in_closed_in_handler && out_closed_in_handler)
      in

      run >|= fun () ->
      !closed_explicitly);

  test "with_connection"
    (fun () ->
      let open Establish_server in

      let in_channel' = ref Lwt_io.stdin in
      let out_channel' = ref Lwt_io.stdout in

      let local = local () in

      Lwt_io.establish_server_with_client_address local
        (fun _client_address _channels -> Lwt.return_unit)
      >>= fun server ->

      Lwt_io.with_connection local (fun (in_channel, out_channel) ->
        in_channel' := in_channel;
        out_channel' := out_channel;
        Lwt.return_unit)

      >>= fun () ->
      Lwt_io.shutdown_server server >>= fun () ->
      is_closed_in !in_channel' >>= fun in_closed ->
      is_closed_out !out_channel' >|= fun out_closed ->
      in_closed && out_closed);

  (* Makes the channel fail with EBADF on close. Tries to close the channel
     manually, and handles the exception. When with_close_connection tries to
     close the socket again implicitly, that should not raise the exception
     again. *)
  test "with_close_connection: no duplicate exceptions"
    (fun () ->
      let exceptions_observed = ref 0 in

      let expecting_ebadf f =
        Lwt.catch f
          (function
            | Unix.Unix_error (Unix.EBADF, _, _) ->
              exceptions_observed := !exceptions_observed + 1;
              Lwt.return_unit
            | exn ->
              Lwt.reraise exn)
      in

      let fd_r, fd_w = Lwt_unix.pipe () in
      let in_channel = Lwt_io.of_fd ~mode:Lwt_io.input fd_r in
      let out_channel = Lwt_io.of_fd ~mode:Lwt_io.output fd_w in

      Lwt_unix.close fd_r >>= fun () ->
      Lwt_unix.close fd_w >>= fun () ->

      expecting_ebadf (fun () ->
        Lwt_io.with_close_connection
          (fun _ ->
            expecting_ebadf (fun () -> Lwt_io.close in_channel) >>= fun () ->
            expecting_ebadf (fun () -> Lwt_io.close out_channel))
          (in_channel, out_channel))
      >|= fun () ->
      !exceptions_observed = 2);

  test "open_temp_file"
    (fun () ->
       Lwt_io.open_temp_file () >>= fun (fname, out_chan) ->
       Lwt_io.write out_chan "test file content" >>= fun () ->
       Lwt_io.close out_chan >>= fun _ ->
       Unix.unlink fname; Lwt.return_true
    );

  test "with_temp_filename"
    (fun () ->
       let prefix = "test_tempfile" in
      let filename = ref "." in
      let wrap f (filename', chan) = filename := filename'; f chan in
      let write_data chan = Lwt_io.write chan "test file content" in
      let write_data_fail _ = Lwt.fail Dummy_error in
      Lwt_io.with_temp_file (wrap write_data) ~prefix >>= fun _ ->
      let no_temps1 = not (Sys.file_exists !filename) in
       Lwt.catch
         (fun () -> Lwt_io.with_temp_file (wrap write_data_fail))
         (fun exn ->
            if exn = Dummy_error
            then Lwt.return (not (Sys.file_exists !filename))
            else Lwt.return_false
         )
       >>= fun no_temps2 ->
       Lwt.return (no_temps1 && no_temps2)
    );

  (* Verify that no exceptions are thrown if the function passed to
     with_temp_file closes the channel on its own. *)
  test "with_temp_filename close handle"
    (fun () ->
       let f (_, chan) = Lwt_io.write chan "test file content" >>= fun _ ->
         Lwt_io.close chan in
       Lwt_io.with_temp_file f >>= fun _ -> Lwt.return_true;
    );

  test "create_temp_dir" begin fun () ->
    let prefix = "temp_dir" in
    let suffix = "_foo" in
    Lwt_io.create_temp_dir ~parent:Filename.current_dir_name ~prefix ~suffix ()
      >>= fun path ->

    let name = Filename.basename path in
    let prefix_matches = String.sub name 0 (String.length prefix) = prefix in
    let actual_suffix =
      String.sub
        name (String.length name - String.length suffix) (String.length suffix)
    in
    let suffix_matches = actual_suffix = suffix in
    let directory_exists = Sys.is_directory path in

    Lwt_unix.rmdir path >>= fun () ->

    Lwt.return (prefix_matches && suffix_matches && directory_exists)
  end;

  test "with_temp_dir" ~sequential:true begin fun () ->
    Lwt_io.with_temp_dir ~parent:Filename.current_dir_name ~prefix:"temp_dir"
        begin fun path ->

      let directory_existed = Sys.is_directory path in

      open_out (Filename.concat path "foo") |> close_out;
      open_out (Filename.concat path "bar") |> close_out;
      let had_files = Array.length (Sys.readdir path) = 2 in

      Lwt.return (path, directory_existed, had_files)
    end >>= fun (path, directory_existed, had_files) ->

    let directory_removed = not (Sys.file_exists path) in

    Lwt.return (directory_existed && had_files && directory_removed)
  end;

  test "file_length on directory" begin fun () ->
    Lwt.catch
      (fun () ->
        Lwt_io.file_length "." >>= fun _ ->
        Lwt.return_false)
      (function
      | Unix.Unix_error (Unix.EISDIR, "file_length", ".") ->
        Lwt.return_true
      | exn -> Lwt.reraise exn)
  end;

  test "input channel of_bytes initial position"
    (fun () ->
       let ichan = Lwt_io.of_bytes ~mode:Lwt_io.input @@ Lwt_bytes.of_string "abcd" in
       Lwt.return (Lwt_io.position ichan = 0L)
    );

  test "input channel of_bytes position after read"
    (fun () ->
       let ichan = Lwt_io.of_bytes ~mode:Lwt_io.input @@ Lwt_bytes.of_string "abcd" in
       Lwt_io.read_char ichan >|= fun _ ->
       Lwt_io.position ichan = 1L
    );

  test "input channel of_bytes position after set_position"
    (fun () ->
       let ichan = Lwt_io.of_bytes ~mode:Lwt_io.input @@ Lwt_bytes.of_string "abcd" in
       Lwt_io.set_position ichan 2L >|= fun () ->
       Lwt_io.position ichan = 2L
    );

  test "output channel of_bytes initial position"
    (fun () ->
       let ochan = Lwt_io.of_bytes ~mode:Lwt_io.output @@ Lwt_bytes.create 4 in
       Lwt.return (Lwt_io.position ochan = 0L)
    );

  test "output channel of_bytes position after read"
    (fun () ->
       let ochan = Lwt_io.of_bytes ~mode:Lwt_io.output @@ Lwt_bytes.create 4 in
       Lwt_io.write_char ochan 'a' >|= fun _ ->
       Lwt_io.position ochan = 1L
    );

  test "output channel of_bytes position after set_position"
    (fun () ->
       let ochan = Lwt_io.of_bytes ~mode:Lwt_io.output @@ Lwt_bytes.create 4 in
       Lwt_io.set_position ochan 2L >|= fun _ ->
       Lwt_io.position ochan = 2L
    );

  test "NumberIO.LE.read_int" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_int
    >|= (=) 0x04030201
  end;

  test "NumberIO.BE.read_int" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_int
    >|= (=) 0x01020304
  end;

  test "NumberIO.LE.read_int16" begin fun () ->
    Lwt_bytes.of_string "\x01\x02"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_int16
    >|= (=) 0x0201
  end;

  test "NumberIO.BE.read_int16" begin fun () ->
    Lwt_bytes.of_string "\x01\x02"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_int16
    >|= (=) 0x0102
  end;

  test "NumberIO.LE.read_int16, negative" begin fun () ->
    Lwt_bytes.of_string "\xfe\xff"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_int16
    >|= (=) (-2)
  end;

  test "NumberIO.BE.read_int16, negative" begin fun () ->
    Lwt_bytes.of_string "\xff\xfe"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_int16
    >|= (=) (-2)
  end;

  test "NumberIO.LE.read_int32" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_int32
    >|= (=) 0x04030201l
  end;

  test "NumberIO.BE.read_int32" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_int32
    >|= (=) 0x01020304l
  end;

  test "NumberIO.LE.read_int64" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04\x05\x06\x07\x08"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_int64
    >|= (=) 0x0807060504030201L
  end;

  test "NumberIO.BE.read_int64" begin fun () ->
    Lwt_bytes.of_string "\x01\x02\x03\x04\x05\x06\x07\x08"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_int64
    >|= (=) 0x0102030405060708L
  end;

  test "NumberIO.LE.read_float32" begin fun () ->
    Lwt_bytes.of_string "\x80\x01\x81\x47"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_float32
    >|= fun n -> instrument (n = 66051.) "NumberIO.LE.read_float32: %f" n
  end;

  test "NumberIO.BE.read_float32" begin fun () ->
    Lwt_bytes.of_string "\x47\x81\x01\x80"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_float32
    >|= fun n -> instrument (n = 66051.) "NumberIO.BE.read_float32: %f" n
  end;

  test "NumberIO.LE.read_float64" begin fun () ->
    Lwt_bytes.of_string "\x70\x60\x50\x40\x30\x20\xf0\x42"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.LE.read_float64
    >|= Int64.bits_of_float
    >|= (=) 0x42F0203040506070L
  end;

  test "NumberIO.BE.read_float64" begin fun () ->
    Lwt_bytes.of_string "\x42\xf0\x20\x30\x40\x50\x60\x70"
    |> Lwt_io.(of_bytes ~mode:input)
    |> Lwt_io.BE.read_float64
    >|= Int64.bits_of_float
    >|= (=) 0x42F0203040506070L
  end;

  test "NumberIO.LE.write_int" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.LE.write_int (Lwt_io.(of_bytes ~mode:output) buffer)
      0x01020304 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x04\x03\x02\x01")
  end;

  test "NumberIO.BE.write_int" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.BE.write_int (Lwt_io.(of_bytes ~mode:output) buffer)
      0x01020304 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x01\x02\x03\x04")
  end;

  test "NumberIO.LE.write_int16" begin fun () ->
    let buffer = Lwt_bytes.create 2 in
    Lwt_io.LE.write_int16 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x0102 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x02\x01")
  end;

  test "NumberIO.BE.write_int16" begin fun () ->
    let buffer = Lwt_bytes.create 2 in
    Lwt_io.BE.write_int16 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x0102 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x01\x02")
  end;

  test "NumberIO.LE.write_int32" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.LE.write_int32 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x01020304l >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x04\x03\x02\x01")
  end;

  test "NumberIO.BE.write_int32" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.BE.write_int32 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x01020304l >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x01\x02\x03\x04")
  end;

  test "NumberIO.LE.write_int64" begin fun () ->
    let buffer = Lwt_bytes.create 8 in
    Lwt_io.LE.write_int64 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x0102030405060708L >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x08\x07\x06\x05\x04\x03\x02\x01")
  end;

  test "NumberIO.BE.write_int64" begin fun () ->
    let buffer = Lwt_bytes.create 8 in
    Lwt_io.BE.write_int64 (Lwt_io.(of_bytes ~mode:output) buffer)
      0x0102030405060708L >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x01\x02\x03\x04\x05\x06\x07\x08")
  end;

  test "NumberIO.LE.write_float32" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.LE.write_float32 (Lwt_io.(of_bytes ~mode:output) buffer)
      66051. >|= fun () ->
    instrument (Lwt_bytes.to_string buffer = "\x80\x01\x81\x47")
      "NumberIO.LE.write_float32: %02X %02X %02X %02X"
      (Char.code (Lwt_bytes.get buffer 0))
      (Char.code (Lwt_bytes.get buffer 1))
      (Char.code (Lwt_bytes.get buffer 2))
      (Char.code (Lwt_bytes.get buffer 3))
  end;

  test "NumberIO.BE.write_float32" begin fun () ->
    let buffer = Lwt_bytes.create 4 in
    Lwt_io.BE.write_float32 (Lwt_io.(of_bytes ~mode:output) buffer)
      66051. >|= fun () ->
    instrument (Lwt_bytes.to_string buffer = "\x47\x81\x01\x80")
      "NumberIO.BE.write_float32: %02X %02X %02X %02X"
      (Char.code (Lwt_bytes.get buffer 0))
      (Char.code (Lwt_bytes.get buffer 1))
      (Char.code (Lwt_bytes.get buffer 2))
      (Char.code (Lwt_bytes.get buffer 3))
  end;

  test "NumberIO.LE.write_float64" begin fun () ->
    let buffer = Lwt_bytes.create 8 in
    Lwt_io.LE.write_float64 (Lwt_io.(of_bytes ~mode:output) buffer)
      (Int64.float_of_bits 0x42F0203040506070L) >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x70\x60\x50\x40\x30\x20\xf0\x42")
  end;

  test "NumberIO.BE.write_float64" begin fun () ->
    let buffer = Lwt_bytes.create 8 in
    Lwt_io.BE.write_float64 (Lwt_io.(of_bytes ~mode:output) buffer)
      (Int64.float_of_bits 0x42F0203040506070L) >>= fun () ->
    Lwt.return (Lwt_bytes.to_string buffer = "\x42\xf0\x20\x30\x40\x50\x60\x70")
  end;

  test "Write from Lwt_bytes" begin fun () ->
    let bytes = Lwt_bytes.of_string "Hello World" in
    let out = Lwt_bytes.create 11 in
    Lwt_io.write_from_exactly_bigstring (Lwt_io.(of_bytes ~mode:output) out)
      bytes 0 11 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string out = "Hello World")
  end;

  test "Read from Lwt_bytes" begin fun () ->
    let bytes_in = Lwt_bytes.create 11 in
    let bytes = Lwt_bytes.of_string "Hello World" in
    Lwt_io.read_into_exactly_bigstring (Lwt_io.(of_bytes ~mode:input) bytes)
      bytes_in 0 11 >>= fun () ->
    Lwt.return (Lwt_bytes.to_string bytes_in = "Hello World")
  end;
]