From 850fbca119effcad011bb0fda018f9e6654a54c1 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Thu, 31 Oct 2024 12:15:08 +0100 Subject: [PATCH] Add test for async generators --- tests/testthat/test-async.R | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/testthat/test-async.R b/tests/testthat/test-async.R index 5765688..9ad4f74 100644 --- a/tests/testthat/test-async.R +++ b/tests/testthat/test-async.R @@ -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") })