Skip to content

Commit

Permalink
Make later_fd() tests more robust (#202)
Browse files Browse the repository at this point in the history
* make later_fd() tests more robust

* use blocking run_now()
  • Loading branch information
shikokuchuo authored Nov 27, 2024
1 parent 50aebba commit d82ec07
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions tests/testthat/test-later-fd.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ test_that("later_fd", {

# timeout
later_fd(callback, c(fd1, fd2), timeout = 0)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(FALSE, FALSE))
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(FALSE, FALSE, FALSE, FALSE))

# cancellation
Expand All @@ -35,57 +33,49 @@ test_that("later_fd", {

# timeout (> 1 loop)
later_fd(callback, c(fd1, fd2), timeout = 1.1)
Sys.sleep(1.25)
run_now()
run_now(1.3)
expect_equal(result, c(FALSE, FALSE))

# fd1 ready
later_fd(callback, c(fd1, fd2), timeout = 0.9)
res <- nanonext::send(s2, "msg")
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(TRUE, FALSE))

# both fd1, fd2 ready
res <- nanonext::send(s1, "msg")
Sys.sleep(0.1)
later_fd(callback, c(fd1, fd2), timeout = 1)
Sys.sleep(0.1)
run_now()
run_now(1)
expect_equal(result, c(TRUE, TRUE))

# no exceptions
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = -0.1)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(TRUE, TRUE, FALSE, FALSE))

# fd2 ready
res <- nanonext::recv(s1)
later_fd(callback, c(fd1, fd2), timeout = 1L)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(FALSE, TRUE))

# fd2 invalid
res <- nanonext::recv(s2)
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0.1)
close(s2)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_length(result, 4L)

# both fd1, fd2 invalid
close(s1)
later_fd(callback, c(fd1, fd2), c(fd1, fd2), timeout = 0)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, c(NA, NA, NA, NA))

# no fds supplied
later_fd(callback, timeout = -1)
Sys.sleep(0.2)
run_now()
run_now(1)
expect_equal(result, logical())

on.exit()
Expand All @@ -107,12 +97,12 @@ test_that("loop_empty() reflects later_fd callbacks", {
cancel <- later_fd(~{}, fd1)
expect_false(loop_empty())
cancel()
Sys.sleep(1.2) # check for cancellation happens every ~1 sec
Sys.sleep(1.25) # check for cancellation happens every ~1 sec
expect_true(loop_empty())

later_fd(~{}, fd1, timeout = 0)
expect_false(loop_empty())
run_now()
run_now(1)
expect_true(loop_empty())

})
Expand Down

0 comments on commit d82ec07

Please sign in to comment.