Skip to content

Commit 9c875e9

Browse files
committed
Fix test race on IO#write.
1 parent a56d14b commit 9c875e9

3 files changed

Lines changed: 30 additions & 23 deletions

File tree

lib/async/task.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def to_s
136136

137137
# @deprecated Prefer {Kernel#sleep} except when compatibility with `stable-v1` is required.
138138
def sleep(duration = nil)
139-
warn("`Async::Task#sleep` is deprecated, use `Kernel#sleep` instead.", uplevel: 1, category: :deprecated) if $VERBOSE
139+
Kernel.warn("`Async::Task#sleep` is deprecated, use `Kernel#sleep` instead.", uplevel: 1, category: :deprecated) if $VERBOSE
140140

141141
super
142142
end

test/async/limited_queue.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,16 @@ def before
8787
with "#close" do
8888
it "signals tasks waiting to enqueue items when closed" do
8989
queue.enqueue(:item1)
90-
90+
9191
# This task will block as the queue is full:
9292
waiting_task = reactor.async do
93-
queue.enqueue(:item2)
93+
expect do
94+
queue.enqueue(:item2)
95+
end.to raise_exception(Async::Queue::ClosedError)
9496
end
95-
97+
9698
queue.close
97-
98-
expect do
99-
waiting_task.wait
100-
end.to raise_exception(Async::Queue::ClosedError)
99+
waiting_task.wait
101100
end
102101
end
103102
end

test/async/task.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@
7575

7676
with "#current?" do
7777
it "can check if it is the currently running task" do
78+
was_current = nil
79+
7880
task = reactor.async do |task|
79-
expect(task).to be(:current?)
80-
sleep(0.1)
81+
was_current = task.current?
8182
end
8283

84+
expect(was_current).to be == true
8385
expect(task).not.to be(:current?)
8486
end
8587
end
@@ -124,7 +126,8 @@
124126
reactor.run do
125127
expect do
126128
reactor.async do |task|
127-
expect(task).to receive(:warn).and_return(nil)
129+
# Ensure the wait is executed before raising the exception:
130+
task.yield
128131

129132
raise "boom"
130133
end.wait
@@ -154,19 +157,19 @@
154157
it "can consume exceptions" do
155158
task = nil
156159

157-
expect do
158-
task = reactor.async do |task|
159-
expect(task).to receive(:warn).and_return(nil)
160-
161-
raise "boom"
162-
end
163-
end.not.to raise_exception
164-
165160
reactor.run do
166161
expect do
167-
task.wait
168-
end.to raise_exception(RuntimeError, message: be =~ /boom/)
162+
task = reactor.async do |task|
163+
expect(task).to receive(:warn).and_return(nil)
164+
165+
raise "boom"
166+
end
167+
end.not.to raise_exception
169168
end
169+
170+
expect do
171+
task.wait
172+
end.to raise_exception(RuntimeError, message: be =~ /boom/)
170173
end
171174

172175
it "won't consume non-StandardError exceptions" do
@@ -508,6 +511,7 @@
508511

509512
it "can stop resumed task" do
510513
items = [1, 2, 3]
514+
value = nil
511515

512516
reactor.run do
513517
condition = Async::Condition.new
@@ -520,11 +524,11 @@
520524
end
521525

522526
value = condition.wait # (2) value = Fiber.yield
523-
expect(value).to be == 3
524527
producer.stop # (5) [producer is resumed already] producer.stop
525528
end
526529

527530
expect(items).to be == [1, 2]
531+
expect(value).to be == 3
528532
end
529533

530534
it "can stop a child task with transient children" do
@@ -720,7 +724,7 @@ def sleep_forever
720724
end
721725

722726
innocent_task = reactor.async do |task|
723-
expect{error_task.wait}.to raise_exception(RuntimeError, message: be =~ /boom/)
727+
error_task.wait
724728
end
725729

726730
expect do
@@ -729,6 +733,10 @@ def sleep_forever
729733

730734
expect(error_task).to be(:finished?)
731735
expect(innocent_task).to be(:finished?)
736+
737+
expect do
738+
innocent_task.wait
739+
end.to raise_exception(RuntimeError, message: be =~ /boom/)
732740
end
733741

734742
it "will not raise exception values returned by the task" do

0 commit comments

Comments
 (0)