Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,31 @@ public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IR
throw context.runtime.newErrnoEOPNOTSUPPError("Socket.socketpair only supports streaming UNIX sockets");
}

return RubyUNIXSocket.socketpair(context, recv, arrayOf(domain, type));
final Ruby runtime = context.runtime;

// TODO: type and protocol

UnixSocketChannel[] sp;

try {
sp = UnixSocketChannel.pair();
final RubyClass socketClass = runtime.getClass("Socket");

RubySocket sock0 = new RubySocket(runtime, socketClass);
ChannelFD fd0 = newChannelFD(runtime, sp[0]);
sock0.initFieldsFromDescriptor(runtime, fd0);
sock0.initSocket(fd0);

RubySocket sock1 = new RubySocket(runtime, socketClass);
ChannelFD fd1 = newChannelFD(runtime, sp[1]);
sock1.initFieldsFromDescriptor(runtime, fd1);
sock1.initSocket(fd1);

return runtime.newArray(sock0, sock1);

} catch (IOException ioe) {
throw runtime.newIOErrorFromException(ioe);
}
}

private void initFieldsFromDescriptor(Ruby runtime, ChannelFD fd) {
Expand Down
12 changes: 11 additions & 1 deletion spec/ruby/library/socket/shared/socketpair.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
describe :socket_socketpair, shared: true do
platform_is_not :windows do
it "ensures the returned sockets are connected" do
s1, s2 = Socket.socketpair(Socket::AF_UNIX, 1, 0)
s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, 1, 0)
s1.puts("test")
s2.gets.should == "test\n"
s1.close
s2.close
end

it "responses with array of two sockets" do
s1, s2 = Socket.public_send(@method, :UNIX, :STREAM)

s1.should be_an_instance_of(Socket)
s2.should be_an_instance_of(Socket)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opened file descriptors should be closed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ensure
s1.close
s2.close
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etehtsea We usually update from ruby/spec via @eregon's process of migrating subtree commits back and forth. I'm not sure, but this separate commit may interfere with that process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK to add specs directly here, even if the commits contains other changes. The advantages is it's tested directly against jruby and we don't need to wait for a spec merge.
However I prefer if the specs are in a commit alone as usually it means I don't need to edit the commit message 😃

end
end
1 change: 0 additions & 1 deletion spec/tags/ruby/library/socket/socket/socketpair_tags.txt

This file was deleted.