diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2022-09-20 16:10:56 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2023-08-30 10:07:18 +0200 |
commit | bcc905100f1079e191632cfd02319c10af82dac0 (patch) | |
tree | e8dbe37eb4de741c51210f65ffaa0336ce579c8a /spec/ruby | |
parent | acedbcb1b4eb6b362f11e783bff53c237d05afc6 (diff) | |
download | ruby-bcc905100f1079e191632cfd02319c10af82dac0.tar.gz |
BasicSocket#recv* return `nil` rather than an empty packet
[Bug #19012]
man recvmsg(2) states:
> Return Value
> These calls return the number of bytes received, or -1 if an error occurred.
> The return value will be 0 when the peer has performed an orderly shutdown.
Not too sure how one is supposed to make the difference between a packet of
size 0 and a closed connection.
Diffstat (limited to 'spec/ruby')
-rw-r--r-- | spec/ruby/library/socket/basicsocket/recv_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/library/socket/basicsocket/send_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/library/socket/basicsocket/shutdown_spec.rb | 20 | ||||
-rw-r--r-- | spec/ruby/library/socket/fixtures/classes.rb | 2 |
4 files changed, 32 insertions, 13 deletions
diff --git a/spec/ruby/library/socket/basicsocket/recv_spec.rb b/spec/ruby/library/socket/basicsocket/recv_spec.rb index b6ccda5d00..a56114f4ab 100644 --- a/spec/ruby/library/socket/basicsocket/recv_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recv_spec.rb @@ -32,6 +32,25 @@ describe "BasicSocket#recv" do ScratchPad.recorded.should == 'hello' end + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + t = Thread.new do + client = @server.accept + packet = client.recv(10) + client.close + packet + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + + t.value.should be_nil + end + end + platform_is_not :solaris do it "accepts flags to specify unusual receiving behaviour" do t = Thread.new do diff --git a/spec/ruby/library/socket/basicsocket/send_spec.rb b/spec/ruby/library/socket/basicsocket/send_spec.rb index 868801df30..041ce03d72 100644 --- a/spec/ruby/library/socket/basicsocket/send_spec.rb +++ b/spec/ruby/library/socket/basicsocket/send_spec.rb @@ -22,7 +22,7 @@ describe "BasicSocket#send" do client = @server.accept loop do got = client.recv(5) - break if got.empty? + break if got.nil? || got.empty? data << got end client.close @@ -67,7 +67,7 @@ describe "BasicSocket#send" do client = @server.accept loop do got = client.recv(5) - break if got.empty? + break if got.nil? || got.empty? data << got end client.close diff --git a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb index 41d9581bde..c78b32de38 100644 --- a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb +++ b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb @@ -23,7 +23,7 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading' do @client.shutdown(Socket::SHUT_RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing' do @@ -35,7 +35,7 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading and writing' do @client.shutdown(Socket::SHUT_RDWR) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end @@ -49,13 +49,13 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading using :RD' do @client.shutdown(:RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using :SHUT_RD' do @client.shutdown(:SHUT_RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing using :WR' do @@ -73,7 +73,7 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading and writing' do @client.shutdown(:RDWR) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end @@ -87,13 +87,13 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading using "RD"' do @client.shutdown('RD') - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using "SHUT_RD"' do @client.shutdown('SHUT_RD') - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing using "WR"' do @@ -123,7 +123,7 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using "SHUT_RD"' do @@ -131,7 +131,7 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading and writing' do @@ -139,7 +139,7 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end diff --git a/spec/ruby/library/socket/fixtures/classes.rb b/spec/ruby/library/socket/fixtures/classes.rb index 406bd7c710..786629d2ef 100644 --- a/spec/ruby/library/socket/fixtures/classes.rb +++ b/spec/ruby/library/socket/fixtures/classes.rb @@ -113,7 +113,7 @@ module SocketSpecs begin data = socket.recv(1024) - return if data.empty? + return if data.nil? || data.empty? log "SpecTCPServer received: #{data.inspect}" return if data == "QUIT" |