cql-rb 2.0.0.pre0 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -2
  3. data/lib/cql.rb +8 -3
  4. data/lib/cql/client.rb +21 -356
  5. data/lib/cql/client/authenticators.rb +70 -0
  6. data/lib/cql/client/batch.rb +54 -0
  7. data/lib/cql/client/{asynchronous_client.rb → client.rb} +241 -6
  8. data/lib/cql/client/connector.rb +3 -2
  9. data/lib/cql/client/{asynchronous_prepared_statement.rb → prepared_statement.rb} +103 -0
  10. data/lib/cql/protocol.rb +1 -2
  11. data/lib/cql/protocol/cql_byte_buffer.rb +285 -0
  12. data/lib/cql/protocol/cql_protocol_handler.rb +3 -3
  13. data/lib/cql/protocol/frame_decoder.rb +3 -3
  14. data/lib/cql/protocol/frame_encoder.rb +2 -2
  15. data/lib/cql/protocol/request.rb +0 -2
  16. data/lib/cql/protocol/requests/auth_response_request.rb +2 -2
  17. data/lib/cql/protocol/requests/batch_request.rb +10 -10
  18. data/lib/cql/protocol/requests/credentials_request.rb +2 -2
  19. data/lib/cql/protocol/requests/execute_request.rb +13 -13
  20. data/lib/cql/protocol/requests/options_request.rb +2 -2
  21. data/lib/cql/protocol/requests/prepare_request.rb +2 -2
  22. data/lib/cql/protocol/requests/query_request.rb +13 -13
  23. data/lib/cql/protocol/requests/register_request.rb +2 -2
  24. data/lib/cql/protocol/requests/startup_request.rb +2 -2
  25. data/lib/cql/protocol/response.rb +2 -4
  26. data/lib/cql/protocol/responses/auth_challenge_response.rb +2 -2
  27. data/lib/cql/protocol/responses/auth_success_response.rb +2 -2
  28. data/lib/cql/protocol/responses/authenticate_response.rb +2 -2
  29. data/lib/cql/protocol/responses/detailed_error_response.rb +15 -15
  30. data/lib/cql/protocol/responses/error_response.rb +4 -4
  31. data/lib/cql/protocol/responses/event_response.rb +3 -3
  32. data/lib/cql/protocol/responses/prepared_result_response.rb +4 -4
  33. data/lib/cql/protocol/responses/raw_rows_result_response.rb +1 -1
  34. data/lib/cql/protocol/responses/ready_response.rb +1 -1
  35. data/lib/cql/protocol/responses/result_response.rb +3 -3
  36. data/lib/cql/protocol/responses/rows_result_response.rb +22 -22
  37. data/lib/cql/protocol/responses/schema_change_event_response.rb +2 -2
  38. data/lib/cql/protocol/responses/schema_change_result_response.rb +2 -2
  39. data/lib/cql/protocol/responses/set_keyspace_result_response.rb +2 -2
  40. data/lib/cql/protocol/responses/status_change_event_response.rb +2 -2
  41. data/lib/cql/protocol/responses/supported_response.rb +2 -2
  42. data/lib/cql/protocol/responses/void_result_response.rb +1 -1
  43. data/lib/cql/protocol/type_converter.rb +78 -81
  44. data/lib/cql/time_uuid.rb +6 -0
  45. data/lib/cql/uuid.rb +2 -1
  46. data/lib/cql/version.rb +1 -1
  47. data/spec/cql/client/batch_spec.rb +8 -8
  48. data/spec/cql/client/{asynchronous_client_spec.rb → client_spec.rb} +162 -0
  49. data/spec/cql/client/connector_spec.rb +13 -3
  50. data/spec/cql/client/{asynchronous_prepared_statement_spec.rb → prepared_statement_spec.rb} +148 -1
  51. data/spec/cql/client/request_runner_spec.rb +2 -2
  52. data/spec/cql/protocol/cql_byte_buffer_spec.rb +895 -0
  53. data/spec/cql/protocol/cql_protocol_handler_spec.rb +1 -1
  54. data/spec/cql/protocol/frame_decoder_spec.rb +14 -14
  55. data/spec/cql/protocol/frame_encoder_spec.rb +7 -7
  56. data/spec/cql/protocol/requests/auth_response_request_spec.rb +4 -4
  57. data/spec/cql/protocol/requests/batch_request_spec.rb +21 -21
  58. data/spec/cql/protocol/requests/credentials_request_spec.rb +2 -2
  59. data/spec/cql/protocol/requests/execute_request_spec.rb +13 -13
  60. data/spec/cql/protocol/requests/options_request_spec.rb +1 -1
  61. data/spec/cql/protocol/requests/prepare_request_spec.rb +2 -2
  62. data/spec/cql/protocol/requests/query_request_spec.rb +13 -13
  63. data/spec/cql/protocol/requests/register_request_spec.rb +2 -2
  64. data/spec/cql/protocol/requests/startup_request_spec.rb +4 -4
  65. data/spec/cql/protocol/responses/auth_challenge_response_spec.rb +5 -5
  66. data/spec/cql/protocol/responses/auth_success_response_spec.rb +5 -5
  67. data/spec/cql/protocol/responses/authenticate_response_spec.rb +3 -3
  68. data/spec/cql/protocol/responses/detailed_error_response_spec.rb +15 -15
  69. data/spec/cql/protocol/responses/error_response_spec.rb +5 -5
  70. data/spec/cql/protocol/responses/event_response_spec.rb +8 -8
  71. data/spec/cql/protocol/responses/prepared_result_response_spec.rb +7 -7
  72. data/spec/cql/protocol/responses/raw_rows_result_response_spec.rb +1 -1
  73. data/spec/cql/protocol/responses/ready_response_spec.rb +2 -2
  74. data/spec/cql/protocol/responses/result_response_spec.rb +16 -16
  75. data/spec/cql/protocol/responses/rows_result_response_spec.rb +21 -21
  76. data/spec/cql/protocol/responses/schema_change_event_response_spec.rb +3 -3
  77. data/spec/cql/protocol/responses/schema_change_result_response_spec.rb +3 -3
  78. data/spec/cql/protocol/responses/set_keyspace_result_response_spec.rb +2 -2
  79. data/spec/cql/protocol/responses/status_change_event_response_spec.rb +3 -3
  80. data/spec/cql/protocol/responses/supported_response_spec.rb +3 -3
  81. data/spec/cql/protocol/responses/topology_change_event_response_spec.rb +3 -3
  82. data/spec/cql/protocol/responses/void_result_response_spec.rb +2 -2
  83. data/spec/cql/protocol/type_converter_spec.rb +25 -13
  84. data/spec/cql/time_uuid_spec.rb +17 -4
  85. data/spec/cql/uuid_spec.rb +5 -1
  86. data/spec/integration/protocol_spec.rb +48 -42
  87. data/spec/spec_helper.rb +0 -1
  88. metadata +27 -39
  89. data/lib/cql/byte_buffer.rb +0 -177
  90. data/lib/cql/client/synchronous_client.rb +0 -79
  91. data/lib/cql/client/synchronous_prepared_statement.rb +0 -63
  92. data/lib/cql/future.rb +0 -515
  93. data/lib/cql/io.rb +0 -15
  94. data/lib/cql/io/connection.rb +0 -220
  95. data/lib/cql/io/io_reactor.rb +0 -349
  96. data/lib/cql/protocol/decoding.rb +0 -187
  97. data/lib/cql/protocol/encoding.rb +0 -114
  98. data/spec/cql/byte_buffer_spec.rb +0 -337
  99. data/spec/cql/client/synchronous_client_spec.rb +0 -170
  100. data/spec/cql/client/synchronous_prepared_statement_spec.rb +0 -155
  101. data/spec/cql/future_spec.rb +0 -737
  102. data/spec/cql/io/connection_spec.rb +0 -484
  103. data/spec/cql/io/io_reactor_spec.rb +0 -402
  104. data/spec/cql/protocol/decoding_spec.rb +0 -547
  105. data/spec/cql/protocol/encoding_spec.rb +0 -386
  106. data/spec/integration/io_spec.rb +0 -283
  107. data/spec/support/fake_server.rb +0 -106
