1
+ import gleam/dynamic
2
+ import gleam/erlang/atom
1
3
import gleam/erlang/process . { type Pid }
2
- import gleam/otp/task . { Timeout }
4
+ import gleam/function
5
+ import gleam/io
6
+ import gleam/otp/task . { Exit , Timeout }
7
+ import gleam/string
3
8
import gleeunit/should
4
9
5
10
@ external ( erlang , "gleam_otp_test_external" , "flush" )
@@ -117,6 +122,20 @@ pub fn try_await2_test() {
117
122
118
123
task . try_await2 ( task1 , task2 , 8 )
119
124
|> should . equal ( # ( Ok ( 1 ) , Ok ( 2 ) ) )
125
+
126
+ // We want to make sure timers don't leak!
127
+ assert_no_leftover_messages ( )
128
+ }
129
+
130
+ fn assert_no_leftover_messages ( ) -> Nil {
131
+ let selector =
132
+ process . new_selector ( )
133
+ |> process . selecting_anything ( function . identity )
134
+
135
+ case process . select ( selector , 20 ) {
136
+ Error ( Nil ) -> Nil
137
+ Ok ( _ ) -> panic as "leftover message"
138
+ }
120
139
}
121
140
122
141
pub fn try_await2_timeout_test ( ) {
@@ -136,6 +155,10 @@ pub fn try_await2_timeout_test() {
136
155
137
156
task . try_await2 ( task1 , task2 , 5 )
138
157
|> should . equal ( # ( Ok ( 1 ) , Error ( Timeout ) ) )
158
+
159
+ // We don't want task messages to leak so we still have to wait for the ones
160
+ // that timed out earlier!
161
+ task . await_forever ( task2 )
139
162
}
140
163
141
164
pub fn try_await3_test ( ) {
@@ -155,6 +178,9 @@ pub fn try_await3_test() {
155
178
156
179
task . try_await3 ( task1 , task2 , task3 , 8 )
157
180
|> should . equal ( # ( Ok ( 1 ) , Ok ( 2 ) , Ok ( 3 ) ) )
181
+
182
+ // We want to make sure timers don't leak!
183
+ assert_no_leftover_messages ( )
158
184
}
159
185
160
186
pub fn try_await3_timeout_test ( ) {
@@ -175,6 +201,10 @@ pub fn try_await3_timeout_test() {
175
201
176
202
task . try_await3 ( task1 , task2 , task3 , 20 )
177
203
|> should . equal ( # ( Error ( Timeout ) , Ok ( 2 ) , Ok ( 3 ) ) )
204
+
205
+ // We don't want task messages to leak so we still have to wait for the ones
206
+ // that timed out earlier!
207
+ task . await_forever ( task1 )
178
208
}
179
209
180
210
pub fn try_await4_test ( ) {
@@ -195,6 +225,9 @@ pub fn try_await4_test() {
195
225
196
226
task . try_await4 ( task1 , task2 , task3 , task4 , 8 )
197
227
|> should . equal ( # ( Ok ( 1 ) , Ok ( 2 ) , Ok ( 3 ) , Ok ( 4 ) ) )
228
+
229
+ // We want to make sure timers don't leak!
230
+ assert_no_leftover_messages ( )
198
231
}
199
232
200
233
pub fn try_await4_timeout_test ( ) {
@@ -216,6 +249,10 @@ pub fn try_await4_timeout_test() {
216
249
217
250
task . try_await4 ( task1 , task2 , task3 , task4 , 20 )
218
251
|> should . equal ( # ( Error ( Timeout ) , Ok ( 2 ) , Ok ( 3 ) , Ok ( 4 ) ) )
252
+
253
+ // We don't want task messages to leak so we still have to wait for the ones
254
+ // that timed out earlier!
255
+ task . await_forever ( task1 )
219
256
}
220
257
221
258
pub fn try_await_all_test ( ) {
@@ -236,6 +273,9 @@ pub fn try_await_all_test() {
236
273
237
274
task . try_await_all ( [ task1 , task2 , task3 , task4 ] , 8 )
238
275
|> should . equal ( [ Ok ( 1 ) , Ok ( 2 ) , Ok ( 3 ) , Ok ( 4 ) ] )
276
+
277
+ // We want to make sure timers don't leak!
278
+ assert_no_leftover_messages ( )
239
279
}
240
280
241
281
pub fn try_await_all_timeout_test ( ) {
@@ -252,10 +292,15 @@ pub fn try_await_all_timeout_test() {
252
292
// 3 and 5 will not finish in time
253
293
let task1 = task . async ( work ( 1 , 1 ) )
254
294
let task2 = task . async ( work ( 2 , 1 ) )
255
- let task3 = task . async ( work ( 3 , 100 ) )
295
+ let task3 = task . async ( work ( 3 , 50 ) )
256
296
let task4 = task . async ( work ( 4 , 1 ) )
257
- let task5 = task . async ( work ( 5 , 50 ) )
297
+ let task5 = task . async ( work ( 5 , 100 ) )
258
298
259
299
task . try_await_all ( [ task1 , task2 , task3 , task4 , task5 ] , 20 )
260
300
|> should . equal ( [ Ok ( 1 ) , Ok ( 2 ) , Error ( Timeout ) , Ok ( 4 ) , Error ( Timeout ) ] )
301
+
302
+ // We don't want task messages to leak so we still have to wait for the ones
303
+ // that timed out earlier!
304
+ task . await_forever ( task3 )
305
+ task . await_forever ( task5 )
261
306
}
0 commit comments