File tree 4 files changed +41
-7
lines changed
4 files changed +41
-7
lines changed Original file line number Diff line number Diff line change 20
20
end
21
21
22
22
group :test do
23
- gem "sus" , "~> 0.29" , ">= 0.29.1 "
23
+ gem "sus" , "~> 0.31 "
24
24
gem "covered"
25
25
gem "decode"
26
26
gem "rubocop"
Original file line number Diff line number Diff line change @@ -15,17 +15,21 @@ module Kernel
15
15
#
16
16
# @public Since `stable-v1`.
17
17
# @asynchronous Will block until given block completes executing.
18
- def Sync ( &block )
18
+ def Sync ( annotation : nil , &block )
19
19
if task = ::Async ::Task . current?
20
- yield task
20
+ if annotation
21
+ task . annotate ( annotation ) { yield task }
22
+ else
23
+ yield task
24
+ end
21
25
elsif scheduler = Fiber . scheduler
22
26
::Async ::Task . run ( scheduler , &block ) . wait
23
27
else
24
28
# This calls Fiber.set_scheduler(self):
25
29
reactor = Async ::Reactor . new
26
30
27
31
begin
28
- return reactor . run ( finished : ::Async ::Condition . new , &block ) . wait
32
+ return reactor . run ( annotation : annotation , finished : ::Async ::Condition . new , &block ) . wait
29
33
ensure
30
34
Fiber . set_scheduler ( nil )
31
35
end
Original file line number Diff line number Diff line change 17
17
Async ( transient : true ) do |task |
18
18
expect ( task ) . to be ( :transient? )
19
19
end
20
+
21
+ Async ( annotation : 'foobar' ) do |task |
22
+ expect ( task . annotation ) . to be == 'foobar'
23
+ end
20
24
end
21
25
end
22
26
end
Original file line number Diff line number Diff line change 21
21
22
22
expect ( result ) . to be == value
23
23
end
24
-
24
+
25
+ it "passes annotation through to initial task" do
26
+ Sync ( annotation : 'foobar' ) do |task |
27
+ expect ( task . annotation ) . to be == 'foobar'
28
+ end
29
+ end
30
+
25
31
it "can run inside reactor" do
26
32
Async do |task |
27
33
result = Sync do |sync_task |
28
34
expect ( Async ::Task . current ) . to be == task
29
35
expect ( sync_task ) . to be == task
30
-
36
+
31
37
next value
32
38
end
33
-
39
+
34
40
expect ( result ) . to be == value
35
41
end
36
42
end
43
+
44
+ with "parent task" do
45
+ it "replaces and restores existing task's annotation" do
46
+ annotations = [ ]
47
+
48
+ Async ( annotation : "foo" ) do |t1 |
49
+ annotations << t1 . annotation
50
+
51
+ Sync ( annotation : "bar" ) do |t2 |
52
+ expect ( t2 ) . to be_equal ( t1 )
53
+ annotations << t1 . annotation
54
+ end
55
+
56
+ annotations << t1 . annotation
57
+ end . wait
58
+
59
+ expect ( annotations ) . to be == %w[ foo bar foo ]
60
+ end
61
+ end
62
+
37
63
38
64
it "can propagate error without logging them" do
39
65
expect ( Console ) . not . to receive ( :error )
You can’t perform that action at this time.
0 commit comments