|
| 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