File: test_domain.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (42 lines) | stat: -rw-r--r-- 1,051 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
let%expect_test _ =
  let d = Domain.spawn (fun () -> 1 + 2) in
  print_int (Domain.join d);
  [%expect {| 3 |}];
  let d = Domain.spawn (fun () -> 1 + 2) in
  let d_id = Domain.get_id d in
  let id = Domain.self () in
  Printf.printf "d_id: %d\n" (d_id :> int);
  Printf.printf "self id: %d\n" (id :> int);
  let res = Domain.join d in
  Printf.printf "result: %d\n" res;
  [%expect {|
    d_id: 2
    self id: 0
    result: 3 |}]

type _ Effect.t += A : int Effect.t

let handle comp =
  Effect.Deep.try_with
    comp
    ()
    { effc =
        (fun (type a) (e : a Effect.t) ->
          match e with
          | A ->
              Some
                (fun (k : (a, _) Effect.Deep.continuation) -> Effect.Deep.continue k 42)
          | _ -> None)
    }

let er = ref Not_found

let%expect_test _ =
  handle (fun () -> print_int (Effect.perform A));
  [%expect {| 42 |}]

let%expect_test _ =
  let f () = Effect.perform A in
  handle (fun () ->
      if Random.int 2 < 1 then print_int (1 + f ()) else print_int (f () + 1));
  [%expect {| 43 |}]