Skip to content

Commit 76287cf

Browse files
committed
Add unit tests for run_now()
1 parent 8f24e2e commit 76287cf

File tree

6 files changed

+100
-3
lines changed

6 files changed

+100
-3
lines changed

.Rbuildignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
3-
.travis.yml
3+
.travis.yml
4+
^include$

DESCRIPTION

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Imports:
1818
LinkingTo: Rcpp, BH
1919
Roxygen: list(markdown = TRUE)
2020
RoxygenNote: 6.0.1
21-
Suggests: knitr,
22-
rmarkdown
21+
Suggests:
22+
knitr,
23+
rmarkdown,
24+
testthat
2325
VignetteBuilder: knitr

include

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inst/include

inst/bgtest.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <later_api.h>
2+
#include <unistd.h>
3+
4+
// Used in tests/testthat/test-run_now.R
5+
6+
class TestTask : public later::BackgroundTask {
7+
int _timeoutSecs;
8+
9+
public:
10+
TestTask(int timeoutSecs) : _timeoutSecs(timeoutSecs) {}
11+
12+
protected:
13+
// The task to be executed on the background thread.
14+
// Neither the R runtime nor any R data structures may be
15+
// touched from the background thread; any values that need
16+
// to be passed into or out of the Execute method must be
17+
// included as fields on the Task subclass object.
18+
void execute() {
19+
sleep(_timeoutSecs);
20+
}
21+
22+
// A short task that runs on the main R thread after the
23+
// background task has completed. It's safe to access the
24+
// R runtime and R data structures from here.
25+
void complete() {}
26+
};
27+
28+
29+
// [[Rcpp::depends(later)]]
30+
// [[Rcpp::export]]
31+
void launchBgTask(int secsToSleep) {
32+
(new TestTask(secsToSleep))->begin();
33+
}

tests/testthat.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See https://github.com/r-lib/testthat/issues/86
2+
Sys.setenv("R_TESTS" = "")
3+
4+
library(testthat)
5+
library(later)
6+
7+
test_check("later")

tests/testthat/test-run_now.R

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
context("test-run_now.R")
2+
3+
test_that("run_now waits and returns FALSE if no tasks", {
4+
x <- system.time({
5+
result <- later::run_now(0.5)
6+
})
7+
expect_gte(as.numeric(x[["elapsed"]]), 0.499)
8+
expect_identical(result, FALSE)
9+
10+
x <- system.time({
11+
result <- later::run_now(3)
12+
})
13+
expect_gte(as.numeric(x[["elapsed"]]), 2.999)
14+
expect_identical(result, FALSE)
15+
})
16+
17+
test_that("run_now returns immediately after executing a task", {
18+
x <- system.time({
19+
later::later(~{}, 0)
20+
result <- later::run_now(2)
21+
})
22+
expect_lt(as.numeric(x[["elapsed"]]), 0.25)
23+
expect_identical(result, TRUE)
24+
})
25+
26+
test_that("run_now executes all scheduled tasks, not just one", {
27+
later::later(~{}, 0)
28+
later::later(~{}, 0)
29+
result1 <- later::run_now()
30+
result2 <- later::run_now()
31+
expect_identical(result1, TRUE)
32+
expect_identical(result2, FALSE)
33+
})
34+
35+
test_that("run_now doesn't go past a failed task", {
36+
later::later(~stop("boom"), 0)
37+
later::later(~{}, 0)
38+
expect_error(later::run_now())
39+
expect_true(later::run_now())
40+
})
41+
42+
test_that("run_now wakes up when a background thread calls later()", {
43+
env <- new.env()
44+
Rcpp::sourceCpp(system.file("bgtest.cpp", package = "later"), env = env)
45+
# The background task sleeps
46+
env$launchBgTask(1)
47+
48+
x <- system.time({
49+
result <- later::run_now(3)
50+
})
51+
expect_lt(as.numeric(x[["elapsed"]]), 1.25)
52+
expect_true(result)
53+
})

0 commit comments

Comments
 (0)