Skip to content

Commit ab07965

Browse files
additional test for multidomain: pipe communications
1 parent 7fe46b8 commit ab07965

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

test/multidomain/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(tests
2-
(names basic domainworkers movingpromises)
2+
(names basic domainworkers movingpromises unixpipe)
33
(libraries lwt lwt.unix))

test/multidomain/unixpipe.ml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
open Lwt.Syntax
2+
3+
let checks = Atomic.make 0
4+
5+
let () = Lwt_unix.init_domain ()
6+
7+
let write w s =
8+
let b = Bytes.unsafe_of_string s in
9+
let* l = Lwt_unix.write w b 0 (Bytes.length b) in
10+
assert (l = Bytes.length b);
11+
Lwt.return_unit
12+
13+
let read r n =
14+
let b = Bytes.create n in
15+
let* l = Lwt_unix.read r b 0 n in
16+
assert (l = n);
17+
Lwt.return (Bytes.unsafe_to_string b)
18+
19+
let rec run data w r =
20+
let* () = Lwt.pause () in
21+
match data with
22+
| [] -> Lwt.return_unit
23+
| datum::data ->
24+
let* () = write w datum in
25+
let* readed = read r (String.length datum) in
26+
assert (datum = readed);
27+
Atomic.incr checks;
28+
run data w r
29+
30+
let run_in_domain data w r = Domain.spawn (fun () -> Lwt_main.run (run data w r))
31+
32+
let (a_from_b, b_to_a) = Lwt_unix.pipe ()
33+
let (b_from_a, a_to_b) = Lwt_unix.pipe ()
34+
let data = [ "aaa"; "bbbb"; "alhskjdflkhjasdflkhjhjklasfdlhjksadxf" ]
35+
36+
let a2b = run_in_domain data a_to_b a_from_b
37+
let b2a = run_in_domain data b_to_a b_from_a
38+
39+
let () = Domain.join a2b
40+
let () = Domain.join b2a
41+
let () =
42+
if Atomic.get checks = 2 * List.length data then begin
43+
Printf.printf "unixpipe: ✓\n";
44+
exit 0
45+
end else begin
46+
Printf.printf "unixpipe: ×\n";
47+
exit 1
48+
end

0 commit comments

Comments
 (0)