Skip to content

Commit e88a93b

Browse files
TESTS: Add value() tests for cancel()
1 parent 3336246 commit e88a93b

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: future.tests
22
Title: Test Suite for 'Future API' Backends
3-
Version: 0.9.0-9004
3+
Version: 0.9.0-9005
44
Authors@R: c(
55
person("Henrik", "Bengtsson", role = c("aut", "cre", "cph"), email = "[email protected]"),
66
person(family = "The R Consortium", comment = "Project was awarded an Infrastructure Steering Committee (ISC) grant in 2017", role = "fnd"))

inst/test-db/cancel.R

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
make_test(title = "cancel()", args = list(lazy = c(FALSE, TRUE), interrupt = c(FALSE, TRUE)), tags = c("cancel"), {
22
f0 <- future(NULL)
33
v <- value(f0)
4+
print(class(f0))
5+
6+
delay <- if (interrupt) 0.0 else 0.5
7+
f <- future({
8+
Sys.sleep(delay)
9+
42L
10+
}, lazy = lazy)
11+
12+
cat(sprintf("state = %s\n", sQuote(f[["state"]])))
13+
if (lazy) {
14+
stopifnot(f[["state"]] %in% "created")
15+
} else {
16+
stopifnot(f[["state"]] %in% c("submitted", "running", "finished"))
17+
}
18+
19+
cat(sprintf("interrupt = %s\n", sQuote(interrupt)))
20+
f <- cancel(f, interrupt = interrupt)
21+
cat(sprintf("state = %s\n", sQuote(f[["state"]])))
22+
if (lazy) {
23+
stopifnot(f[["state"]] %in% "created")
24+
} else {
25+
stopifnot(f[["state"]] %in% c("canceled", "finished"))
26+
}
27+
28+
## A canceled future may be resolved or not, which
29+
## depends on future been interrupted or not
30+
r <- resolved(f)
31+
cat(sprintf("resolved = %s\n", sQuote(r)))
32+
33+
v <- tryCatch({
34+
value(f)
35+
}, FutureInterruptError = identity)
36+
print(v)
37+
})
38+
39+
40+
make_test(title = "cancel() and value()", args = list(lazy = c(FALSE, TRUE), interrupt = c(FALSE, TRUE)), tags = c("cancel", "value", "devel"), {
41+
f0 <- future(NULL)
42+
v <- value(f0)
43+
print(class(f0))
444

45+
delay <- if (interrupt) 0.0 else 0.5
546
f <- future({
6-
Sys.sleep(0.5)
7-
list(a = 1, b = 42L)
47+
Sys.sleep(delay)
48+
42L
849
}, lazy = lazy)
950

1051
cat(sprintf("state = %s\n", sQuote(f[["state"]])))
@@ -27,4 +68,31 @@ make_test(title = "cancel()", args = list(lazy = c(FALSE, TRUE), interrupt = c(F
2768
## depends on future been interrupted or not
2869
r <- resolved(f)
2970
cat(sprintf("resolved = %s\n", sQuote(r)))
71+
72+
v <- tryCatch({
73+
value(f)
74+
}, FutureInterruptError = identity)
75+
print(v)
76+
77+
## Did we require an interrupt and does the backend support it?
78+
interrupt2 <- interrupt && plan("backend")[["interrupts"]]
79+
if (interrupt2) {
80+
if (lazy || inherits(f0, c("UniprocessFuture", "BatchtoolsUniprocessFuture"))) {
81+
stopifnot(
82+
!inherits(v, "FutureInterruptError"),
83+
v == 42L
84+
)
85+
} else {
86+
stopifnot(inherits(v, "FutureInterruptError"))
87+
}
88+
} else {
89+
if (lazy) {
90+
stopifnot(!inherits(v, "FutureInterruptError"))
91+
} else {
92+
stopifnot(
93+
!inherits(v, "FutureInterruptError"),
94+
v == 42L
95+
)
96+
}
97+
}
3098
})

0 commit comments

Comments
 (0)