@@ -1,402 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
-
6
- module Cql
7
- module Io
8
- describe IoReactor do
9
- let :reactor do
10
- described_class.new(protocol_handler_factory, selector: selector, clock: clock)
11
- end
12
-
13
- let :protocol_handler_factory do
14
- double(:protocol_handler_factory)
15
- end
16
-
17
- let! :selector do
18
- IoReactorSpec::FakeSelector.new
19
- end
20
-
21
- let :clock do
22
- double(:clock, now: 0)
23
- end
24
-
25
- describe '#start' do
26
- after do
27
- reactor.stop.value if reactor.running?
28
- end
29
-
30
- it 'returns a future that is resolved when the reactor has started' do
31
- reactor.start.value
32
- end
33
-
34
- it 'returns a future that resolves to the reactor' do
35
- reactor.start.value.should equal(reactor)
36
- end
37
-
38
- it 'is running after being started' do
39
- reactor.start.value
40
- reactor.should be_running
41
- end
42
-
43
- it 'cannot be started again once stopped' do
44
- reactor.start.value
45
- reactor.stop.value
46
- expect { reactor.start }.to raise_error(ReactorError)
47
- end
48
-
49
- it 'calls the selector' do
50
- called = false
51
- selector.handler { called = true; [[], [], []] }
52
- reactor.start.value
53
- await { called }
54
- reactor.stop.value
55
- called.should be_true, 'expected the selector to have been called'
56
- end
57
- end
58
-
59
- describe '#stop' do
60
- after do
61
- reactor.stop.value if reactor.running?
62
- end
63
-
64
- it 'returns a future that is resolved when the reactor has stopped' do
65
- reactor.start.value
66
- reactor.stop.value
67
- end
68
-
69
- it 'returns a future which resolves to the reactor' do
70
- reactor.start.value
71
- reactor.stop.value.should equal(reactor)
72
- end
73
-
74
- it 'is not running after being stopped' do
75
- reactor.start.value
76
- reactor.stop.value
77
- reactor.should_not be_running
78
- end
79
-
80
- it 'closes all sockets' do
81
- connection = nil
82
- protocol_handler_factory.stub(:new) do |sh, _|
83
- connection = sh
84
- double(:protocol_handler)
85
- end
86
- reactor.start.value
87
- reactor.connect('example.com', 9999, 5)
88
- reactor.stop.value
89
- connection.should be_closed
90
- end
91
-
92
- it 'cancels all active timers' do
93
- reactor.start.value
94
- clock.stub(:now).and_return(1)
95
- expired_timer = reactor.schedule_timer(1)
96
- active_timer1 = reactor.schedule_timer(999)
97
- active_timer2 = reactor.schedule_timer(111)
98
- expired_timer.should_not_receive(:fail)
99
- clock.stub(:now).and_return(2)
100
- await { expired_timer.completed? }
101
- reactor.stop.value
102
- active_timer1.should be_failed
103
- active_timer2.should be_failed
104
- end
105
- end
106
-
107
- describe '#on_error' do
108
- before do
109
- selector.handler { raise 'Blurgh' }
110
- end
111
-
112
- it 'calls the listeners when the reactor crashes' do
113
- error = nil
114
- reactor.on_error { |e| error = e }
115
- reactor.start
116
- await { error }
117
- error.message.should == 'Blurgh'
118
- end
119
-
120
- it 'calls the listener immediately when the reactor has already crashed' do
121
- error = nil
122
- reactor.start.value
123
- await { !reactor.running? }
124
- reactor.on_error { |e| error = e }
125
- await { error }
126
- end
127
-
128
- it 'ignores errors raised by listeners' do
129
- called = false
130
- reactor.on_error { raise 'Blurgh' }
131
- reactor.on_error { called = true }
132
- reactor.start
133
- await { called }
134
- called.should be_true, 'expected all close listeners to have been called'
135
- end
136
- end
137
-
138
- describe '#connect' do
139
- let :protocol_handler do
140
- double(:protocol_handler)
141
- end
142
-
143
- before do
144
- protocol_handler_factory.stub(:call) do |connection, _|
145
- connection.to_io.stub(:connect_nonblock)
146
- protocol_handler.stub(:connection).and_return(connection)
147
- protocol_handler
148
- end
149
- end
150
-
151
- before do
152
- selector.handler do |readables, writables, _, _|
153
- writables.each do |writable|
154
- fake_connected(writable)
155
- end
156
- [[], writables, []]
157
- end
158
- end
159
-
160
- def fake_connected(connection)
161
- connection.to_io.stub(:connect_nonblock)
162
- end
163
-
164
- after do
165
- reactor.stop if reactor.running?
166
- end
167
-
168
- it 'calls #call on the protocol handler factory with the connection and the reactor itself' do
169
- reactor.start.value
170
- reactor.connect('example.com', 9999, 5).value
171
- protocol_handler_factory.should have_received(:call).with(an_instance_of(Connection), reactor)
172
- end
173
-
174
- it 'calls #new on the protocol handler factory with the connection and the reactor itself' do
175
- protocol_handler_factory.stub(:new)
176
- reactor.start.value
177
- reactor.connect('example.com', 9999, 5).value
178
- protocol_handler_factory.should have_received(:new).with(an_instance_of(Connection), reactor)
179
- end
180
-
181
- it 'returns a future that resolves to a new protocol handler' do
182
- reactor.start.value
183
- f = reactor.connect('example.com', 9999, 5)
184
- f.value.should equal(protocol_handler)
185
- end
186
-
187
- it 'returns a new protocol handler which wraps a socket handler' do
188
- reactor.start.value
189
- protocol_handler = reactor.connect('example.com', 9999, 5).value
190
- protocol_handler.connection.should_not be_nil
191
- protocol_handler.connection.host.should == 'example.com'
192
- protocol_handler.connection.port.should == 9999
193
- protocol_handler.connection.connection_timeout.should == 5
194
- end
195
- end
196
-
197
- describe '#schedule_timer' do
198
- before do
199
- reactor.start.value
200
- end
201
-
202
- after do
203
- reactor.stop.value
204
- end
205
-
206
- it 'returns a future that is resolved after the specified duration' do
207
- clock.stub(:now).and_return(1)
208
- f = reactor.schedule_timer(0.1)
209
- clock.stub(:now).and_return(1.1)
210
- await { f.resolved? }
211
- end
212
- end
213
-
214
- describe '#to_s' do
215
- context 'returns a string that' do
216
- it 'includes the class name' do
217
- reactor.to_s.should include('Cql::Io::IoReactor')
218
- end
219
-
220
- it 'includes a list of its connections' do
221
- reactor.to_s.should include('@connections=[')
222
- reactor.to_s.should include('#<Cql::Io::Unblocker>')
223
- end
224
- end
225
- end
226
- end
227
-
228
- describe IoLoopBody do
229
- let :loop_body do
230
- described_class.new(selector: selector, clock: clock)
231
- end
232
-
233
- let :selector do
234
- double(:selector)
235
- end
236
-
237
- let :clock do
238
- double(:clock, now: 0)
239
- end
240
-
241
- let :socket do
242
- double(:socket, connected?: false, connecting?: false, writable?: false, closed?: false)
243
- end
244
-
245
- describe '#tick' do
246
- before do
247
- loop_body.add_socket(socket)
248
- end
249
-
250
- it 'passes connected sockets as readables to the selector' do
251
- socket.stub(:connected?).and_return(true)
252
- selector.should_receive(:select).with([socket], anything, anything, anything).and_return([nil, nil, nil])
253
- loop_body.tick
254
- end
255
-
256
- it 'passes writable sockets as writable to the selector' do
257
- socket.stub(:writable?).and_return(true)
258
- selector.should_receive(:select).with(anything, [socket], anything, anything).and_return([nil, nil, nil])
259
- loop_body.tick
260
- end
261
-
262
- it 'passes connecting sockets as writable to the selector' do
263
- socket.stub(:connecting?).and_return(true)
264
- socket.stub(:connect)
265
- selector.should_receive(:select).with(anything, [socket], anything, anything).and_return([nil, nil, nil])
266
- loop_body.tick
267
- end
268
-
269
- it 'filters out closed sockets' do
270
- socket.stub(:closed?).and_return(true)
271
- selector.should_receive(:select).with([], [], anything, anything).and_return([nil, nil, nil])
272
- loop_body.tick
273
- socket.stub(:connected?).and_return(true)
274
- selector.should_receive(:select).with([], [], anything, anything).and_return([nil, nil, nil])
275
- loop_body.tick
276
- end
277
-
278
- it 'calls #read on all readable sockets returned by the selector' do
279
- socket.stub(:connected?).and_return(true)
280
- socket.should_receive(:read)
281
- selector.stub(:select) do |r, w, _, _|
282
- [[socket], nil, nil]
283
- end
284
- loop_body.tick
285
- end
286
-
287
- it 'calls #connect on all connecting sockets' do
288
- socket.stub(:connecting?).and_return(true)
289
- socket.should_receive(:connect)
290
- selector.stub(:select).and_return([nil, nil, nil])
291
- loop_body.tick
292
- end
293
-
294
- it 'calls #flush on all writable sockets returned by the selector' do
295
- socket.stub(:writable?).and_return(true)
296
- socket.should_receive(:flush)
297
- selector.stub(:select) do |r, w, _, _|
298
- [nil, [socket], nil]
299
- end
300
- loop_body.tick
301
- end
302
-
303
- it 'allows the caller to specify a custom timeout' do
304
- selector.should_receive(:select).with(anything, anything, anything, 99).and_return([[], [], []])
305
- loop_body.tick(99)
306
- end
307
-
308
- it 'completes timers that have expired' do
309
- selector.stub(:select).and_return([nil, nil, nil])
310
- clock.stub(:now).and_return(1)
311
- promise = Promise.new
312
- loop_body.schedule_timer(1, promise)
313
- loop_body.tick
314
- promise.future.should_not be_completed
315
- clock.stub(:now).and_return(2)
316
- loop_body.tick
317
- promise.future.should be_completed
318
- end
319
-
320
- it 'clears out timers that have expired' do
321
- selector.stub(:select).and_return([nil, nil, nil])
322
- clock.stub(:now).and_return(1)
323
- promise = Promise.new
324
- loop_body.schedule_timer(1, promise)
325
- clock.stub(:now).and_return(2)
326
- loop_body.tick
327
- promise.future.should be_completed
328
- promise.should_not_receive(:fulfill)
329
- loop_body.tick
330
- end
331
- end
332
-
333
- describe '#close_sockets' do
334
- it 'closes all sockets' do
335
- socket1 = double(:socket1, closed?: false)
336
- socket2 = double(:socket2, closed?: false)
337
- socket1.should_receive(:close)
338
- socket2.should_receive(:close)
339
- loop_body.add_socket(socket1)
340
- loop_body.add_socket(socket2)
341
- loop_body.close_sockets
342
- end
343
-
344
- it 'closes all sockets, even when one of them raises an error' do
345
- socket1 = double(:socket1, closed?: false)
346
- socket2 = double(:socket2, closed?: false)
347
- socket1.stub(:close).and_raise('Blurgh')
348
- socket2.should_receive(:close)
349
- loop_body.add_socket(socket1)
350
- loop_body.add_socket(socket2)
351
- loop_body.close_sockets
352
- end
353
-
354
- it 'does not close already closed sockets' do
355
- socket.stub(:closed?).and_return(true)
356
- socket.should_not_receive(:close)
357
- loop_body.add_socket(socket)
358
- loop_body.close_sockets
359
- end
360
- end
361
-
362
- describe '#cancel_timers' do
363
- before do
364
- selector.stub(:select).and_return([nil, nil, nil])
365
- end
366
-
367
- it 'fails all active timers with a CancelledError' do
368
- p1 = Promise.new
369
- p2 = Promise.new
370
- p3 = Promise.new
371
- clock.stub(:now).and_return(1)
372
- loop_body.schedule_timer(1, p1)
373
- loop_body.schedule_timer(3, p2)
374
- loop_body.schedule_timer(3, p3)
375
- clock.stub(:now).and_return(2)
376
- loop_body.tick
377
- loop_body.cancel_timers
378
- p1.future.should be_completed
379
- p2.future.should be_failed
380
- p3.future.should be_failed
381
- expect { p3.future.value }.to raise_error(CancelledError)
382
- end
383
- end
384
- end
385
- end
386
- end
387
-
388
- module IoReactorSpec
389
- class FakeSelector
390
- def initialize
391
- handler { [[], [], []] }
392
- end
393
-
394
- def handler(&body)
395
- @body = body
396
- end
397
-
398
- def select(*args)
399
- @body.call(*args)
400
- end
401
- end
402
- end
@@ -1,547 +0,0 @@
1
- # encoding: ascii-8bit
2
-
3
- require 'spec_helper'
4
-
5
-
6
- module Cql
7
- module Protocol
8
- describe Decoding do
9
- describe '#read_byte!' do
10
- let :buffer do
11
- ByteBuffer.new("\xab")
12
- end
13
-
14
- it 'decodes a raw byte' do
15
- Decoding.read_byte!(buffer).should == 0xab
16
- end
17
-
18
- it 'consumes the byte' do
19
- Decoding.read_byte!(buffer)
20
- buffer.should be_empty
21
- end
22
-
23
- it 'raises an error when there is no byte available' do
24
- expect { Decoding.read_byte!(ByteBuffer.new) }.to raise_error(DecodingError)
25
- end
26
- end
27
-
28
- describe '#read_varint!' do
29
- it 'decodes a variable length integer' do
30
- buffer = ByteBuffer.new("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a[")
31
- Decoding.read_varint!(buffer, 17).should == 1231312312331283012830129382342342412123
32
- end
33
-
34
- it 'decodes a negative variable length integer' do
35
- buffer = ByteBuffer.new("\xC9v\x8D:\x86")
36
- Decoding.read_varint!(buffer, 5).should == -234234234234
37
- end
38
-
39
- it 'decodes an unsigned variable length integer' do
40
- buffer = ByteBuffer.new("\xC9v\x8D:\x86")
41
- Decoding.read_varint!(buffer, 5, false).should == 865277393542
42
- end
43
-
44
- it 'consumes the bytes' do
45
- buffer = ByteBuffer.new("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a[\x01\x02\x03")
46
- Decoding.read_varint!(buffer, 17)
47
- buffer.should eql_bytes("\x01\x02\x03")
48
- end
49
-
50
- it 'raises an error when there is not enough bytes available' do
51
- buffer = ByteBuffer.new("\xC9v\x8D:")
52
- expect { Decoding.read_varint!(buffer, 7) }.to raise_error(DecodingError)
53
- end
54
- end
55
-
56
- describe '#read_decimal!' do
57
- let :buffer do
58
- ByteBuffer.new("\x00\x00\x00\x12\r'\xFDI\xAD\x80f\x11g\xDCfV\xAA")
59
- end
60
-
61
- it 'decodes a decimal to a BigDecimal' do
62
- Decoding.read_decimal!(buffer).should == BigDecimal.new('1042342234234.123423435647768234')
63
- end
64
-
65
- it 'decodes a negative decimal' do
66
- buffer = ByteBuffer.new("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V")
67
- Decoding.read_decimal!(buffer).should == BigDecimal.new('-1042342234234.123423435647768234')
68
- end
69
-
70
- it 'decodes a positive decimal with only fractions' do
71
- buffer = ByteBuffer.new("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o")
72
- Decoding.read_decimal!(buffer).should == BigDecimal.new('0.0012095473475870063')
73
- end
74
-
75
- it 'decodes a negative decimal with only fractions' do
76
- buffer = ByteBuffer.new("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91")
77
- Decoding.read_decimal!(buffer).should == BigDecimal.new('-0.0012095473475870063')
78
- end
79
-
80
- it 'consumes the bytes' do
81
- buffer << 'HELLO'
82
- Decoding.read_decimal!(buffer, buffer.length - 5)
83
- buffer.should eql_bytes('HELLO')
84
- end
85
-
86
- it 'defaults to using the buffer length' do
87
- Decoding.read_decimal!(buffer.dup).should == Decoding.read_decimal!(buffer, buffer.length)
88
- end
89
-
90
- it 'raises an error when there is not enough bytes available' do
91
- b = ByteBuffer.new(buffer.read(3))
92
- expect { Decoding.read_decimal!(b, 7) }.to raise_error(DecodingError)
93
- end
94
- end
95
-
96
- describe '#read_long!' do
97
- it 'decodes a positive long' do
98
- buffer = ByteBuffer.new("\x00\x00\xca\xfe\xba\xbe\x00\x00")
99
- Decoding.read_long!(buffer).should == 0x0000cafebabe0000
100
- end
101
-
102
- it 'decodes a negative long' do
103
- buffer = ByteBuffer.new("\xff\xee\xdd\xcc\xbb\xaa\x99\x88")
104
- Decoding.read_long!(buffer).should == 0xffeeddccbbaa9988 - 0x10000000000000000
105
- end
106
-
107
- it 'consumes the bytes' do
108
- buffer = ByteBuffer.new("\xca\xfe\xba\xbe\xca\xfe\xba\xbe\xca\xfe\xba\xbe")
109
- Decoding.read_long!(buffer)
110
- buffer.should eql_bytes("\xca\xfe\xba\xbe")
111
- end
112
-
113
- it 'raises an error when there is not enough bytes available' do
114
- buffer = ByteBuffer.new("\xca\xfe\xba\xbe\x00")
115
- expect { Decoding.read_long!(buffer) }.to raise_error(DecodingError)
116
- end
117
- end
118
-
119
- describe '#read_double!' do
120
- it 'decodes a double' do
121
- buffer = ByteBuffer.new("@\xC3\x88\x0F\xC2\x7F\x9DU")
122
- Decoding.read_double!(buffer).should == 10000.123123123
123
- end
124
-
125
- it 'consumes the bytes' do
126
- buffer = ByteBuffer.new("@\xC3\x88\x0F\xC2\x7F\x9DUxyz")
127
- Decoding.read_double!(buffer)
128
- buffer.should eql_bytes('xyz')
129
- end
130
-
131
- it 'raises an error when there is not enough bytes available' do
132
- buffer = ByteBuffer.new("@\xC3\x88\x0F")
133
- expect { Decoding.read_double!(buffer) }.to raise_error(DecodingError)
134
- end
135
- end
136
-
137
- describe '#read_float!' do
138
- it 'decodes a float' do
139
- buffer = ByteBuffer.new("AB\x14{")
140
- Decoding.read_float!(buffer).should be_within(0.00001).of(12.13)
141
- end
142
-
143
- it 'consumes the bytes' do
144
- buffer = ByteBuffer.new("AB\x14{xyz")
145
- Decoding.read_float!(buffer)
146
- buffer.should eql_bytes('xyz')
147
- end
148
-
149
- it 'raises an error when there is not enough bytes available' do
150
- buffer = ByteBuffer.new("\x0F")
151
- expect { Decoding.read_float!(buffer) }.to raise_error(DecodingError)
152
- end
153
- end
154
-
155
- describe '#read_int!' do
156
- let :buffer do
157
- ByteBuffer.new("\x00\xff\x00\xff")
158
- end
159
-
160
- it 'decodes a positive int' do
161
- Decoding.read_int!(buffer).should == 0x00ff00ff
162
- end
163
-
164
- it 'decodes a negative int' do
165
- buffer = ByteBuffer.new("\xff\xee\xdd\xcc")
166
- Decoding.read_int!(buffer).should == 0xffeeddcc - 0x100000000
167
- end
168
-
169
- it 'consumes the bytes' do
170
- buffer << "\xab\xcd"
171
- Decoding.read_int!(buffer)
172
- buffer.should eql_bytes("\xab\xcd")
173
- end
174
-
175
- it 'raises an error when there are not enough bytes in the buffer' do
176
- buffer = ByteBuffer.new("\x01\xab")
177
- expect { Decoding.read_int!(buffer) }.to raise_error(DecodingError)
178
- end
179
- end
180
-
181
- describe '#read_short!' do
182
- let :buffer do
183
- ByteBuffer.new("\x00\x02")
184
- end
185
-
186
- it 'decodes a short' do
187
- Decoding.read_short!(buffer).should == 2
188
- end
189
-
190
- it 'consumes the bytes' do
191
- buffer << "\xff\xff"
192
- Decoding.read_short!(buffer)
193
- buffer.should eql_bytes("\xff\xff")
194
- end
195
-
196
- it 'raises an error when there are not enough bytes in the buffer' do
197
- buffer = ByteBuffer.new("\x01")
198
- expect { Decoding.read_short!(buffer) }.to raise_error(DecodingError)
199
- end
200
- end
201
-
202
- describe '#read_string!' do
203
- let :buffer do
204
- ByteBuffer.new("\x00\x0bhej och hå")
205
- end
206
-
207
- it 'decodes a string' do
208
- Decoding.read_string!(buffer).should == 'hej och hå'.force_encoding(::Encoding::UTF_8)
209
- end
210
-
211
- it 'decodes a string as UTF-8' do
212
- Decoding.read_string!(buffer).encoding.should == ::Encoding::UTF_8
213
- end
214
-
215
- it 'decodes an empty string' do
216
- buffer = ByteBuffer.new("\x00\x00")
217
- Decoding.read_string!(buffer).should be_empty
218
- end
219
-
220
- it 'consumes the bytes' do
221
- buffer << "\xff\xff"
222
- Decoding.read_string!(buffer)
223
- buffer.should eql_bytes("\xff\xff")
224
- end
225
-
226
- it 'raises an error when there are not enough bytes in the buffer' do
227
- b = ByteBuffer.new(buffer.read(5))
228
- expect { Decoding.read_string!(b) }.to raise_error(DecodingError)
229
- end
230
- end
231
-
232
- describe '#read_long_string!' do
233
- let :buffer do
234
- ByteBuffer.new("\x00\x01\x00\00" << ('x' * 0x10000))
235
- end
236
-
237
- it 'decodes a string' do
238
- Decoding.read_long_string!(buffer.dup).should start_with('xxx')
239
- Decoding.read_long_string!(buffer).length.should == 0x10000
240
- end
241
-
242
- it 'decodes a string as UTF-8' do
243
- Decoding.read_long_string!(buffer).encoding.should == ::Encoding::UTF_8
244
- end
245
-
246
- it 'consumes the bytes' do
247
- buffer << "\xff\xff"
248
- Decoding.read_long_string!(buffer)
249
- buffer.should eql_bytes("\xff\xff")
250
- end
251
-
252
- it 'raises an error when there are not enough bytes in the buffer' do
253
- b = ByteBuffer.new(buffer.read(246))
254
- expect { Decoding.read_long_string!(b) }.to raise_error(DecodingError)
255
- end
256
- end
257
-
258
- describe '#read_uuid!' do
259
- let :buffer do
260
- ByteBuffer.new("\xA4\xA7\t\x00$\xE1\x11\xDF\x89$\x00\x1F\xF3Y\x17\x11")
261
- end
262
-
263
- it 'decodes a UUID as a Cql::Uuid' do
264
- Decoding.read_uuid!(buffer).should == Uuid.new('a4a70900-24e1-11df-8924-001ff3591711')
265
- end
266
-
267
- it 'decodes a UUID as a Cql::TimeUuid' do
268
- uuid = Decoding.read_uuid!(buffer, TimeUuid)
269
- uuid.should == TimeUuid.new('a4a70900-24e1-11df-8924-001ff3591711')
270
- uuid.should be_a(TimeUuid)
271
- end
272
-
273
- it 'consumes the bytes' do
274
- Decoding.read_uuid!(buffer)
275
- buffer.should be_empty
276
- end
277
-
278
- it 'raises an error when there a not enough bytes in the buffer' do
279
- b = ByteBuffer.new(buffer.discard(2).read(5))
280
- expect { Decoding.read_uuid!(b) }.to raise_error(DecodingError)
281
- end
282
- end
283
-
284
- describe '#read_string_list!' do
285
- let :buffer do
286
- ByteBuffer.new("\x00\x02\x00\x05hello\x00\x05world")
287
- end
288
-
289
- it 'decodes a string list' do
290
- Decoding.read_string_list!(buffer).should == %w[hello world]
291
- end
292
-
293
- it 'decodes an empty string list' do
294
- buffer = ByteBuffer.new("\x00\x00")
295
- Decoding.read_string_list!(buffer).should == []
296
- end
297
-
298
- it 'consumes the bytes' do
299
- buffer << "\xff\xff"
300
- Decoding.read_string_list!(buffer)
301
- buffer.should eql_bytes("\xff\xff")
302
- end
303
-
304
- it 'raises an error when there are not enough bytes in the buffer' do
305
- b = ByteBuffer.new(buffer.read(13))
306
- expect { Decoding.read_string_list!(b) }.to raise_error(DecodingError)
307
- end
308
- end
309
-
310
- describe '#read_bytes!' do
311
- let :buffer do
312
- ByteBuffer.new("\x00\x01\x00\x00" << ("\x42" * 0x10000))
313
- end
314
-
315
- it 'decodes a byte array' do
316
- Decoding.read_bytes!(buffer).should eql_bytes("\x42" * 0x10000)
317
- end
318
-
319
- it 'decodes an empty byte array' do
320
- buffer = ByteBuffer.new("\x00\x00\x00\x00")
321
- Decoding.read_bytes!(buffer).should be_empty
322
- end
323
-
324
- it 'decodes null' do
325
- buffer = ByteBuffer.new("\x80\x00\x00\x00")
326
- Decoding.read_bytes!(buffer).should be_nil
327
- end
328
-
329
- it 'consumes the bytes' do
330
- buffer << "\xab\xcd"
331
- Decoding.read_bytes!(buffer)
332
- buffer.should eql_bytes("\xab\xcd")
333
- end
334
-
335
- it 'raises an error when there are not enough bytes in the buffer' do
336
- b = ByteBuffer.new(buffer.read(10))
337
- expect { Decoding.read_bytes!(b) }.to raise_error(DecodingError)
338
- end
339
- end
340
-
341
- describe '#read_short_bytes!' do
342
- let :buffer do
343
- ByteBuffer.new("\x01\x00" << ("\x42" * 0x100))
344
- end
345
-
346
- it 'decodes a byte array' do
347
- Decoding.read_short_bytes!(buffer).should eql_bytes("\x42" * 0x100)
348
- end
349
-
350
- it 'decodes an empty byte array' do
351
- buffer = ByteBuffer.new("\x00\x00\x00\x00")
352
- Decoding.read_short_bytes!(buffer).should be_empty
353
- end
354
-
355
- it 'decodes null' do
356
- buffer = ByteBuffer.new("\x80\x00")
357
- Decoding.read_short_bytes!(buffer).should be_nil
358
- end
359
-
360
- it 'consumes the bytes' do
361
- buffer << "\xab\xcd"
362
- Decoding.read_short_bytes!(buffer)
363
- buffer.should eql_bytes("\xab\xcd")
364
- end
365
-
366
- it 'raises an error when there are not enough bytes in the buffer' do
367
- b = ByteBuffer.new(buffer.read(10))
368
- expect { Decoding.read_short_bytes!(b) }.to raise_error(DecodingError)
369
- end
370
- end
371
-
372
- describe '#read_option!' do
373
- it 'decodes an option ID and value with instructions from a block' do
374
- buffer = ByteBuffer.new("\x00\x01\x00\x03foo")
375
- id, value = Decoding.read_option!(buffer) do |id, buffer|
376
- Decoding.read_string!(buffer)
377
- end
378
- id.should == 1
379
- value.should == 'foo'
380
- end
381
-
382
- it 'decodes an option ID and nil value when there is no block' do
383
- buffer = ByteBuffer.new("\xaa\xbb")
384
- id, value = Decoding.read_option!(buffer)
385
- id.should == 0xaabb
386
- value.should be_nil
387
- end
388
-
389
- it 'consumes the bytes' do
390
- buffer = ByteBuffer.new("\x00\x01\x00\x03\xab")
391
- id, value = Decoding.read_option!(buffer) do |id, buffer|
392
- Decoding.read_short!(buffer)
393
- end
394
- buffer.should eql_bytes("\xab")
395
- end
396
-
397
- it 'raises an error when there are not enough bytes in the buffer' do
398
- buffer = ByteBuffer.new("\xaa")
399
- expect { Decoding.read_option!(buffer) }.to raise_error(DecodingError)
400
- end
401
- end
402
-
403
- describe '#read_inet!' do
404
- it 'decodes an IPv4 + port pair' do
405
- buffer = ByteBuffer.new("\x04\x00\x00\x00\x00\x00\x00#R")
406
- ip_addr, port = Decoding.read_inet!(buffer)
407
- ip_addr.should == IPAddr.new('0.0.0.0')
408
- port.should == 9042
409
- end
410
-
411
- it 'decodes an IPv6 + port pair' do
412
- buffer = ByteBuffer.new("\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00#R")
413
- ip_addr, port = Decoding.read_inet!(buffer)
414
- ip_addr.should == IPAddr.new('::1')
415
- port.should == 9042
416
- end
417
-
418
- it 'consumes the bytes' do
419
- buffer = ByteBuffer.new("\x04\x00\x00\x00\x00\x00\x00#R\xff\xaa")
420
- Decoding.read_inet!(buffer)
421
- buffer.should eql_bytes("\xff\xaa")
422
- end
423
-
424
- it 'raises an error when there are not enough bytes in the buffer' do
425
- buffer1 = ByteBuffer.new("\x04\x00\x00\x00\x00\x00\x00")
426
- expect { Decoding.read_inet!(buffer1) }.to raise_error(DecodingError)
427
- buffer2 = ByteBuffer.new("\x04\x00\x00\x00")
428
- expect { Decoding.read_inet!(buffer2) }.to raise_error(DecodingError)
429
- end
430
- end
431
-
432
- describe '#read_consistency!' do
433
- it 'decodes ANY' do
434
- buffer = ByteBuffer.new("\x00\x00")
435
- Decoding.read_consistency!(buffer).should == :any
436
- end
437
-
438
- it 'decodes ONE' do
439
- buffer = ByteBuffer.new("\x00\x01")
440
- Decoding.read_consistency!(buffer).should == :one
441
- end
442
-
443
- it 'decodes TWO' do
444
- buffer = ByteBuffer.new("\x00\x02")
445
- Decoding.read_consistency!(buffer).should == :two
446
- end
447
-
448
- it 'decodes THREE' do
449
- buffer = ByteBuffer.new("\x00\x03")
450
- Decoding.read_consistency!(buffer).should == :three
451
- end
452
-
453
- it 'decodes QUORUM' do
454
- buffer = ByteBuffer.new("\x00\x04")
455
- Decoding.read_consistency!(buffer).should == :quorum
456
- end
457
-
458
- it 'decodes ALL' do
459
- buffer = ByteBuffer.new("\x00\x05")
460
- Decoding.read_consistency!(buffer).should == :all
461
- end
462
-
463
- it 'decodes LOCAL_QUORUM' do
464
- buffer = ByteBuffer.new("\x00\x06")
465
- Decoding.read_consistency!(buffer).should == :local_quorum
466
- end
467
-
468
- it 'decodes EACH_QUORUM' do
469
- buffer = ByteBuffer.new("\x00\x07")
470
- Decoding.read_consistency!(buffer).should == :each_quorum
471
- end
472
-
473
- it 'decodes SERIAL' do
474
- buffer = ByteBuffer.new("\x00\x08")
475
- Decoding.read_consistency!(buffer).should == :serial
476
- end
477
-
478
- it 'decodes LOCAL_SERIAL' do
479
- buffer = ByteBuffer.new("\x00\x09")
480
- Decoding.read_consistency!(buffer).should == :local_serial
481
- end
482
-
483
- it 'decodes LOCAL_ONE' do
484
- buffer = ByteBuffer.new("\x00\x0a")
485
- Decoding.read_consistency!(buffer).should == :local_one
486
- end
487
-
488
- it 'raises an exception for an unknown consistency' do
489
- expect { Decoding.read_consistency!(ByteBuffer.new("\xff\xff")) }.to raise_error(DecodingError)
490
- expect { Decoding.read_consistency!(ByteBuffer.new("\x00\x0f")) }.to raise_error(DecodingError)
491
- end
492
- end
493
-
494
- describe '#read_string_map!' do
495
- let :buffer do
496
- ByteBuffer.new("\x00\x02\x00\x05hello\x00\x05world\x00\x03foo\x00\x03bar")
497
- end
498
-
499
- it 'decodes a string multimap' do
500
- Decoding.read_string_map!(buffer).should == {'hello' => 'world', 'foo' => 'bar'}
501
- end
502
-
503
- it 'decodes an empty string map' do
504
- buffer = ByteBuffer.new("\x00\x00")
505
- Decoding.read_string_map!(buffer).should == {}
506
- end
507
-
508
- it 'consumes the bytes' do
509
- buffer << "\xff"
510
- Decoding.read_string_map!(buffer)
511
- buffer.should eql_bytes("\xff")
512
- end
513
-
514
- it 'raises an error when there are not enough bytes in the buffer' do
515
- b = ByteBuffer.new(buffer.read(20))
516
- expect { Decoding.read_string_map!(b) }.to raise_error(DecodingError)
517
- end
518
- end
519
-
520
- describe '#read_string_multimap!' do
521
- let :buffer do
522
- ByteBuffer.new("\x00\x02\x00\x0bCQL_VERSION\x00\x01\x00\x053.0.0\x00\x0bCOMPRESSION\x00\x02\x00\x06snappy\x00\x04gzip")
523
- end
524
-
525
- it 'decodes a string multimap' do
526
- Decoding.read_string_multimap!(buffer).should == {'CQL_VERSION' => ['3.0.0'], 'COMPRESSION' => ['snappy', 'gzip']}
527
- end
528
-
529
- it 'decodes an empty string multimap' do
530
- buffer = ByteBuffer.new("\x00\x00")
531
- Decoding.read_string_multimap!(buffer).should == {}
532
- end
533
-
534
- it 'consumes the bytes' do
535
- buffer << "\xff"
536
- Decoding.read_string_multimap!(buffer)
537
- buffer.should eql_bytes("\xff")
538
- end
539
-
540
- it 'raises an error when there are not enough bytes in the buffer' do
541
- b = ByteBuffer.new(buffer.read(40))
542
- expect { Decoding.read_string_multimap!(b) }.to raise_error(DecodingError)
543
- end
544
- end
545
- end
546
- end
547
- end