ione-rpc 1.0.0.pre1 → 1.0.0.pre2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ione/rpc/client_peer.rb +12 -3
- data/lib/ione/rpc/codec.rb +22 -0
- data/lib/ione/rpc/peer.rb +0 -4
- data/lib/ione/rpc/server.rb +1 -1
- data/lib/ione/rpc/version.rb +1 -1
- data/spec/ione/rpc/client_peer_spec.rb +22 -2
- data/spec/ione/rpc/client_spec.rb +1 -0
- data/spec/ione/rpc/codec_spec.rb +13 -0
- data/spec/ione/rpc/peer_common.rb +1 -0
- data/spec/ione/rpc/server_spec.rb +7 -3
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5c7643bab20779d92db259349ee30689f27f00ca
         | 
| 4 | 
            +
              data.tar.gz: ac4e876e133758cc94c5e52682685865225d7cdc
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 54903ddee404d09c58056af6e1766d8d4c2323037cbfdfac42c66c82ad11f152425fc542eab87adf05ea4ef67afb32e351e011ff906b4329edc2f1ba0af4b49e
         | 
| 7 | 
            +
              data.tar.gz: cfc8c07ab978079837a309b736505031ca422441c6c1083d81a23e55a82a0729528fb3c9874819ce5a9755bc4a6711d0946bdfcc64a1298db78fac5e167ae86a
         | 
    
        data/lib/ione/rpc/client_peer.rb
    CHANGED
    
    | @@ -13,6 +13,7 @@ module Ione | |
| 13 13 | 
             
                    @lock = Mutex.new
         | 
| 14 14 | 
             
                    @channels = [nil] * max_channels
         | 
| 15 15 | 
             
                    @queue = []
         | 
| 16 | 
            +
                    @encode_eagerly = @codec.recoding?
         | 
| 16 17 | 
             
                  end
         | 
| 17 18 |  | 
| 18 19 | 
             
                  def send_message(request, timeout=nil)
         | 
| @@ -21,10 +22,14 @@ module Ione | |
| 21 22 | 
             
                      take_channel(promise)
         | 
| 22 23 | 
             
                    end
         | 
| 23 24 | 
             
                    if channel
         | 
| 24 | 
            -
                       | 
| 25 | 
            +
                      @connection.write(@codec.encode(request, channel))
         | 
| 25 26 | 
             
                    else
         | 
| 26 27 | 
             
                      @lock.synchronize do
         | 
| 27 | 
            -
                        @ | 
| 28 | 
            +
                        if @encode_eagerly
         | 
| 29 | 
            +
                          @queue << [@codec.encode(request, -1), promise]
         | 
| 30 | 
            +
                        else
         | 
| 31 | 
            +
                          @queue << [request, promise]
         | 
| 32 | 
            +
                        end
         | 
| 28 33 | 
             
                      end
         | 
| 29 34 | 
             
                    end
         | 
| 30 35 | 
             
                    if timeout
         | 
| @@ -59,7 +64,11 @@ module Ione | |
| 59 64 | 
             
                      while count < max
         | 
| 60 65 | 
             
                        request, promise = @queue[count]
         | 
| 61 66 | 
             
                        if (channel = take_channel(promise))
         | 
| 62 | 
            -
                           | 
| 67 | 
            +
                          if @encode_eagerly
         | 
| 68 | 
            +
                            @connection.write(@codec.recode(request, channel))
         | 
| 69 | 
            +
                          else
         | 
| 70 | 
            +
                            @connection.write(@codec.encode(request))
         | 
| 71 | 
            +
                          end
         | 
| 63 72 | 
             
                          count += 1
         | 
| 64 73 | 
             
                        else
         | 
| 65 74 | 
             
                          break
         | 
    
        data/lib/ione/rpc/codec.rb
    CHANGED
    
    | @@ -79,6 +79,27 @@ module Ione | |
| 79 79 | 
             
                    end
         | 
| 80 80 | 
             
                  end
         | 
| 81 81 |  | 
| 82 | 
            +
                  # Whether or not this codec supports channel recoding, see {#recode}.
         | 
| 83 | 
            +
                  def recoding?
         | 
| 84 | 
            +
                    true
         | 
| 85 | 
            +
                  end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  # Recode the channel of a frame.
         | 
| 88 | 
            +
                  #
         | 
