Skip to content

Commit d82ec07

Browse files
authored
Make later_fd() tests more robust (#202)
* make later_fd() tests more robust * use blocking run_now()
1 parent 50aebba commit d82ec07

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

tests/testthat/test-later-fd.R

+12-22
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ test_that("later_fd", {
1414

1515
# timeout
1616
later_fd(callback, c(fd1, fd2), timeout = 0)
17-
Sys.sleep(0.2)
18-
run_now()
17+
run_now(1)
1918
expect_equal(result, c(FALSE, FALSE))
2019
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0)
21-
Sys.sleep(0.2)
22-
run_now()
20+
run_now(1)
2321
expect_equal(result, c(FALSE, FALSE, FALSE, FALSE))
2422

2523
# cancellation
@@ -35,57 +33,49 @@ test_that("later_fd", {
3533

3634
# timeout (> 1 loop)
3735
later_fd(callback, c(fd1, fd2), timeout = 1.1)
38-
Sys.sleep(1.25)
39-
run_now()
36+
run_now(1.3)
4037
expect_equal(result, c(FALSE, FALSE))
4138

4239
# fd1 ready
4340
later_fd(callback, c(fd1, fd2), timeout = 0.9)
4441
res <- nanonext::send(s2, "msg")
45-
Sys.sleep(0.2)
46-
run_now()
42+
run_now(1)
4743
expect_equal(result, c(TRUE, FALSE))
4844

4945
# both fd1, fd2 ready
5046
res <- nanonext::send(s1, "msg")
5147
Sys.sleep(0.1)
5248
later_fd(callback, c(fd1, fd2), timeout = 1)
53-
Sys.sleep(0.1)
54-
run_now()
49+
run_now(1)
5550
expect_equal(result, c(TRUE, TRUE))
5651

5752
# no exceptions
5853
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = -0.1)
59-
Sys.sleep(0.2)
60-
run_now()
54+
run_now(1)
6155
expect_equal(result, c(TRUE, TRUE, FALSE, FALSE))
6256

6357
# fd2 ready
6458
res <- nanonext::recv(s1)
6559
later_fd(callback, c(fd1, fd2), timeout = 1L)
66-
Sys.sleep(0.2)
67-
run_now()
60+
run_now(1)
6861
expect_equal(result, c(FALSE, TRUE))
6962

7063
# fd2 invalid
7164
res <- nanonext::recv(s2)
7265
later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0.1)
7366
close(s2)
74-
Sys.sleep(0.2)
75-
run_now()
67+
run_now(1)
7668
expect_length(result, 4L)
7769

7870
# both fd1, fd2 invalid
7971
close(s1)
8072
later_fd(callback, c(fd1, fd2), c(fd1, fd2), timeout = 0)
81-
Sys.sleep(0.2)
82-
run_now()
73+
run_now(1)
8374
expect_equal(result, c(NA, NA, NA, NA))
8475

8576
# no fds supplied
8677
later_fd(callback, timeout = -1)
87-
Sys.sleep(0.2)
88-
run_now()
78+
run_now(1)
8979
expect_equal(result, logical())
9080

9181
on.exit()
@@ -107,12 +97,12 @@ test_that("loop_empty() reflects later_fd callbacks", {
10797
cancel <- later_fd(~{}, fd1)
10898
expect_false(loop_empty())
10999
cancel()
110-
Sys.sleep(1.2) # check for cancellation happens every ~1 sec
100+
Sys.sleep(1.25) # check for cancellation happens every ~1 sec
111101
expect_true(loop_empty())
112102

113103
later_fd(~{}, fd1, timeout = 0)
114104
expect_false(loop_empty())
115-
run_now()
105+
run_now(1)
116106
expect_true(loop_empty())
117107

118108
})

0 commit comments

Comments
 (0)