Skip to content

Commit 333cae6

Browse files
committed
More test coverage.
1 parent 3e993bc commit 333cae6

File tree

6 files changed

+166
-5
lines changed

6 files changed

+166
-5
lines changed

lib/async/wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def wait_writable(timeout = @timeout)
6464
# Wait fo the io to become either readable or writable.
6565
# @parameter duration [Float] timeout after the given duration if not `nil`.
6666
def wait_any(timeout = @timeout)
67-
@io.wait_any(timeout) or raise TimeoutError
67+
@io.to_io.wait(::IO::READABLE|::IO::WRITABLE|::IO::PRIORITY, timeout) or raise TimeoutError
6868
end
6969

7070
# Close the io and monitor.

spec/async/condition_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
condition.wait
5757
expect do
5858
producer.stop
59-
end.to_not raise_error
59+
end.to_not raise_exception
6060
end
6161

6262
consumer.wait

spec/async/semaphore_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
subject.acquire do
9595
end
9696
end
97-
end.to raise_error(Async::TimeoutError)
97+
end.to raise_exception(Async::TimeoutError)
9898

9999
expect(subject.waiting).to be_empty
100100
end

spec/async/task_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def sleep_forever
504504
end
505505
end
506506

507-
expect{task.wait}.to raise_error(Async::TimeoutError)
507+
expect{task.wait}.to raise_exception(Async::TimeoutError)
508508

509509
error = task.result
510510
expect(error.backtrace).to include(/sleep_forever/)

spec/async/variable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
subject.resolve(value)
4141
expect do
4242
subject.resolve(value)
43-
end.to raise_error(FrozenError)
43+
end.to raise_exception(FrozenError)
4444
end
4545
end
4646

spec/async/wrapper_spec.rb

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
require "async/rspec"
24+
require "async/wrapper"
25+
26+
RSpec.describe Async::Wrapper do
27+
include_context Async::RSpec::Reactor
28+
29+
let(:pipe) {IO.pipe}
30+
let(:input) {Async::Wrapper.new(pipe.last)}
31+
let(:output) {Async::Wrapper.new(pipe.first)}
32+
33+
after(:each) do
34+
input.close unless input.closed?
35+
output.close unless output.closed?
36+
end
37+
38+
describe '#wait_readable' do
39+
it "can wait to be readable" do
40+
reader = reactor.async do
41+
expect(output.wait_readable).to be_truthy
42+
end
43+
44+
input.io.write('Hello World')
45+
reader.wait
46+
end
47+
48+
it "can timeout if no event occurs" do
49+
expect do
50+
output.wait_readable(0.1)
51+
end.to raise_exception(Async::TimeoutError)
52+
end
53+
54+
it "can wait for readability in sequential tasks" do
55+
reactor.async do
56+
input.wait_writable(1)
57+
input.io.write('Hello World')
58+
end
59+
60+
2.times do
61+
reactor.async do
62+
expect(output.wait_readable(1)).to be_truthy
63+
end.wait
64+
end
65+
end
66+
end
67+
68+
describe '#wait_writable' do
69+
it "can wait to be writable" do
70+
expect(input.wait_writable).to be_truthy
71+
end
72+
73+
it "can be cancelled while waiting to be readable" do
74+
task = reactor.async do
75+
input.wait_readable
76+
end
77+
78+
output.close
79+
80+
expect do
81+
task.wait
82+
end.to raise_exception(IOError)
83+
end
84+
end
85+
86+
describe '#wait_priority' do
87+
let(:pipe) {::Socket.pair(:UNIX, :STREAM)}
88+
89+
it "can wait to be priority" do
90+
begin
91+
# I've tested this successfully on Linux but it fails on Darwin.
92+
input.io.send('!', Socket::MSG_OOB)
93+
rescue Errno::EOPNOTSUPP => error
94+
skip error.message
95+
end
96+
97+
reader = reactor.async do
98+
expect(output.wait_priority).to be_truthy
99+
end
100+
101+
reader.wait
102+
end
103+
end
104+
105+
describe "#wait_any" do
106+
it "can wait for any events" do
107+
reactor.async do
108+
input.wait_any(1)
109+
input.io.write('Hello World')
110+
end
111+
112+
expect(output.wait_readable(1)).to be_truthy
113+
end
114+
115+
it "can wait for readability in one task and writability in another" do
116+
reactor.async do
117+
expect do
118+
input.wait_readable(1)
119+
end.to raise_exception(IOError)
120+
end
121+
122+
reactor.async do
123+
input.wait_writable
124+
125+
input.close
126+
output.close
127+
end.wait
128+
end
129+
end
130+
131+
describe '#reactor=' do
132+
it 'can assign a wrapper to a reactor' do
133+
input.reactor = reactor
134+
135+
expect(input.reactor).to be == reactor
136+
end
137+
end
138+
139+
describe '#dup' do
140+
let(:dup) {input.dup}
141+
142+
it 'dups the underlying io' do
143+
expect(dup.io).to_not eq input.io
144+
145+
dup.close
146+
147+
expect(input).to_not be_closed
148+
end
149+
end
150+
151+
describe '#close' do
152+
it "can't wait on closed wrapper" do
153+
input.close
154+
output.close
155+
156+
expect do
157+
output.wait_readable
158+
end.to raise_exception(IOError, /closed stream/)
159+
end
160+
end
161+
end

0 commit comments

Comments
 (0)