| 89 | 
            +
                  # This is used primarily by the client when it needs to queue a request
         | 
| 90 | 
            +
                  # because all channels are occupied. When the codec supports recoding the
         | 
| 91 | 
            +
                  # client can encode the request when it is queued, instead of when it is
         | 
| 92 | 
            +
                  # dequeued. This means that in most cases the calling thread will do the
         | 
| 93 | 
            +
                  # encoding instead of the IO reactor thread.
         | 
| 94 | 
            +
                  #
         | 
| 95 | 
            +
                  # @param [String] bytes an encoded frame (the output from {#encode})
         | 
| 96 | 
            +
                  # @param [Integer] channel the channel to encode into the frame
         | 
| 97 | 
            +
                  # @return [String] the reencoded frame
         | 
| 98 | 
            +
                  def recode(bytes, channel)
         | 
| 99 | 
            +
                    bytes[2, 2] = [channel].pack(CHANNEL_FORMAT)
         | 
| 100 | 
            +
                    bytes
         | 
| 101 | 
            +
                  end
         | 
| 102 | 
            +
             | 
| 82 103 | 
             
                  # @!method encode_message(message)
         | 
| 83 104 | 
             
                  #
         | 
| 84 105 | 
             
                  # Encode an object to bytes that can be sent over the network.
         | 
| @@ -131,6 +152,7 @@ module Ione | |
| 131 152 |  | 
| 132 153 | 
             
                  FRAME_V1_FORMAT = 'ccNa*'.freeze
         | 
| 133 154 | 
             
                  FRAME_V2_FORMAT = 'ccnNa*'.freeze
         | 
| 155 | 
            +
                  CHANNEL_FORMAT = 'n'.freeze
         | 
| 134 156 | 
             
                end
         | 
| 135 157 |  | 
| 136 158 | 
             
                # A codec that works with encoders like JSON, MessagePack, YAML and others
         | 
    
        data/lib/ione/rpc/peer.rb
    CHANGED
    
    
    
        data/lib/ione/rpc/server.rb
    CHANGED
    
    
    
        data/lib/ione/rpc/version.rb
    CHANGED
    
    
| @@ -16,7 +16,7 @@ module Ione | |
| 16 16 | 
             
                  end
         | 
| 17 17 |  | 
| 18 18 | 
             
                  let :codec do
         | 
| 19 | 
            -
                    double(:codec)
         | 
| 19 | 
            +
                    double(:codec, recoding?: true)
         | 
| 20 20 | 
             
                  end
         | 
| 21 21 |  | 
| 22 22 | 
             
                  let :scheduler do
         | 
| @@ -29,7 +29,7 @@ module Ione | |
| 29 29 |  | 
| 30 30 | 
             
                  before do
         | 
| 31 31 | 
             
                    codec.stub(:decode) do |buffer, current_frame|
         | 
