Skip to content

Commit

Permalink
Add test for async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Oct 31, 2024
1 parent 01f2ca2 commit 850fbca
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/testthat/test-async.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ test_that("for loops support await_each()", {
}))
})

test_that("Iterators are cleaned up from most nested to least nested", {
called <- NULL

g1 <- coro::async_generator(function() {
on.exit(called <<- c(called, "g1"))
yield(1)
yield(2)
})
g2 <- coro::async_generator(function() {
on.exit(called <<- c(called, "g2"))
yield(1)
yield(2)
})

h <- coro::async_generator(function() {
on.exit(called <<- c(called, "h"))
for (i in await_each(g1())) {
for (j in await_each(g2())) {
yield(c(i, j))
}
stop("foo")
}
})

expect_error(
wait_for(async_collect(h()))
)
expect_equal(called, c("g2", "g1", "h"))
})

test_that("await_each() can't be used in generators", {
expect_error(generator(function() for (x in await_each(i)) NULL)()(), "non-async generator")
})
Expand Down

0 comments on commit 850fbca

Please sign in to comment.