ione 1.3.0.pre0 → 1.3.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/ione/byte_buffer.rb +31 -6
- data/lib/ione/future.rb +38 -26
- data/lib/ione/io/acceptor.rb +1 -0
- data/lib/ione/io/base_connection.rb +23 -10
- data/lib/ione/io/connection.rb +1 -0
- data/lib/ione/io/io_reactor.rb +27 -37
- data/lib/ione/io/ssl_connection.rb +1 -0
- data/lib/ione/version.rb +1 -1
- data/spec/integration/io_spec.rb +3 -3
- data/spec/integration/ssl_spec.rb +7 -2
- data/spec/ione/byte_buffer_spec.rb +91 -38
- data/spec/ione/future_spec.rb +64 -65
- data/spec/ione/heap_spec.rb +18 -18
- data/spec/ione/io/acceptor_spec.rb +5 -5
- data/spec/ione/io/connection_common.rb +2 -2
- data/spec/ione/io/io_reactor_spec.rb +43 -30
- data/spec/ione/io/ssl_acceptor_spec.rb +3 -3
- data/spec/ione/io/ssl_connection_spec.rb +3 -3
- data/spec/spec_helper.rb +4 -0
- data/spec/support/fake_server.rb +1 -0
- metadata +16 -18
@@ -15,28 +15,28 @@ module Ione
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'can be initialized with bytes' do
|
18
|
-
described_class.new('hello').length.should
|
18
|
+
described_class.new('hello').length.should eq(5)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe '#length/#size/#bytesize' do
|
23
23
|
it 'returns the number of bytes in the buffer' do
|
24
24
|
buffer << 'foo'
|
25
|
-
buffer.length.should
|
25
|
+
buffer.length.should eq(3)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'is zero initially' do
|
29
|
-
buffer.length.should
|
29
|
+
buffer.length.should eq(0)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'is aliased as #size' do
|
33
33
|
buffer << 'foo'
|
34
|
-
buffer.size.should
|
34
|
+
buffer.size.should eq(3)
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'is aliased as #bytesize' do
|
38
38
|
buffer << 'foo'
|
39
|
-
buffer.bytesize.should
|
39
|
+
buffer.bytesize.should eq(3)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -67,19 +67,19 @@ module Ione
|
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'stores its bytes as binary' do
|
70
|
-
buffer.append('hällö').length.should
|
71
|
-
buffer.to_s.encoding.should
|
70
|
+
buffer.append('hällö').length.should eq(7)
|
71
|
+
buffer.to_s.encoding.should eq(::Encoding::BINARY)
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'handles appending with multibyte strings' do
|
75
75
|
buffer.append('hello')
|
76
76
|
buffer.append('würld')
|
77
|
-
buffer.to_s.should
|
77
|
+
buffer.to_s.should eq('hellowürld'.force_encoding(::Encoding::BINARY))
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'handles appending with another byte buffer' do
|
81
81
|
buffer.append('hello ').append(ByteBuffer.new('world'))
|
82
|
-
buffer.to_s.should
|
82
|
+
buffer.to_s.should eq('hello world')
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -105,7 +105,7 @@ module Ione
|
|
105
105
|
b2 = described_class.new
|
106
106
|
b1.append('foo')
|
107
107
|
b2.append('foo')
|
108
|
-
b1.should
|
108
|
+
b1.should eq(b2)
|
109
109
|
end
|
110
110
|
|
111
111
|
it 'is equal to another buffer when both are empty' do
|
@@ -121,7 +121,7 @@ module Ione
|
|
121
121
|
b2 = described_class.new
|
122
122
|
b1.append('foo')
|
123
123
|
b2.append('foo')
|
124
|
-
b1.hash.should
|
124
|
+
b1.hash.should eq(b2.hash)
|
125
125
|
end
|
126
126
|
|
127
127
|
it 'is not equal to the hash code of another buffer with other contents' do
|
@@ -135,26 +135,26 @@ module Ione
|
|
135
135
|
it 'is equal to the hash code of another buffer when both are empty' do
|
136
136
|
b1 = described_class.new
|
137
137
|
b2 = described_class.new
|
138
|
-
b1.hash.should
|
138
|
+
b1.hash.should eq(b2.hash)
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
142
142
|
describe '#to_s' do
|
143
143
|
it 'returns the bytes' do
|
144
|
-
buffer.append('hello world').to_s.should
|
144
|
+
buffer.append('hello world').to_s.should eq('hello world')
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
148
148
|
describe '#to_str' do
|
149
149
|
it 'returns the bytes' do
|
150
|
-
buffer.append('hello world').to_str.should
|
150
|
+
buffer.append('hello world').to_str.should eq('hello world')
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
154
154
|
describe '#inspect' do
|
155
155
|
it 'returns the bytes wrapped in ByteBuffer(...)' do
|
156
156
|
buffer.append("\xca\xfe")
|
157
|
-
buffer.inspect.should
|
157
|
+
buffer.inspect.should eq('#<Ione::ByteBuffer: "\xCA\xFE">')
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
@@ -162,12 +162,12 @@ module Ione
|
|
162
162
|
it 'discards the specified number of bytes from the front of the buffer' do
|
163
163
|
buffer.append('hello world')
|
164
164
|
buffer.discard(4)
|
165
|
-
buffer.should
|
165
|
+
buffer.should eq(ByteBuffer.new('o world'))
|
166
166
|
end
|
167
167
|
|
168
168
|
it 'returns the byte buffer' do
|
169
169
|
buffer.append('hello world')
|
170
|
-
buffer.discard(4).should
|
170
|
+
buffer.discard(4).should eq(ByteBuffer.new('o world'))
|
171
171
|
end
|
172
172
|
|
173
173
|
it 'raises an error if the number of bytes in the buffer is fewer than the number to discard' do
|
@@ -185,14 +185,14 @@ module Ione
|
|
185
185
|
describe '#read' do
|
186
186
|
it 'returns the specified number of bytes, as a string' do
|
187
187
|
buffer.append('hello')
|
188
|
-
buffer.read(4).should
|
188
|
+
buffer.read(4).should eq('hell')
|
189
189
|
end
|
190
190
|
|
191
191
|
it 'removes the bytes from the buffer' do
|
192
192
|
buffer.append('hello')
|
193
193
|
buffer.read(3)
|
194
|
-
buffer.should
|
195
|
-
buffer.read(2).should
|
194
|
+
buffer.should eq(ByteBuffer.new('lo'))
|
195
|
+
buffer.read(2).should eq('lo')
|
196
196
|
end
|
197
197
|
|
198
198
|
it 'raises an error if there are not enough bytes' do
|
@@ -208,22 +208,22 @@ module Ione
|
|
208
208
|
|
209
209
|
it 'returns a string with binary encoding' do
|
210
210
|
buffer.append('hello')
|
211
|
-
buffer.read(4).encoding.should
|
211
|
+
buffer.read(4).encoding.should eq(::Encoding::BINARY)
|
212
212
|
buffer.append('∆')
|
213
|
-
buffer.read(2).encoding.should
|
213
|
+
buffer.read(2).encoding.should eq(::Encoding::BINARY)
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
217
217
|
describe '#read_int' do
|
218
218
|
it 'returns the first four bytes interpreted as an int' do
|
219
219
|
buffer.append("\xca\xfe\xba\xbe\x01")
|
220
|
-
buffer.read_int.should
|
220
|
+
buffer.read_int.should eq(0xcafebabe)
|
221
221
|
end
|
222
222
|
|
223
223
|
it 'removes the bytes from the buffer' do
|
224
224
|
buffer.append("\xca\xfe\xba\xbe\x01")
|
225
225
|
buffer.read_int
|
226
|
-
buffer.should
|
226
|
+
buffer.should eq(ByteBuffer.new("\x01"))
|
227
227
|
end
|
228
228
|
|
229
229
|
it 'raises an error if there are not enough bytes' do
|
@@ -235,13 +235,13 @@ module Ione
|
|
235
235
|
describe '#read_short' do
|
236
236
|
it 'returns the first two bytes interpreted as a short' do
|
237
237
|
buffer.append("\xca\xfe\x01")
|
238
|
-
buffer.read_short.should
|
238
|
+
buffer.read_short.should eq(0xcafe)
|
239
239
|
end
|
240
240
|
|
241
241
|
it 'removes the bytes from the buffer' do
|
242
242
|
buffer.append("\xca\xfe\x01")
|
243
243
|
buffer.read_short
|
244
|
-
buffer.should
|
244
|
+
buffer.should eq(ByteBuffer.new("\x01"))
|
245
245
|
end
|
246
246
|
|
247
247
|
it 'raises an error if there are not enough bytes' do
|
@@ -253,14 +253,14 @@ module Ione
|
|
253
253
|
describe '#read_byte' do
|
254
254
|
it 'returns the first bytes interpreted as an int' do
|
255
255
|
buffer.append("\x10\x01")
|
256
|
-
buffer.read_byte.should
|
257
|
-
buffer.read_byte.should
|
256
|
+
buffer.read_byte.should eq(0x10)
|
257
|
+
buffer.read_byte.should eq(0x01)
|
258
258
|
end
|
259
259
|
|
260
260
|
it 'removes the byte from the buffer' do
|
261
261
|
buffer.append("\x10\x01")
|
262
262
|
buffer.read_byte
|
263
|
-
buffer.should
|
263
|
+
buffer.should eq(ByteBuffer.new("\x01"))
|
264
264
|
end
|
265
265
|
|
266
266
|
it 'raises an error if there are no bytes' do
|
@@ -269,8 +269,8 @@ module Ione
|
|
269
269
|
|
270
270
|
it 'can interpret the byte as signed' do
|
271
271
|
buffer.append("\x81\x02")
|
272
|
-
buffer.read_byte(true).should
|
273
|
-
buffer.read_byte(true).should
|
272
|
+
buffer.read_byte(true).should eq(-127)
|
273
|
+
buffer.read_byte(true).should eq(2)
|
274
274
|
end
|
275
275
|
end
|
276
276
|
|
@@ -278,14 +278,14 @@ module Ione
|
|
278
278
|
it 'changes the bytes at the specified location' do
|
279
279
|
buffer.append('foo bar')
|
280
280
|
buffer.update(4, 'baz')
|
281
|
-
buffer.to_s.should
|
281
|
+
buffer.to_s.should eq('foo baz')
|
282
282
|
end
|
283
283
|
|
284
284
|
it 'handles updates after a read' do
|
285
285
|
buffer.append('foo bar')
|
286
286
|
buffer.read(1)
|
287
287
|
buffer.update(3, 'baz')
|
288
|
-
buffer.to_s.should
|
288
|
+
buffer.to_s.should eq('oo baz')
|
289
289
|
end
|
290
290
|
|
291
291
|
it 'handles updates after multiple reads and appends' do
|
@@ -295,7 +295,7 @@ module Ione
|
|
295
295
|
buffer.update(4, 'baz')
|
296
296
|
buffer.append('yyyy')
|
297
297
|
buffer.read(1)
|
298
|
-
buffer.to_s.should
|
298
|
+
buffer.to_s.should eq('o bbazyyyy')
|
299
299
|
end
|
300
300
|
|
301
301
|
it 'returns itself' do
|
@@ -330,17 +330,70 @@ module Ione
|
|
330
330
|
x.bytesize.should be <= buffer.bytesize
|
331
331
|
buffer.to_str.should start_with(x)
|
332
332
|
end
|
333
|
+
|
334
|
+
it 'considers contents in the write when read buffer consumed' do
|
335
|
+
buffer.append('foo')
|
336
|
+
buffer.append('bar')
|
337
|
+
buffer.read_byte
|
338
|
+
buffer.discard(5)
|
339
|
+
buffer.append('hello')
|
340
|
+
x = buffer.cheap_peek
|
341
|
+
x.bytesize.should be > 0
|
342
|
+
x.bytesize.should be <= buffer.bytesize
|
343
|
+
buffer.to_str.should start_with(x)
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'returns nil in readonly mode when read buffer is consumed' do
|
347
|
+
buffer.append('foo')
|
348
|
+
buffer.append('bar')
|
349
|
+
buffer.read_byte
|
350
|
+
buffer.discard(5)
|
351
|
+
buffer.append('hello')
|
352
|
+
x = buffer.cheap_peek(true)
|
353
|
+
x.should be_nil
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
describe '#getbyte' do
|
358
|
+
it 'returns the nth byte interpreted as an int' do
|
359
|
+
buffer.append("\x80\x01")
|
360
|
+
expect(buffer.getbyte(0)).to eq(0x80)
|
361
|
+
expect(buffer.getbyte(1)).to eq(0x01)
|
362
|
+
end
|
363
|
+
|
364
|
+
it 'returns nil if there are no bytes' do
|
365
|
+
expect(buffer.getbyte(0)).to be_nil
|
366
|
+
end
|
367
|
+
|
368
|
+
it 'returns nil if the index goes beyond the buffer' do
|
369
|
+
buffer.append("\x80\x01")
|
370
|
+
expect(buffer.getbyte(2)).to be_nil
|
371
|
+
end
|
372
|
+
|
373
|
+
it 'handles interleaved writes' do
|
374
|
+
buffer.append("\x80\x01")
|
375
|
+
buffer.read_byte
|
376
|
+
buffer.append("\x81\x02")
|
377
|
+
expect(buffer.getbyte(0)).to eq(0x01)
|
378
|
+
expect(buffer.getbyte(1)).to eq(0x81)
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'can interpret the byte as signed' do
|
382
|
+
buffer.append("\x80\x02")
|
383
|
+
expect(buffer.getbyte(0, true)).to eq(-128)
|
384
|
+
expect(buffer.getbyte(1, true)).to eq(2)
|
385
|
+
end
|
333
386
|
end
|
334
387
|
|
335
388
|
describe '#index' do
|
336
389
|
it 'returns the first index of the specified substring' do
|
337
390
|
buffer.append('fizz buzz')
|
338
|
-
buffer.index('zz').should
|
391
|
+
buffer.index('zz').should eq(2)
|
339
392
|
end
|
340
393
|
|
341
394
|
it 'returns the first index of the specified substring, after the specified index' do
|
342
395
|
buffer.append('fizz buzz')
|
343
|
-
buffer.index('zz', 3).should
|
396
|
+
buffer.index('zz', 3).should eq(7)
|
344
397
|
end
|
345
398
|
|
346
399
|
it 'returns nil when the substring is not found' do
|
@@ -352,14 +405,14 @@ module Ione
|
|
352
405
|
buffer.append('foo bar')
|
353
406
|
buffer.read(1)
|
354
407
|
buffer.append(' baz baz')
|
355
|
-
buffer.index('baz', 8).should
|
408
|
+
buffer.index('baz', 8).should eq(11)
|
356
409
|
end
|
357
410
|
|
358
411
|
it 'returns the first index when the matching substring spans the read and write buffer' do
|
359
412
|
buffer.append('foo bar')
|
360
413
|
buffer.read(1)
|
361
414
|
buffer.append('bar barbar')
|
362
|
-
buffer.index('barbar', 0).should
|
415
|
+
buffer.index('barbar', 0).should eq(3)
|
363
416
|
end
|
364
417
|
|
365
418
|
it 'returns nil when the substring does not fit in the search space' do
|