Source file test_exns.ml

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
open! Core
open! Import
open Await
open Expect_test_helpers_core

type nothing = |

type scheduler =
  | Seq
  | Work_stealing

let run sched ~f =
  try
    match sched with
    | Seq -> Common.Sequential.parallel f
    | Work_stealing -> Common.Parallel.parallel f
  with
  | exn -> printf "Top level: %s\n" (Exn.to_string exn)
;;

let run_concurrent ~f =
  try Parallel_scheduler.with_concurrent f with
  | exn -> printf "Top level: %s\n" (Exn.to_string exn)
;;

let%expect_test "raise" =
  run Seq ~f:(fun _ -> failwith "fail");
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "fork raise" =
  run Seq ~f:(fun parallel ->
    match Parallel.fork_join parallel [ (fun _ -> failwith "fail") ] with
    | [ (_ : nothing) ] -> .);
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "for raise" =
  run Seq ~f:(fun parallel ->
    Parallel.for_ parallel ~start:0 ~stop:1 ~f:(fun _ i ->
      failwith ("fail" ^ Int.to_string i)));
  [%expect {| Top level: (Failure fail0) |}]
;;

let%expect_test "fork raise multiple" =
  run Seq ~f:(fun parallel ->
    match
      Parallel.fork_join
        parallel
        [ (fun _ -> failwith "fail0")
        ; (fun _ -> failwith "fail1")
        ; (fun _ -> failwith "fail2")
        ]
    with
    | [ (_ : nothing); (_ : nothing); (_ : nothing) ] -> .);
  [%expect {| Top level: (Failure fail0) |}]
;;

let%expect_test "seq raise" =
  run Seq ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun _ -> print_endline "hello")
        (fun _ ->
          match (failwith "fail" : nothing) with
          | _ -> .)
    in
    ());
  [%expect
    {|
    hello
    Top level: (Failure fail)
    |}]
;;

let%expect_test "seq raise2" =
  run Seq ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun _ ->
          match (failwith "fail1" : nothing) with
          | _ -> .)
        (fun _ ->
          match (failwith "fail2" : nothing) with
          | _ -> .)
    in
    ());
  [%expect {| Top level: (Failure fail1) |}]
;;

let%expect_test "for raise" =
  run Seq ~f:(fun parallel ->
    Parallel.for_ parallel ~start:0 ~stop:2 ~f:(fun _ i ->
      if i = 0 then print_endline "hello" else failwith "fail"));
  [%expect
    {|
    hello
    Top level: (Failure fail)
    |}]
;;

let%expect_test "ws raise1" =
  run Work_stealing ~f:(fun _ -> failwith "fail");
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "ws raise" =
  run Work_stealing ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun _ -> print_endline "hello")
        (fun _ ->
          match (failwith "fail" : nothing) with
          | _ -> .)
    in
    ());
  [%expect
    {|
    hello
    Top level: (Failure fail)
    |}]
;;

