You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/18 03:15:25 UTC

svn commit: r668995 - in /incubator/thrift/trunk/lib/rb: lib/thrift/transport.rb lib/thrift/transport/socket.rb spec/socket_spec.rb spec/transport_spec.rb

Author: kclark
Date: Tue Jun 17 18:15:25 2008
New Revision: 668995

URL: http://svn.apache.org/viewvc?rev=668995&view=rev
Log:
Add a few accessors and specs to prepare for the upcoming NonblockingServer

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
    incubator/thrift/trunk/lib/rb/spec/socket_spec.rb
    incubator/thrift/trunk/lib/rb/spec/transport_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb?rev=668995&r1=668994&r2=668995&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb Tue Jun 17 18:15:25 2008
@@ -50,6 +50,7 @@
     deprecate! :readAll => :read_all
   
     def write(buf); end
+    alias_method :<<, :write
 
     def flush; end
   end
@@ -182,8 +183,12 @@
   deprecate_class! :TFramedTransportFactory => FramedTransportFactory
 
   class MemoryBuffer < Transport
-    def initialize
-      @buf = ''
+    # If you pass a string to this, you should #dup that string
+    # unless you want it to be modified by #read and #write
+    #--
+    # yes this behavior is intentional
+    def initialize(buffer = nil)
+      @buf = buffer || ''
     end
 
     def open?
@@ -200,6 +205,7 @@
       not @buf.empty?
     end
 
+    # this method does not use the passed object directly but copies it
     def reset_buffer(new_buf = '')
       @buf.replace new_buf
     end

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb?rev=668995&r1=668994&r2=668995&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb Tue Jun 17 18:15:25 2008
@@ -17,9 +17,7 @@
       @handle = nil
     end
 
-    def set_handle(handle)
-      @handle = handle
-    end
+    attr_accessor :handle
 
     def open
       begin
@@ -66,6 +64,8 @@
       @handle = nil
     end
 
+    attr_reader :handle
+
     def listen
       @handle = TCPServer.new(nil, @port)
     end
@@ -74,7 +74,7 @@
       unless @handle.nil?
         sock = @handle.accept
         trans = Socket.new
-        trans.set_handle(sock)
+        trans.handle = sock
         trans
       end
     end

Modified: incubator/thrift/trunk/lib/rb/spec/socket_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/socket_spec.rb?rev=668995&r1=668994&r2=668995&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/socket_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/socket_spec.rb Tue Jun 17 18:15:25 2008
@@ -29,9 +29,9 @@
       TCPSocket.should_receive(:new).and_return(@handle)
       @socket.open
       @socket.should be_open
-      @socket.set_handle nil
+      @socket.handle = nil
       @socket.should_not be_open
-      @socket.set_handle @handle
+      @socket.handle = @handle
       @handle.should_receive(:close)
       @socket.close
       @socket.should_not be_open
@@ -85,8 +85,8 @@
     end
 
     it "should create a handle when calling listen" do
-      TCPServer.should_receive(:new).with(nil, 1234)
       @socket.listen
+      @socket.handle.should be_an_instance_of(TCPServer)
     end
 
     it "should create a Thrift::Socket to wrap accepted sockets" do
@@ -97,7 +97,7 @@
       handle.should_receive(:accept).and_return(sock)
       trans = mock("Socket")
       Socket.should_receive(:new).and_return(trans)
-      trans.should_receive(:set_handle).with(sock)
+      trans.should_receive(:handle=).with(sock)
       @socket.accept.should == trans
     end
 

Modified: incubator/thrift/trunk/lib/rb/spec/transport_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/transport_spec.rb?rev=668995&r1=668994&r2=668995&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/transport_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/transport_spec.rb Tue Jun 17 18:15:25 2008
@@ -26,6 +26,10 @@
         Transport.method_defined?(sym).should be_true
       end
     end
+
+    it "should alias << to write" do
+      Transport.instance_method(:<<).should == Transport.instance_method(:write)
+    end
   end
 
   describe ServerTransport do
@@ -208,6 +212,13 @@
       @buffer = MemoryBuffer.new
     end
 
+    it "should accept a buffer on input and use it directly" do
+      s = "this is a test"
+      @buffer = MemoryBuffer.new(s)
+      @buffer.read(4).should == "this"
+      s.should == " is a test"
+    end
+
     it "should always remain open" do
       @buffer.should be_open
       @buffer.close
@@ -235,6 +246,15 @@
       @buffer.available.should == 0
     end
 
+    it "should copy the given string whne resetting the buffer" do
+      s = "this is a test"
+      @buffer.reset_buffer(s)
+      @buffer.available.should == 14
+      @buffer.read(10)
+      @buffer.available.should == 4
+      s.should == "this is a test"
+    end
+
     it "should return from read what was given in write" do
       @buffer.write "test data"
       @buffer.read(4).should == "test"