-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_simple.jl
105 lines (87 loc) · 2.61 KB
/
test_simple.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module TestSimple
include("preamble.jl")
# Test that task group are synchronized and the task context can be
# passed around across threads/tasks.
@testset "wait" begin
results = SyncSeq()
bg(context, id) = @in context @go @go push!(results, id)
@taskgroup begin
@go bg(_, 1)
@go bg(_, 2)
@go bg(_, 3)
end
@test sort(collect(results)) == 1:3
end
# Test cancellation on error.
@testset "cancel on error" begin
results = SyncSeq()
function bg(context, id)
@in context begin
@go begin
push!(results, ("before sleep", id))
@await tickingfor(_, 1)
push!(results, ("after sleep", id))
end
if id == 1
@await ErrorException("terminate") # "throw" an error
end
end
end
@taskgroup begin
@go bg(_, 1)
@go bg(_, 2)
end
@test sort(collect(results)) == [("before sleep", 1), ("before sleep", 2)]
end
# Test manual cancellation
@testset "manual cancel" begin
results = SyncSeq()
function bg(context, id)
@in context begin
@go begin
push!(results, ("before sleep", id))
@await tickingfor(_, 1)
push!(results, ("after sleep", id))
end
if id == 1
cancel!(context)
end
end
end
@taskgroup begin
@go bg(_, 1)
@go bg(_, 2)
end
@test sort(collect(results)) == [("before sleep", 1), ("before sleep", 2)]
end
# Test that cancellation of `@cancelscope` does not leak out.
@testset "`@cancelscope`" begin
results = SyncSeq()
function bg(context, id)
@in context begin
subctx = @cancelscope begin
@go begin
push!(results, ("before sleep", id))
@await tickingfor(_, 1)
push!(results, ("after sleep", id))
end
end
if id == 1
# Manually cancel only this scope:
cancel!(subctx)
# Note: `@await ErrorException("terminate")` inside
# `@cancelscope` does not work because it cancels
# everything; i.e., the error bubbles up until the
# root `@taskgroup` which in turn cancels everything,
# not just this `@cancelscope`.
end
end
end
@taskgroup begin
@go bg(_, 1)
@go bg(_, 2)
end
@test sort(collect(results)) ==
[("after sleep", 2), ("before sleep", 1), ("before sleep", 2)]
end
end # module