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