Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | class ThriftSocketSpec < Spec::ExampleGroup |
| 4 | include Thrift |
| 5 | |
| 6 | before(:each) do |
| 7 | @socket = Socket.new |
| 8 | @handle = mock("Handle") |
| 9 | end |
| 10 | |
| 11 | describe Socket do |
| 12 | it "should open a TCPSocket" do |
| 13 | TCPSocket.should_receive(:new).with('localhost', 9090).and_return(@handle) |
| 14 | @socket.open.should == @handle |
| 15 | end |
| 16 | |
| 17 | it "should accept host/port options" do |
| 18 | TCPSocket.should_receive(:new).with('my.domain', 1234) |
| 19 | Socket.new('my.domain', 1234).open |
| 20 | end |
| 21 | |
| 22 | it "should raise a TransportException when it cannot open a socket" do |
| 23 | TCPSocket.should_receive(:new).with('localhost', 9090).and_raise(StandardError) |
| 24 | lambda { @socket.open }.should raise_error(TransportException, "Could not connect to localhost:9090") { |e| e.type.should == TransportException::NOT_OPEN } |
| 25 | end |
| 26 | |
| 27 | it "should be open whenever it has a handle" do |
| 28 | @socket.should_not be_open |
| 29 | TCPSocket.should_receive(:new).and_return(@handle) |
| 30 | @socket.open |
| 31 | @socket.should be_open |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 32 | @socket.handle = nil |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 33 | @socket.should_not be_open |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 34 | @socket.handle = @handle |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 35 | @handle.should_receive(:close) |
| 36 | @socket.close |
| 37 | @socket.should_not be_open |
| 38 | end |
| 39 | |
| 40 | it "should write data to the handle" do |
| 41 | TCPSocket.should_receive(:new).and_return(@handle) |
| 42 | @socket.open |
| 43 | @handle.should_receive(:write).with("foobar") |
| 44 | @socket.write("foobar") |
| 45 | @handle.should_receive(:write).with("fail").and_raise(StandardError) |
| 46 | lambda { @socket.write("fail") }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN } |
| 47 | end |
| 48 | |
| 49 | it "should raise an error when it cannot read from the handle" do |
| 50 | TCPSocket.should_receive(:new).and_return(@handle) |
| 51 | @socket.open |
Kevin Clark | c78eeef | 2008-06-18 01:15:36 +0000 | [diff] [blame] | 52 | @handle.should_receive(:read).with(17).and_raise(StandardError) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 53 | lambda { @socket.read(17) }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN } |
| 54 | end |
| 55 | |
| 56 | it "should raise an error when it reads no data from the handle" do |
| 57 | TCPSocket.should_receive(:new).and_return(@handle) |
| 58 | @socket.open |
Kevin Clark | c78eeef | 2008-06-18 01:15:36 +0000 | [diff] [blame] | 59 | @handle.should_receive(:read).with(17).and_return("") |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 60 | lambda { @socket.read(17) }.should raise_error(TransportException, "Socket: Could not read 17 bytes from localhost:9090") |
| 61 | end |
| 62 | |
| 63 | it "should return the data read when reading from the handle works" do |
| 64 | TCPSocket.should_receive(:new).and_return(@handle) |
| 65 | @socket.open |
Kevin Clark | c78eeef | 2008-06-18 01:15:36 +0000 | [diff] [blame] | 66 | @handle.should_receive(:read).with(17).and_return("test data") |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 67 | @socket.read(17).should == "test data" |
| 68 | end |
Kevin Clark | f98286a | 2008-06-18 01:14:08 +0000 | [diff] [blame] | 69 | |
| 70 | it "should declare itself as closed when it has an error" do |
Kevin Clark | ec9106f | 2008-06-18 01:14:26 +0000 | [diff] [blame] | 71 | pending do |
| 72 | TCPSocket.should_receive(:new).and_return(@handle) |
| 73 | @socket.open |
| 74 | @handle.should_receive(:write).with("fail").and_raise(StandardError) |
| 75 | @socket.should be_open |
| 76 | lambda { @socket.write("fail") }.should raise_error |
| 77 | @socket.should_not be_open |
| 78 | end |
Kevin Clark | f98286a | 2008-06-18 01:14:08 +0000 | [diff] [blame] | 79 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 80 | end |
| 81 | |
| 82 | describe ServerSocket do |
| 83 | before(:each) do |
| 84 | @socket = ServerSocket.new(1234) |
| 85 | end |
| 86 | |
| 87 | it "should create a handle when calling listen" do |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 88 | @socket.listen |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 89 | @socket.handle.should be_an_instance_of(TCPServer) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 90 | end |
| 91 | |
Kevin Clark | c675870 | 2008-06-18 01:15:45 +0000 | [diff] [blame^] | 92 | it "should accept an optional host argument" do |
| 93 | @socket = ServerSocket.new('localhost', 1234) |
| 94 | TCPServer.should_receive(:new).with('localhost', 1234) |
| 95 | @socket.listen |
| 96 | end |
| 97 | |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 98 | it "should create a Thrift::Socket to wrap accepted sockets" do |
| 99 | handle = mock("TCPServer") |
| 100 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 101 | @socket.listen |
| 102 | sock = mock("sock") |
| 103 | handle.should_receive(:accept).and_return(sock) |
| 104 | trans = mock("Socket") |
| 105 | Socket.should_receive(:new).and_return(trans) |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 106 | trans.should_receive(:handle=).with(sock) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 107 | @socket.accept.should == trans |
| 108 | end |
| 109 | |
| 110 | it "should close the handle when closed" do |
| 111 | handle = mock("TCPServer") |
| 112 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 113 | @socket.listen |
| 114 | handle.should_receive(:close) |
| 115 | @socket.close |
| 116 | end |
Kevin Clark | 0d6007c | 2008-06-18 01:13:27 +0000 | [diff] [blame] | 117 | |
| 118 | it "should return nil when accepting if there is no handle" do |
| 119 | @socket.accept.should be_nil |
| 120 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 121 | end |
| 122 | end |