tests/async/test-amap.R

test_that("async_map", {
  list <- structure(as.list(1:10), names = letters[1:10])

  fun <- async(function(x) {
    force(x)
    delay(1 / 100)$then(function(value) x * 2)
  })

  result <- synchronise(async_map(list, fun))
  expect_identical(result, as.list(unlist(list) * 2))
})

test_that("async_map with limit", {
  list <- structure(as.list(1:10), names = letters[1:10])
  fun <- function(x) {
    force(x)
    delay(1 / 10000)$then(function(value) x * 2)
  }

  for (l in 1:10) {
    result <- synchronise(async_map(list, fun, .limit = l))
    expect_identical(result, as.list(unlist(list) * 2))
  }
})

test_that("async_map with limit, error", {
  testthat::local_edition(3)
  list <- structure(as.list(1:10), names = letters[1:10])
  fun <- async(function(x) {
    force(x)
    delay(1 / 10000)$then(function() if (x == 7) stop("oops") else x * 2)
  })

  for (l in c(1:10, Inf)) {
    expect_snapshot(
      error = TRUE,
      synchronise(async_map(list, fun, .limit = l))
    )
  }
})

Try the pkgcache package in your browser

Any scripts or data that you put into this service are public.

pkgcache documentation built on June 8, 2025, 10:49 a.m.