Skip to content

Commit b418a77

Browse files
giacomocavalierilpil
authored andcommitted
fix tests
1 parent 2b92d24 commit b418a77

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/gleam/otp/task.gleam

+6-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,12 @@ pub fn try_await_all(
410410
try_await_all_loop(dict.new(), tasks_count, timer, selector)
411411
}
412412

413-
fn try_await_all_loop(values, tasks_count, timer, selector) {
413+
fn try_await_all_loop(
414+
values: dict.Dict(Int, Result(b, AwaitError)),
415+
tasks_count: Int,
416+
timer: process.Timer,
417+
selector: Selector(Message(b)),
418+
) -> List(Result(b, AwaitError)) {
414419
case dict.size(values) == tasks_count {
415420
// If there's no more values to receive then we can return the list...
416421
True -> {

test/gleam/otp/task_test.gleam

+48-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import gleam/dynamic
2+
import gleam/erlang/atom
13
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
38
import gleeunit/should
49

510
@external(erlang, "gleam_otp_test_external", "flush")
@@ -117,6 +122,20 @@ pub fn try_await2_test() {
117122

118123
task.try_await2(task1, task2, 8)
119124
|> 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+
}
120139
}
121140

122141
pub fn try_await2_timeout_test() {
@@ -136,6 +155,10 @@ pub fn try_await2_timeout_test() {
136155

137156
task.try_await2(task1, task2, 5)
138157
|> 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)
139162
}
140163

141164
pub fn try_await3_test() {
@@ -155,6 +178,9 @@ pub fn try_await3_test() {
155178

156179
task.try_await3(task1, task2, task3, 8)
157180
|> should.equal(#(Ok(1), Ok(2), Ok(3)))
181+
182+
// We want to make sure timers don't leak!
183+
assert_no_leftover_messages()
158184
}
159185

160186
pub fn try_await3_timeout_test() {
@@ -175,6 +201,10 @@ pub fn try_await3_timeout_test() {
175201

176202
task.try_await3(task1, task2, task3, 20)
177203
|> 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)
178208
}
179209

180210
pub fn try_await4_test() {
@@ -195,6 +225,9 @@ pub fn try_await4_test() {
195225

196226
task.try_await4(task1, task2, task3, task4, 8)
197227
|> 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()
198231
}
199232

200233
pub fn try_await4_timeout_test() {
@@ -216,6 +249,10 @@ pub fn try_await4_timeout_test() {
216249

217250
task.try_await4(task1, task2, task3, task4, 20)
218251
|> 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)
219256
}
220257

221258
pub fn try_await_all_test() {
@@ -236,6 +273,9 @@ pub fn try_await_all_test() {
236273

237274
task.try_await_all([task1, task2, task3, task4], 8)
238275
|> 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()
239279
}
240280

241281
pub fn try_await_all_timeout_test() {
@@ -252,10 +292,15 @@ pub fn try_await_all_timeout_test() {
252292
// 3 and 5 will not finish in time
253293
let task1 = task.async(work(1, 1))
254294
let task2 = task.async(work(2, 1))
255-
let task3 = task.async(work(3, 100))
295+
let task3 = task.async(work(3, 50))
256296
let task4 = task.async(work(4, 1))
257-
let task5 = task.async(work(5, 50))
297+
let task5 = task.async(work(5, 100))
258298

259299
task.try_await_all([task1, task2, task3, task4, task5], 20)
260300
|> 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)
261306
}

0 commit comments

Comments
 (0)