let%expect_test "ws raise2" =
  run Work_stealing ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun _ ->
          match (failwith "fail" : nothing) with
          | _ -> .)
        (fun _ ->
          match (failwith "fail" : nothing) with
          | _ -> .)
    in
    ());
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "ws nested raise" =
  run Work_stealing ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun parallel ->
          let #((), ()) =
            Parallel.fork_join2
              parallel
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
          in
          ())
        (fun parallel ->
          let #((), ()) = Parallel.fork_join2 parallel (fun _ -> ()) (fun _ -> ()) in
          ())
    in
    ());
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "ws raise4" =
  run Work_stealing ~f:(fun parallel ->
    let #(_, _) =
      Parallel.fork_join2
        parallel
        (fun parallel ->
          let #((), ()) =
            Parallel.fork_join2
              parallel
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
          in
          ())
        (fun parallel ->
          let #((), ()) =
            Parallel.fork_join2
              parallel
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
              (fun _ ->
                match (failwith "fail" : nothing) with
                | _ -> .)
          in
          ())
    in
    ());
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "array init" =
  let open Parallel.Arrays in
  run Seq ~f:(fun parallel ->
    let _ : _ = Array.init parallel 1 ~f:(fun _ -> failwith "fail") in
    ());
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "array map" =
  let open Parallel.Arrays in
  run Seq ~f:(fun parallel ->
    let array = Array.init parallel 1 ~f:(fun _ _ -> 0) in
    let _ : _ = Array.map parallel array ~f:(fun _ _ -> failwith "fail") in
    ());
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent" =
  run_concurrent ~f:(fun _concurrent -> failwith "fail");
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent scope" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun _spawn -> failwith "fail"));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent task" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope _parallel _concurrent -> failwith "fail")));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent task terminated" =
  let wait = Await.Ivar.create () in
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope _parallel concurrent ->
        Await.Ivar.read (Concurrent.await concurrent) wait [@nontail];
        failwith "unreachable");
      Concurrent.spawn spawn ~f:(fun _scope _parallel _concurrent -> failwith "fail")));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent parallel task terminated" =
  let wait : unit Await.Ivar.t = Await.Ivar.create () in
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope parallel concurrent ->
        let #((), ()) =
          Parallel.Biased.fork_join2
            parallel
            (fun _ -> Await.Ivar.read (Concurrent.await concurrent) wait [@nontail])
            (fun _ -> ())
        in
        ());
      Concurrent.spawn spawn ~f:(fun _scope _parallel _concurrent -> failwith "fail")));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent scheduler terminated" =
  let wait = Await.Ivar.create () in
  run_concurrent ~f:(fun concurrent ->
    let scheduler = Concurrent.scheduler concurrent in
    Await.Scope.with_ (Concurrent.await concurrent) () ~f:(fun _ scope ->
      Concurrent.Scheduler.spawn
        scheduler
        scope
        (Concurrent.task (fun _scope _parallel concurrent ->
           Await.Ivar.read (Concurrent.await concurrent) wait;
           failwith "unreachable"));
      Concurrent.Scheduler.spawn
        scheduler
        scope
        (Concurrent.task (fun _scope _parallel _concurrent -> failwith "fail")) [@nontail])
    [@nontail]);
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent parallel task" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope parallel _concurrent ->
        let #((), ()) =
          Parallel.fork_join2
            parallel
            (fun _ -> failwith "fail")
            (fun _ -> failwith "fail")
        in
        ())));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent into scope" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun scope _parallel concurrent ->
        let spawn = Concurrent.into_scope concurrent scope in
        Concurrent.spawn spawn ~f:(fun _ -> failwith "fail") [@nontail])));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent nested scope" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope _parallel concurrent ->
        Concurrent.with_scope concurrent () ~f:(fun spawn ->
          Concurrent.spawn spawn ~f:(fun _ -> failwith "fail") [@nontail]))));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent parallel nested" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun _scope parallel concurrent ->
        let #((), ()) =
          Parallel.Biased.fork_join2
            parallel
            (fun _ ->
              Concurrent.with_scope concurrent () ~f:(fun spawn ->
                Concurrent.spawn spawn ~f:(fun _scope parallel _concurrent ->
                  let #((), ()) =
                    Parallel.fork_join2
                      parallel
                      (fun _ -> failwith "fail")
                      (fun _ -> failwith "fail")
                  in
                  ())))
            (fun _ -> failwith "fail")
        in
        ())));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent parallel into_scope" =
  run_concurrent ~f:(fun concurrent ->
    Concurrent.with_scope concurrent () ~f:(fun spawn ->
      Concurrent.spawn spawn ~f:(fun scope parallel concurrent ->
        let #((), ()) =
          Parallel.Biased.fork_join2
            parallel
            (fun _ ->
              let spawn = Concurrent.into_scope concurrent scope in
              Concurrent.spawn spawn ~f:(fun _scope _parallel _concurrent ->
                failwith "fail")
              [@nontail])
            (fun _ -> ())
        in
        ())));
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent scheduler" =
  run_concurrent ~f:(fun concurrent ->
    let scheduler = Concurrent.scheduler concurrent in
    Await.Scope.with_ (Concurrent.await concurrent) () ~f:(fun _ scope ->
      Concurrent.Scheduler.spawn
        scheduler
        scope
        (Concurrent.task (fun _scope _parallel _concurrent -> failwith "fail")))
    [@nontail]);
  [%expect {| Top level: (Failure fail) |}]
;;

let%expect_test "concurrent scheduler parallel" =
  run_concurrent ~f:(fun concurrent ->
    let scheduler = Concurrent.scheduler concurrent in
    Await.Scope.with_ (Concurrent.await concurrent) () ~f:(fun _ scope ->
      Concurrent.Scheduler.spawn
        scheduler
        scope
        (Concurrent.task (fun _scope parallel _concurrent ->
           let #((), ()) =
             Parallel.fork_join2
               parallel
               (fun _ -> failwith "fail")
               (fun _ -> failwith "fail")
           in
           ())))
    [@nontail]);
  (* inner panics are reported to the monitor *)
  [%expect {| Top level: (Failure fail) |}]
;;