|
3 | 3 | begin
|
4 | 4 | require "socket"
|
5 | 5 | require "test/unit"
|
| 6 | + require "io/nonblock" |
6 | 7 | rescue LoadError
|
7 | 8 | end
|
8 | 9 |
|
@@ -152,4 +153,53 @@ def test_for_fd
|
152 | 153 | sock.close
|
153 | 154 | end
|
154 | 155 | end
|
| 156 | + |
| 157 | + def test_read_write_nonblock |
| 158 | + socks do |sserv, ssock, csock| |
| 159 | + set_nb = true |
| 160 | + buf = String.new |
| 161 | + if ssock.respond_to?(:nonblock?) |
| 162 | + assert_not_predicate(ssock, :nonblock?) |
| 163 | + assert_not_predicate(csock, :nonblock?) |
| 164 | + |
| 165 | + # Linux may use MSG_DONTWAIT to avoid setting O_NONBLOCK |
| 166 | + if RUBY_PLATFORM.match?(/linux/) && Socket.const_defined?(:MSG_DONTWAIT) |
| 167 | + set_nb = false |
| 168 | + end |
| 169 | + end |
| 170 | + assert_equal :wait_readable, ssock.read_nonblock(1, buf, exception: false) |
| 171 | + assert_equal 5, csock.write_nonblock('hello') |
| 172 | + IO.select([ssock]) |
| 173 | + assert_same buf, ssock.read_nonblock(5, buf, exception: false) |
| 174 | + assert_equal 'hello', buf |
| 175 | + buf = '*' * 16384 |
| 176 | + n = 0 |
| 177 | + |
| 178 | + case w = csock.write_nonblock(buf, exception: false) |
| 179 | + when Integer |
| 180 | + n += w |
| 181 | + when :wait_writable |
| 182 | + break |
| 183 | + end while true |
| 184 | + |
| 185 | + assert_equal :wait_writable, w |
| 186 | + assert_raise(IO::WaitWritable) { loop { csock.write_nonblock(buf) } } |
| 187 | + assert_operator n, :>, 0 |
| 188 | + assert_not_predicate(csock, :nonblock?, '[Feature #13362]') unless set_nb |
| 189 | + csock.close |
| 190 | + |
| 191 | + case r = ssock.read_nonblock(16384, buf, exception: false) |
| 192 | + when String |
| 193 | + next |
| 194 | + when nil |
| 195 | + break |
| 196 | + else |
| 197 | + flunk "unexpected read_nonblock return: #{r.inspect}" |
| 198 | + end while true |
| 199 | + |
| 200 | + assert_raise(EOFError) { ssock.read_nonblock(1) } |
| 201 | + |
| 202 | + assert_not_predicate(ssock, :nonblock?) unless set_nb |
| 203 | + end |
| 204 | + end |
155 | 205 | end if defined?(BasicSocket)
|
0 commit comments