File: test_lwt_switch.ml

package info (click to toggle)
lwt 5.9.2-3
  • 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 (180 lines) | stat: -rw-r--r-- 4,768 bytes parent folder | download | duplicates (4)
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
(* 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. *)



open Lwt.Infix
open Test

let suite = suite "lwt_switch" [
  test "turn_off, add_hook"
    (fun () ->
      let hook_1_calls = ref 0 in
      let hook_2_calls = ref 0 in

      let hook call_counter () =
        call_counter := !call_counter + 1;
        Lwt.return_unit
      in

      let switch = Lwt_switch.create () in
      Lwt_switch.add_hook (Some switch) (hook hook_1_calls);
      Lwt_switch.add_hook (Some switch) (hook hook_2_calls);

      let check_1 = !hook_1_calls = 0 in
      let check_2 = !hook_2_calls = 0 in

      Lwt_switch.turn_off switch >>= fun () ->

      let check_3 = !hook_1_calls = 1 in
      let check_4 = !hook_2_calls = 1 in

      Lwt_switch.turn_off switch >|= fun () ->

      let check_5 = !hook_1_calls = 1 in
      let check_6 = !hook_2_calls = 1 in

      let check_7 =
        try
          Lwt_switch.add_hook (Some switch) (fun () -> Lwt.return_unit);
          false
        with Lwt_switch.Off ->
          true
      in

      check_1 && check_2 && check_3 && check_4 && check_5 && check_6 &&
        check_7);

  test "turn_off: hook exception"
    (fun () ->
      let hook () = raise Exit in

      let switch = Lwt_switch.create () in
      Lwt_switch.add_hook (Some switch) hook;

      Lwt.catch
        (fun () -> Lwt_switch.turn_off switch >|= fun () -> false)
        (function
        | Exit -> Lwt.return_true
        | _ -> Lwt.return_false));

  test "with_switch: regular exit"
    (fun () ->
      let hook_called = ref false in

      Lwt_switch.with_switch (fun switch ->
        Lwt_switch.add_hook (Some switch) (fun () ->
          hook_called := true;
          Lwt.return_unit);

        Lwt.return_unit)

      >|= fun () -> !hook_called);

  test "with_switch: exception"
    (fun () ->
      let hook_called = ref false in
      let exception_caught = ref false in

      Lwt.catch
        (fun () ->
          Lwt_switch.with_switch (fun switch ->
            Lwt_switch.add_hook (Some switch) (fun () ->
              hook_called := true;
              Lwt.return_unit);

            raise Exit))
        (function
        | Exit ->
          exception_caught := true;
          Lwt.return_unit
        | _ ->
          Lwt.return_unit)

      >|= fun () -> !hook_called && !exception_caught);

  test "check"
    (fun () ->
      Lwt_switch.check None;

      let switch = Lwt_switch.create () in
      Lwt_switch.check (Some switch);

      Lwt_switch.turn_off switch >|= fun () ->
      try Lwt_switch.check (Some switch); false
      with Lwt_switch.Off -> true);

  test "is_on"
    (fun () ->
      let switch = Lwt_switch.create () in
      let check_1 = Lwt_switch.is_on switch in
      Lwt_switch.turn_off switch >|= fun () ->
      let check_2 = not (Lwt_switch.is_on switch) in
      check_1 && check_2);

  test "add_hook_or_exec"
    (fun () ->
      let hook_calls = ref 0 in

      let hook () =
        hook_calls := !hook_calls + 1;
        Lwt.return_unit
      in

      Lwt_switch.add_hook_or_exec None hook >>= fun () ->
      let check_1 = !hook_calls = 0 in

      let switch = Lwt_switch.create () in
      Lwt_switch.add_hook_or_exec (Some switch) hook >>= fun () ->
      let check_2 = !hook_calls = 0 in

      Lwt_switch.turn_off switch >>= fun () ->
      let check_3 = !hook_calls = 1 in

      Lwt_switch.add_hook_or_exec (Some switch) hook >|= fun () ->
      let check_4 = !hook_calls = 2 in

      check_1 && check_2 && check_3 && check_4);

  test "turn_off waits for hooks: regular exit"
    (fun () ->
      let hooks_finished = ref 0 in

      let hook () =
        Lwt.pause () >>= fun () ->
        hooks_finished := !hooks_finished + 1;
        Lwt.return_unit
      in

      let switch = Lwt_switch.create () in
      Lwt_switch.add_hook (Some switch) hook;
      Lwt_switch.add_hook (Some switch) hook;

      Lwt_switch.turn_off switch >|= fun () ->
      !hooks_finished = 2);

  test "turn_off waits for hooks: hook exception"
    (fun () ->
      let hooks_finished = ref 0 in

      let successful_hook () =
        Lwt.pause () >>= fun () ->
        hooks_finished := !hooks_finished + 1;
        Lwt.return_unit
      in

      let failing_hook () =
        hooks_finished := !hooks_finished + 1;
        raise Exit
      in

      let switch = Lwt_switch.create () in
      Lwt_switch.add_hook (Some switch) successful_hook;
      Lwt_switch.add_hook (Some switch) failing_hook;
      Lwt_switch.add_hook (Some switch) successful_hook;

      Lwt.catch
        (fun () -> Lwt_switch.turn_off switch)
        (fun _ -> Lwt.return_unit) >|= fun () ->
      !hooks_finished = 3);
]