This repository was archived by the owner on Aug 29, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,10 @@ def connected?
197
197
@io . connected?
198
198
end
199
199
200
+ def readable?
201
+ @io . readable?
202
+ end
203
+
200
204
def closed?
201
205
@io . closed?
202
206
end
@@ -246,6 +250,21 @@ def eof!
246
250
247
251
private
248
252
253
+ def sysread ( size , buffer )
254
+ while true
255
+ result = @io . read_nonblock ( size , buffer , exception : false )
256
+
257
+ case result
258
+ when :wait_readable
259
+ @io . wait_readable
260
+ when :wait_writable
261
+ @io . wait_writable
262
+ else
263
+ return result
264
+ end
265
+ end
266
+ end
267
+
249
268
# Fills the buffer from the underlying stream.
250
269
def fill_read_buffer ( size = @block_size )
251
270
# We impose a limit because the underlying `read` system call can fail if we request too much data in one go.
@@ -257,12 +276,12 @@ def fill_read_buffer(size = @block_size)
257
276
flush
258
277
259
278
if @read_buffer . empty?
260
- if @io . read_nonblock ( size , @read_buffer , exception : false )
279
+ if sysread ( size , @read_buffer )
261
280
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
262
281
return true
263
282
end
264
283
else
265
- if chunk = @io . read_nonblock ( size , @input_buffer , exception : false )
284
+ if chunk = sysread ( size , @input_buffer )
266
285
@read_buffer << chunk
267
286
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
268
287
Original file line number Diff line number Diff line change 23
23
end
24
24
end
25
25
26
+ context "native I/O" , if : RUBY_VERSION >= "3.1" do
27
+ let ( :sockets ) do
28
+ @sockets = ::Socket . pair ( ::Socket ::AF_UNIX , ::Socket ::SOCK_STREAM )
29
+ end
30
+
31
+ after do
32
+ @sockets . each ( &:close )
33
+ end
34
+
35
+ let ( :io ) { sockets . first }
36
+ subject { described_class . new ( sockets . last ) }
37
+
38
+ it "can read data" do
39
+ io . write ( "Hello World" )
40
+ io . close_write
41
+
42
+ expect ( subject . read ) . to be == "Hello World"
43
+ end
44
+ end
45
+
26
46
context "socket I/O" do
27
47
let ( :sockets ) do
28
48
@sockets = Async ::IO ::Socket . pair ( Socket ::AF_UNIX , Socket ::SOCK_STREAM )
You can’t perform that action at this time.
0 commit comments