forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_spec.rb
More file actions
365 lines (301 loc) · 9.54 KB
/
session_spec.rb
File metadata and controls
365 lines (301 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# frozen_string_literal: true
require 'spec_helper'
describe Mongo::Session do
require_topology :replica_set, :sharded
let(:session) do
authorized_client.start_session(options)
end
let(:options) do
{}
end
describe '#initialize' do
context 'when options are provided' do
it 'duplicates and freezes the options' do
expect(session.options).not_to be(options)
expect(session.options.frozen?).to be(true)
end
end
it 'sets a server session with an id' do
expect(session.session_id).to be_a(BSON::Document)
end
it 'sets the cluster time to nil' do
expect(session.cluster_time).to be_nil
end
it 'sets the cluster' do
expect(session.cluster).to be(authorized_client.cluster)
end
end
describe '#inspect' do
it 'includes the Ruby object_id in the formatted string' do
expect(session.inspect).to include(session.object_id.to_s)
end
it 'includes the session_id in the formatted string' do
expect(session.inspect).to include(session.session_id.to_s)
end
context 'when options are provided' do
let(:options) do
{ causal_consistency: true }
end
it 'includes the options in the formatted string' do
expect(session.inspect).to include({ implicit: false,
causal_consistency: true }.to_s)
end
end
end
describe '#advance_cluster_time' do
let(:new_cluster_time) do
{ 'clusterTime' => BSON::Timestamp.new(0, 5) }
end
context 'when the session does not have a cluster time' do
before do
session.advance_cluster_time(new_cluster_time)
end
it 'sets the new cluster time' do
expect(session.cluster_time).to eq(new_cluster_time)
end
end
context 'when the session already has a cluster time' do
context 'when the original cluster time is less than the new cluster time' do
let(:original_cluster_time) do
Mongo::ClusterTime.new('clusterTime' => BSON::Timestamp.new(0, 1))
end
before do
session.instance_variable_set(:@cluster_time, original_cluster_time)
session.advance_cluster_time(new_cluster_time)
end
it 'sets the new cluster time' do
expect(session.cluster_time).to eq(new_cluster_time)
end
end
context 'when the original cluster time is equal or greater than the new cluster time' do
let(:original_cluster_time) do
Mongo::ClusterTime.new('clusterTime' => BSON::Timestamp.new(0, 6))
end
before do
session.instance_variable_set(:@cluster_time, original_cluster_time)
session.advance_cluster_time(new_cluster_time)
end
it 'does not update the cluster time' do
expect(session.cluster_time).to eq(original_cluster_time)
end
end
end
end
describe '#advance_operation_time' do
let(:new_operation_time) do
BSON::Timestamp.new(0, 5)
end
context 'when the session does not have an operation time' do
before do
session.advance_operation_time(new_operation_time)
end
it 'sets the new operation time' do
expect(session.operation_time).to eq(new_operation_time)
end
end
context 'when the session already has an operation time' do
context 'when the original operation time is less than the new operation time' do
let(:original_operation_time) do
BSON::Timestamp.new(0, 1)
end
before do
session.instance_variable_set(:@operation_time, original_operation_time)
session.advance_operation_time(new_operation_time)
end
it 'sets the new operation time' do
expect(session.operation_time).to eq(new_operation_time)
end
end
context 'when the original operation time is equal or greater than the new operation time' do
let(:original_operation_time) do
BSON::Timestamp.new(0, 6)
end
before do
session.instance_variable_set(:@operation_time, original_operation_time)
session.advance_operation_time(new_operation_time)
end
it 'does not update the operation time' do
expect(session.operation_time).to eq(original_operation_time)
end
end
end
end
describe 'ended?' do
context 'when the session has not been ended' do
it 'returns false' do
expect(session.ended?).to be(false)
end
end
context 'when the session has been ended' do
before do
session.end_session
end
it 'returns true' do
expect(session.ended?).to be(true)
end
end
end
describe 'end_session' do
let!(:server_session) do
session.instance_variable_get(:@server_session)
end
let(:cluster_session_pool) do
session.cluster.session_pool
end
it 'returns the server session to the cluster session pool' do
session.end_session
expect(cluster_session_pool.instance_variable_get(:@queue)).to include(server_session)
end
context 'when #end_session is called multiple times' do
before do
session.end_session
end
it 'returns nil' do
expect(session.end_session).to be_nil
end
end
end
describe '#retry_writes?' do
context 'when the option is set to true' do
let(:client) do
authorized_client_with_retry_writes
end
it 'returns true' do
expect(client.start_session.retry_writes?).to be(true)
end
end
context 'when the option is set to false' do
let(:client) do
authorized_client.with(retry_writes: false)
end
it 'returns false' do
expect(client.start_session.retry_writes?).to be(false)
end
end
context 'when the option is not defined' do
require_no_retry_writes
let(:client) do
authorized_client
end
it 'returns false' do
expect(client.start_session.retry_writes?).to be(false)
end
end
end
describe '#session_id' do
it 'returns a BSON::Document' do
expect(session.session_id).to be_a(BSON::Document)
end
context 'ended session' do
before do
session.end_session
end
it 'raises SessionEnded' do
expect do
session.session_id
end.to raise_error(Mongo::Error::SessionEnded)
end
end
context 'when the sesion is not materialized' do
let(:session) { authorized_client.get_session(implicit: true) }
before do
expect(session.materialized?).to be false
end
it 'raises SessionNotMaterialized' do
expect do
session.session_id
end.to raise_error(Mongo::Error::SessionNotMaterialized)
end
end
end
describe '#txn_num' do
it 'returns an integer' do
expect(session.txn_num).to be_a(Integer)
end
context 'ended session' do
before do
session.end_session
end
it 'raises SessionEnded' do
expect do
session.txn_num
end.to raise_error(Mongo::Error::SessionEnded)
end
end
end
describe '#next_txn_num' do
it 'returns an integer' do
expect(session.next_txn_num).to be_a(Integer)
end
it 'increments transaction number on each call' do
expect(session.next_txn_num).to eq(1)
expect(session.next_txn_num).to eq(2)
end
context 'ended session' do
before do
session.end_session
end
it 'raises SessionEnded' do
expect do
session.next_txn_num
end.to raise_error(Mongo::Error::SessionEnded)
end
end
end
describe '#start_session' do
context 'when block doesn\'t raise an error' do
it 'closes the session after the block' do
block_session = nil
authorized_client.start_session do |session|
expect(session.ended?).to be false
block_session = session
end
expect(block_session.ended?).to be true
end
end
context 'when block raises an error' do
it 'closes the session after the block' do
block_session = nil
expect do
authorized_client.start_session do |session|
block_session = session
raise 'This is an error!'
end
end.to raise_error(StandardError, 'This is an error!')
expect(block_session.ended?).to be true
end
end
context 'when block returns value' do
it 'is returned by the function' do
res = authorized_client.start_session do |_session|
4
end
expect(res).to be 4
end
end
it 'returns a session with session id' do
session = authorized_client.start_session
session.session_id.should be_a(BSON::Document)
end
end
describe '#unpin' do
context 'when called twice with the same connection' do
# Nested unpin_maybe handlers in the bulk_write code path can trigger
# Session#unpin to be invoked twice for the same network error: once
# from Operation::Executable#do_execute and once from
# BulkWrite#execute_operation. The second call must be a no-op rather
# than raising "Trying to check in a connection which is not currently
# checked out" from the pool.
it 'does not raise on the second call' do
server = authorized_client.cluster.next_primary
server.with_connection do |connection|
session.pin_to_server(server)
session.unpin(connection)
expect do
session.unpin(connection)
end.not_to raise_error
end
end
end
end
end