ione-rpc 1.0.0.pre1 → 1.0.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51144d6e83a35056bbefda51115605b74141c2a1
4
- data.tar.gz: 0d983eee0b3c1810a6c8a3a1a311bf4d7e4b8941
3
+ metadata.gz: 5c7643bab20779d92db259349ee30689f27f00ca
4
+ data.tar.gz: ac4e876e133758cc94c5e52682685865225d7cdc
5
5
  SHA512:
6
- metadata.gz: 652a513b927f5c485e960e2211e4a687ba64034285fa631dbcfee72a91ab19f4bcbbaa09b95875b8deeba3efe2b3b3f77bf8674de1536225f9d0d042b7e06f61
7
- data.tar.gz: 500e33e208a693abf1ce8495d4c8e6250672eb16832bd17ca8e9195def804a41338d2484d431bd13c8bbf0263164bd1c3dca4f3a1f95a3df6a8dc1a41419cc90
6
+ metadata.gz: 54903ddee404d09c58056af6e1766d8d4c2323037cbfdfac42c66c82ad11f152425fc542eab87adf05ea4ef67afb32e351e011ff906b4329edc2f1ba0af4b49e
7
+ data.tar.gz: cfc8c07ab978079837a309b736505031ca422441c6c1083d81a23e55a82a0729528fb3c9874819ce5a9755bc4a6711d0946bdfcc64a1298db78fac5e167ae86a
@@ -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
- write_message(request, channel)
25
+ @connection.write(@codec.encode(request, channel))
25
26
  else
26
27
  @lock.synchronize do
27
- @queue << [request, promise]
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
- write_message(request, channel)
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
@@ -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
@@ -45,10 +45,6 @@ module Ione
45
45
  def handle_closed(cause=nil)
46
46
  @closed_promise.fulfill(cause)
47
47
  end
48
-
49
- def write_message(message, channel)
50
- @connection.write(@codec.encode(message, channel))
51
- end
52
48
  end
53
49
  end
54
50
  end
@@ -92,7 +92,7 @@ module Ione
92
92
  def handle_message(message, channel)
93
93
  f = @server.handle_request(message, self)
94
94
  f.on_value do |response|
95
- write_message(response, channel)
95
+ @connection.write(@codec.encode(response, channel))
96
96
  end
97
97
  end
98
98
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ione
4
4
  module Rpc
5
- VERSION = '1.0.0.pre1'.freeze
5
+ VERSION = '1.0.0.pre2'.freeze
6
6
  end
7
7
  end
@@ -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]+@\d+/).flatten.first
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
@@ -58,6 +58,7 @@ module Ione
58
58
  end
59
59
 
60
60
  before do
61
+ codec.stub(:recoding?).and_return(false)
61
62
  codec.stub(:encode) { |input, channel| input }
62
63
  end
63
64
 
@@ -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}
@@ -29,6 +29,7 @@ shared_examples 'peers' do
29
29
 
30
30
  before do
31
31
  channel = 9
32
+ codec.stub(:recoding?).and_return(false)
32
33
  codec.stub(:decode) do |buffer, previous_frame|
33
34
  if buffer.empty?
34
35
  [empty_frame, channel, false]
@@ -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
- peer.should_not have_received(:write_message)
172
+ sent_pair.should be_nil
169
173
  promise.fulfill('BAZFOO')
170
- peer.should have_received(:write_message).with('BAZFOO', 42)
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.pre1
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-09 00:00:00.000000000 Z
11
+ date: 2014-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ione