@@ -43,81 +43,88 @@ let test_trigger_contract () =
43
43
44
44
(* * This tries to cover much of the public contract of [Computation]s. *)
45
45
let test_computation_contract () =
46
- let attached_total = ref 0 and unattached_total = ref 0 in
47
- let () =
48
- Atomic. trace @@ fun () ->
49
- let computation = Computation. create () in
50
- let returns = ref 0 and cancels = ref 0 in
51
- let () =
52
- Atomic. spawn @@ fun () ->
53
- if Computation. try_return computation 101 then incr returns
54
- in
55
- let () =
56
- Atomic. spawn @@ fun () ->
57
- if Computation. try_cancel computation (Exn_bt. get_callstack 1 Exit ) then
58
- incr cancels
59
- in
60
- let triggers = Array. init 2 @@ fun _ -> Trigger. create () in
61
- let attached = ref 0 and unattached = ref 0 in
62
- let () =
63
- triggers
64
- |> Array. iter @@ fun trigger ->
46
+ [ `FIFO ; `LIFO ]
47
+ |> List. iter @@ fun mode ->
48
+ let attached_total = ref 0 and unattached_total = ref 0 in
49
+ let () =
50
+ Atomic. trace @@ fun () ->
51
+ let computation = Computation. create ~mode () in
52
+ let returns = ref 0 and cancels = ref 0 in
53
+ let () =
65
54
Atomic. spawn @@ fun () ->
66
- if Computation. try_attach computation trigger then incr attached
67
- else incr unattached
68
- in
69
- Atomic. final @@ fun () ->
70
- Atomic. check @@ fun () ->
71
- attached_total += ! attached;
72
- unattached_total += ! unattached;
73
- begin
74
- match Computation. peek computation with
75
- | Some (Ok 101 ) when ! returns = 1 && ! cancels = 0 -> true
76
- | Some (Error { exn = Exit ; _ } ) when ! returns = 0 && ! cancels = 1 -> true
77
- | _ -> false
78
- end
79
- && ! attached + ! unattached = Array. length triggers
80
- && ! attached
81
- = sum_as
82
- (fun trigger -> Bool. to_int (Trigger. is_signaled trigger))
83
- triggers
84
- in
85
- [ attached_total; unattached_total ]
86
- |> List. iter @@ fun total -> if ! total = 0 then Alcotest. fail " uncovered case"
55
+ if Computation. try_return computation 101 then incr returns
56
+ in
57
+ let () =
58
+ Atomic. spawn @@ fun () ->
59
+ if Computation. try_cancel computation (Exn_bt. get_callstack 1 Exit )
60
+ then incr cancels
61
+ in
62
+ let triggers = Array. init 2 @@ fun _ -> Trigger. create () in
63
+ let attached = ref 0 and unattached = ref 0 in
64
+ let () =
65
+ triggers
66
+ |> Array. iter @@ fun trigger ->
67
+ Atomic. spawn @@ fun () ->
68
+ if Computation. try_attach computation trigger then incr attached
69
+ else incr unattached
70
+ in
71
+ Atomic. final @@ fun () ->
72
+ Atomic. check @@ fun () ->
73
+ attached_total += ! attached;
74
+ unattached_total += ! unattached;
75
+ begin
76
+ match Computation. peek computation with
77
+ | Some (Ok 101 ) when ! returns = 1 && ! cancels = 0 -> true
78
+ | Some (Error { exn = Exit ; _ } ) when ! returns = 0 && ! cancels = 1 ->
79
+ true
80
+ | _ -> false
81
+ end
82
+ && ! attached + ! unattached = Array. length triggers
83
+ && ! attached
84
+ = sum_as
85
+ (fun trigger -> Bool. to_int (Trigger. is_signaled trigger))
86
+ triggers
87
+ in
88
+ [ attached_total; unattached_total ]
89
+ |> List. iter @@ fun total ->
90
+ if ! total = 0 then Alcotest. fail " uncovered case"
87
91
88
92
(* * This covers the contract of [Computation] to remove detached triggers.
89
93
90
94
Testing this through the public API would require relying on GC
91
95
statistics. *)
92
96
let test_computation_removes_triggers () =
93
- Atomic. trace @@ fun () ->
94
- let computation = Computation. create () in
95
- let triggers = Array. init 4 @@ fun _ -> Trigger. create () in
96
- let () =
97
- triggers
98
- |> Array. iter @@ fun trigger ->
99
- Atomic. spawn @@ fun () ->
100
- Atomic. check (fun () -> Computation. try_attach computation trigger);
101
- Computation. detach computation trigger
102
- in
103
- Atomic. final @@ fun () ->
104
- Atomic. check @@ fun () ->
105
- Array. for_all Trigger. is_signaled triggers
106
- &&
107
- match Atomic. get computation with
108
- | Canceled _ | Returned _ -> false
109
- | Continue { balance; triggers } ->
110
- balance < = 0
111
- && List. length triggers < = 2
112
- &&
113
- let trigger = Trigger. create () in
114
- Computation. try_attach computation trigger
115
- && begin
116
- match Atomic. get computation with
117
- | Canceled _ | Returned _ -> false
118
- | Continue { balance; triggers } ->
119
- balance = 1 && triggers = [ trigger ]
120
- end
97
+ [ `FIFO ; `LIFO ]
98
+ |> List. iter @@ fun mode ->
99
+ Atomic. trace @@ fun () ->
100
+ let computation = Computation. create ~mode () in
101
+ let triggers = Array. init 4 @@ fun _ -> Trigger. create () in
102
+ let () =
103
+ triggers
104
+ |> Array. iter @@ fun trigger ->
105
+ Atomic. spawn @@ fun () ->
106
+ Atomic. check (fun () -> Computation. try_attach computation trigger);
107
+ Computation. detach computation trigger
108
+ in
109
+ Atomic. final @@ fun () ->
110
+ Atomic. check @@ fun () ->
111
+ Array. for_all Trigger. is_signaled triggers
112
+ &&
113
+ match Atomic. get computation with
114
+ | Canceled _ | Returned _ -> false
115
+ | Continue { balance_and_mode; triggers } ->
116
+ balance_and_mode < = Computation. fifo_bit
117
+ && List. length triggers < = 2
118
+ &&
119
+ let trigger = Trigger. create () in
120
+ Computation. try_attach computation trigger
121
+ && begin
122
+ match Atomic. get computation with
123
+ | Canceled _ | Returned _ -> false
124
+ | Continue { balance_and_mode; triggers } ->
125
+ balance_and_mode < = Computation. one + Computation. fifo_bit
126
+ && triggers = [ trigger ]
127
+ end
121
128
122
129
let () =
123
130
Alcotest. run " Picos DSCheck"
0 commit comments