| 32 | 
            -
                      message = buffer.to_s.scan(/[\w\d] | 
| 32 | 
            +
                      message = buffer.to_s.scan(/[\w\d]+@-?\d+/).flatten.first
         | 
| 33 33 | 
             
                      if message
         | 
| 34 34 | 
             
                        payload, channel = message.split('@')
         | 
| 35 35 | 
             
                        buffer.discard(message.bytesize)
         | 
| @@ -41,6 +41,10 @@ module Ione | |
| 41 41 | 
             
                    codec.stub(:encode) do |message, channel|
         | 
| 42 42 | 
             
                      '%s@%03d' % [message, channel]
         | 
| 43 43 | 
             
                    end
         | 
| 44 | 
            +
                    codec.stub(:recode) do |message, channel|
         | 
| 45 | 
            +
                      payload, _ = message.split('@')
         | 
| 46 | 
            +
                      codec.encode(payload, channel)
         | 
| 47 | 
            +
                    end
         | 
| 44 48 | 
             
                  end
         | 
| 45 49 |  | 
| 46 50 | 
             
                  before do
         | 
| @@ -89,6 +93,11 @@ module Ione | |
| 89 93 | 
             
                      connection.written_bytes.bytesize.should == max_channels * 7
         | 
| 90 94 | 
             
                    end
         | 
| 91 95 |  | 
| 96 | 
            +
                    it 'encodes messages when they are enqueued' do
         | 
| 97 | 
            +
                      (max_channels + 2).times { peer.send_message('foo') }
         | 
| 98 | 
            +
                      codec.should have_received(:encode).exactly(max_channels + 2).times
         | 
| 99 | 
            +
                    end
         | 
| 100 | 
            +
             | 
| 92 101 | 
             
                    it 'sends queued requests when channels become available' do
         | 
| 93 102 | 
             
                      (max_channels + 2).times { |i| peer.send_message("foo#{i.to_s.rjust(3, '0')}") }
         | 
| 94 103 | 
             
                      length_before = connection.written_bytes.bytesize
         | 
| @@ -117,6 +126,17 @@ module Ione | |
| 117 126 | 
             
                      scheduler.timer_promises.first.fulfill
         | 
| 118 127 | 
             
                      expect { f.value }.to_not raise_error
         | 
| 119 128 | 
             
                    end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                    context 'with a non-recoding codec' do
         | 
| 131 | 
            +
                      let :codec do
         | 
| 132 | 
            +
                        double(:codec, recoding?: false)
         | 
| 133 | 
            +
                      end
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                      it 'encodes messages when they are dequeued' do
         | 
| 136 | 
            +
                        (max_channels + 2).times { peer.send_message('foo') }
         | 
| 137 | 
            +
                        codec.should have_received(:encode).exactly(max_channels).times
         | 
| 138 | 
            +
                      end
         | 
| 139 | 
            +
                    end
         | 
| 120 140 | 
             
                  end
         | 
| 121 141 | 
             
                end
         | 
| 122 142 | 
             
              end
         | 
    
        data/spec/ione/rpc/codec_spec.rb
    CHANGED
    
    | @@ -90,6 +90,19 @@ module Ione | |
| 90 90 | 
             
                    end
         | 
| 91 91 | 
             
                  end
         | 
| 92 92 |  | 
| 93 | 
            +
                  describe '#recode' do
         | 
| 94 | 
            +
                    let :object do
         | 
| 95 | 
            +
                      {'foo' => 'bar', 'baz' => 42}
         | 
| 96 | 
            +
                    end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                    it 'changes the channel in the encoded bytes' do
         | 
| 99 | 
            +
                      encoded = codec.encode(object, 42)
         | 
| 100 | 
            +
                      recoded = codec.recode(encoded, 99)
         | 
| 101 | 
            +
                      _, channel, _ = codec.decode(Ione::ByteBuffer.new(recoded), nil)
         | 
| 102 | 
            +
                      channel.should == 99
         | 
| 103 | 
            +
                    end
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
             | 
| 93 106 | 
             
                  context 'when decoding and encoding' do
         | 
| 94 107 | 
             
                    let :message do
         | 
| 95 108 | 
             
                      {'foo' => 'bar', 'baz' => 42}
         | 
| @@ -160,14 +160,18 @@ module Ione | |
| 160 160 | 
             
                    end
         | 
| 161 161 |  | 
| 162 162 | 
             
                    it 'responds to the same peer and channel when the future returned by #handle_request is resolved' do
         | 
| 163 | 
            +
                      sent_pair = nil
         | 
| 164 | 
            +
                      codec.stub(:encode) do |response, channel|
         | 
| 165 | 
            +
                        sent_pair = [response, channel]
         | 
| 166 | 
            +
                        response
         | 
| 167 | 
            +
                      end
         | 
| 163 168 | 
             
                      promise = Promise.new
         | 
| 164 169 | 
             
                      server.override_handle_request { promise.future }
         | 
| 165 170 | 
             
                      peer = server.connections.first
         | 
| 166 | 
            -
                      peer.stub(:write_message)
         | 
| 167 171 | 
             
                      peer.handle_message('FOOBAZ', 42)
         | 
| 168 | 
            -
                       | 
| 172 | 
            +
                      sent_pair.should be_nil
         | 
| 169 173 | 
             
                      promise.fulfill('BAZFOO')
         | 
| 170 | 
            -
                       | 
| 174 | 
            +
                      sent_pair.should == ['BAZFOO', 42]
         | 
| 171 175 | 
             
                    end
         | 
| 172 176 |  | 
| 173 177 | 
             
                    it 'uses the codec to encode the response' do
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: ione-rpc
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0.0. | 
| 4 | 
            +
              version: 1.0.0.pre2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Theo Hultberg
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2014-06- | 
| 11 | 
            +
            date: 2014-06-10 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: ione
         |