diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | 95e8c48dd3348503a8c7db5d0498894a1b676395 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/socket/unixserver | |
parent | ed7d803500de38186c74bce94d233e85ef51e503 (diff) |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/socket/unixserver')
6 files changed, 185 insertions, 0 deletions
diff --git a/spec/rubyspec/library/socket/unixserver/accept_nonblock_spec.rb b/spec/rubyspec/library/socket/unixserver/accept_nonblock_spec.rb new file mode 100644 index 0000000000..bad9139eea --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/accept_nonblock_spec.rb @@ -0,0 +1,40 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXServer#accept_nonblock" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + + @socket = @server.accept_nonblock + @client.send("foobar", 0) + end + + after :each do + @socket.close + @client.close + @server.close + rm_r @path + end + + it "accepts a connection in a non-blocking way" do + data = @socket.recvfrom(6).first + data.should == "foobar" + end + + it "returns a UNIXSocket" do + @socket.should be_kind_of(UNIXSocket) + end + + ruby_version_is '2.3' do + it 'returns :wait_readable in exceptionless mode' do + @server.accept_nonblock(exception: false).should == :wait_readable + end + end + end +end diff --git a/spec/rubyspec/library/socket/unixserver/accept_spec.rb b/spec/rubyspec/library/socket/unixserver/accept_spec.rb new file mode 100644 index 0000000000..6ce1f2c6db --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/accept_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +platform_is_not :windows do + describe "UNIXServer#accept" do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + end + + after :each do + rm_r @path + end + + it "accepts what is written by the client" do + server = UNIXServer.open(SocketSpecs.socket_path) + client = UNIXSocket.open(SocketSpecs.socket_path) + + client.send('hello', 0) + + sock = server.accept + data, info = sock.recvfrom(5) + + data.should == 'hello' + info.should_not be_empty + + server.close + client.close + sock.close + end + + it "can be interrupted by Thread#kill" do + server = UNIXServer.new(@path) + t = Thread.new { + server.accept + } + Thread.pass while t.status and t.status != "sleep" + + # kill thread, ensure it dies in a reasonable amount of time + t.kill + a = 1 + while a < 2000 + break unless t.alive? + Thread.pass + sleep 0.2 + a += 1 + end + a.should < 2000 + server.close + end + + it "can be interrupted by Thread#raise" do + server = UNIXServer.new(@path) + t = Thread.new { + server.accept + } + Thread.pass while t.status and t.status != "sleep" + + # raise in thread, ensure the raise happens + ex = Exception.new + t.raise ex + lambda { t.join }.should raise_error(Exception) + server.close + end + end +end diff --git a/spec/rubyspec/library/socket/unixserver/for_fd_spec.rb b/spec/rubyspec/library/socket/unixserver/for_fd_spec.rb new file mode 100644 index 0000000000..bf8aa41d40 --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/for_fd_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +platform_is_not :windows do + describe "UNIXServer#for_fd" do + before :each do + @unix_path = tmp("unix_socket") + @unix = UNIXServer.new(@unix_path) + end + + after :each do + @unix.close if @unix + rm_r @unix_path + end + + it "can calculate the path" do + b = UNIXServer.for_fd(@unix.fileno) + b.autoclose = false + + b.path.should == @unix_path + end + end +end diff --git a/spec/rubyspec/library/socket/unixserver/new_spec.rb b/spec/rubyspec/library/socket/unixserver/new_spec.rb new file mode 100644 index 0000000000..d34aa0ca03 --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/new_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/new', __FILE__) + +describe "UNIXServer.new" do + it_behaves_like :unixserver_new, :new +end diff --git a/spec/rubyspec/library/socket/unixserver/open_spec.rb b/spec/rubyspec/library/socket/unixserver/open_spec.rb new file mode 100644 index 0000000000..4401d9dda8 --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/open_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/new', __FILE__) + +describe "UNIXServer.open" do + it_behaves_like :unixserver_new, :open + + platform_is_not :windows do + before :each do + @path = tmp("unixserver_spec") + rm_r @path + end + + after :each do + @server.close if @server + @server = nil + rm_r @path + end + + it "yields the new UNIXServer object to the block, if given" do + UNIXServer.open(@path) do |unix| + unix.path.should == @path + unix.addr.should == ["AF_UNIX", @path] + end + end + end +end diff --git a/spec/rubyspec/library/socket/unixserver/shared/new.rb b/spec/rubyspec/library/socket/unixserver/shared/new.rb new file mode 100644 index 0000000000..9b0798b828 --- /dev/null +++ b/spec/rubyspec/library/socket/unixserver/shared/new.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../../../fixtures/classes', __FILE__) +require 'tempfile' + +describe :unixserver_new, shared: true do + platform_is_not :windows do + before :each do + @path = tmp("unixserver_spec") + rm_r @path + end + + after :each do + @server.close if @server + @server = nil + rm_r @path + end + + it "creates a new UNIXServer" do + @server = UNIXServer.send(@method, @path) + @server.path.should == @path + @server.addr.should == ["AF_UNIX", @path] + end + end +end |