Skip to content

Commit

Permalink
Add more tests for close cross-thread/fiber
Browse files Browse the repository at this point in the history
Also, catch exceptions inside thread/fiber so that stderr is clean and
so that we can ensure we've caught the right IOError.
  • Loading branch information
jscheid committed Jan 15, 2025
1 parent 4e0b9f6 commit d7b87fe
Showing 1 changed file with 114 additions and 3 deletions.
117 changes: 114 additions & 3 deletions test/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,122 @@

read_task = Async do
r.read(5)
nil
rescue IOError => e
e.to_s
end

r.close

expect{read_task.wait}.to raise_exception(IOError)

expect(read_task.wait).to be == 'closed stream'
end

it "can interrupt reading fiber when closing from another fiber" do
r, w = IO.pipe

read_task = Async do
r.read(5)
nil
rescue IOError => e
e.to_s
end

close_task = Async do
r.close
end

close_task.wait
expect(read_task.wait).to be == 'closed stream'
end

it "can interrupt reading fiber when closing from a new thread" do
r, w = IO.pipe

read_task = Async do
r.read(5)
nil
rescue IOError => e
e.to_s
end

close_thread = Thread.new do
r.close
end

close_thread.value
expect(read_task.wait).to be == 'closed stream'
end

it "can interrupt reading fiber when closing from a fiber in a new thread" do
r, w = IO.pipe

read_task = Async do
r.read(5)
nil
rescue IOError => e
e.to_s
end

close_thread = Thread.new do
close_task = Async do
r.close
end
close_task.wait
end

close_thread.value
expect(read_task.wait).to be == 'closed stream'
end

it "can interrupt reading thread when closing from a fiber" do
r, w = IO.pipe

read_thread = Thread.new do
Thread.current.report_on_exception = false
r.read(5)
nil
rescue IOError => e
e.to_s
end

# Wait until read_thread blocks on I/O
while read_thread.status != 'sleep'
sleep(0.001)
end

close_task = Async do
r.close
end

close_task.wait
expect(read_thread.value).to be == 'closed stream'
end

it "can interrupt reading fiber in a new thread when closing from a fiber" do
r, w = IO.pipe

read_thread = Thread.new do
Thread.current.report_on_exception = false
read_task = Async do
r.read(5)
nil
rescue IOError => e
e.to_s
end
read_task.wait
end

# Wait until read_thread blocks on I/O
while read_thread.status != 'sleep'
sleep(0.001)
end

close_task = Async do
r.close
end
close_task.wait

expect(read_thread.value).to be == 'closed stream'
end
end
end

0 comments on commit d7b87fe

Please sign in to comment.