This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
/
node_spec.rb
445 lines (338 loc) · 9.76 KB
/
node_spec.rb
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
require "spec_helper"
describe Moped::Node, replica_set: true do
let(:replica_set_node) do
@replica_set.nodes.first
end
let(:node) do
Moped::Node.new(replica_set_node.address, pool_size: 1)
end
describe "#auto_discovering?" do
context "when no option is provided" do
let(:node) do
described_class.new(replica_set_node.address, {})
end
it "returns true" do
expect(node).to be_auto_discovering
end
end
context "when an option is provided" do
context "when the option is true" do
let(:node) do
described_class.new(replica_set_node.address, auto_discover: true)
end
it "returns true" do
expect(node).to be_auto_discovering
end
end
context "when the option is false" do
let(:node) do
described_class.new(replica_set_node.address, auto_discover: false)
end
it "returns true" do
expect(node).to_not be_auto_discovering
end
end
end
end
describe "#disconnect" do
context "when the node is running" do
before do
node.disconnect
end
it "disconnects from the server" do
node.should_not be_connected
end
end
end
describe "#latency" do
let(:node) do
Moped::Node.new("127.0.0.1:27017")
end
context "when the node is not connected" do
it "returns nil" do
expect(node.latency).to be_nil
end
end
context "when the node is connected" do
before do
node.connect
end
it "returns the latency in seconds" do
expect(node.latency < 1).to be_true
end
end
end
describe "#peers" do
let(:info) do
{
"setName" => "moped_dev",
"ismaster" => true,
"secondary" => false,
"hosts" => [ "127.0.0.1:27017", "localhost:27018" ],
"primary" => "127.0.0.1:27017",
"me" => "127.0.0.1:27017",
"ok" => 1.0
}
end
context "when the node is auto discovering" do
let(:node) do
described_class.new("127.0.0.1:27017", pool_size: 1)
end
before do
node.should_receive(:command).with("admin", ismaster: 1).and_return(info)
node.refresh
end
it "auto discovers additional host nodes" do
expect(node.peers.size).to eq(2)
end
end
context "when the node is not auto discovering" do
let(:node) do
described_class.new("127.0.0.1:27017", auto_discover: false)
end
before do
node.should_receive(:command).with("admin", ismaster: 1).and_return(info)
node.refresh
end
it "does not auto discover additional host nodes" do
expect(node.peers.size).to eq(0)
end
end
end
describe "#ensure_connected" do
context "when the node is running" do
it "processes the block" do
node.ensure_connected do
node.command("admin", ping: 1)
end.should eq("ok" => 1)
end
end
context "when the node is not running" do
before do
replica_set_node.stop
end
it "raises a connection error" do
lambda do
node.ensure_connected do
node.command("admin", ping: 1)
end
end.should raise_exception(Moped::Errors::ConnectionFailure)
end
it "marks the node as down" do
begin
node.ensure_connected do
node.command("admin", ping: 1)
end
rescue Moped::Errors::ConnectionFailure
node.should be_down
end
end
end
context "when node is connected but connection is dropped" do
before do
node.ensure_connected do
node.command("admin", ping: 1)
end
replica_set_node.hiccup
end
it "reconnects without raising an exception" do
node.ensure_connected do
node.command("admin", ping: 1)
end.should eq("ok" => 1)
end
end
context "when the server crashes or responds with nil" do
it "fails over to the next node" do
replica_set_node.crash_on_next_message!
node.ensure_connected do
node.command("admin", ping: 1)
end.should eq("ok" => 1)
end
end
context "when node closes the connection before sending a reply" do
it "fails over to the next node" do
replica_set_node.hiccup_on_next_message!
node.ensure_connected do
node.command("admin", ping: 1)
end.should eq("ok" => 1)
end
end
context "when the socket gets disconnected in the middle of a send" do
before do
node.connection do |conn|
conn.stub(:connected?).and_return(true)
conn.instance_variable_set(:@sock, nil)
end
end
it "reconnects the socket" do
lambda do
node.ensure_connected do
node.command("admin", ping: 1)
end
end.should_not raise_exception
end
end
context "when there is a reconfiguration" do
let(:potential_reconfiguration_error) do
Moped::Errors::QueryFailure.new("", {})
end
before do
node.connection do |conn|
conn.stub(:connected?).and_return(false)
conn.stub(:connect).and_raise(potential_reconfiguration_error)
end
end
context "and the reconfigation is of a replica set" do
before do
potential_reconfiguration_error.stub(:reconfiguring_replica_set?).and_return(true)
end
it "raises a ReplicaSetReconfigured error" do
expect {
node.ensure_connected {}
}.to raise_error(Moped::Errors::ReplicaSetReconfigured)
end
end
context "and the reconfigation is not of a replica set" do
before do
potential_reconfiguration_error.stub(:reconfiguring_replica_set?).and_return(false)
end
it "raises a PotentialReconfiguration error" do
expect {
node.ensure_connected {}
}.to raise_error(Moped::Errors::PotentialReconfiguration)
end
end
end
end
describe "#initialize" do
let(:node) do
described_class.new("iamnota.mongoid.org")
end
let(:non_default_node) do
described_class.new("iama.mongoid.org:5309")
end
context "when dns cannot resolve the address" do
it "flags the node as being down" do
node.should be_down
end
it "sets the down_at time" do
node.down_at.should be_within(1).of(Time.now)
end
context "when attempting to refresh the node" do
before do
node.refresh
end
it "keeps the node flagged as down" do
node.should be_down
end
it "updates the down_at time" do
node.down_at.should be_within(1).of(Time.now)
end
end
end
end
describe "#messagable?" do
let(:node) do
Moped::Node.new("127.0.0.1:27017")
end
context "when the node is primary" do
before do
node.instance_variable_set(:@primary, true)
end
it "returns true" do
expect(node).to be_messagable
end
end
context "when the node is secondary" do
before do
node.instance_variable_set(:@secondary, true)
end
it "returns true" do
expect(node).to be_messagable
end
end
context "when the node is an arbiter" do
before do
node.instance_variable_set(:@arbiter, true)
end
it "returns false" do
expect(node).to_not be_messagable
end
end
context "when the node is passive" do
before do
node.instance_variable_set(:@passive, true)
end
it "returns false" do
expect(node).to_not be_messagable
end
end
end
describe "#refresh" do
context "when the ismaster command fails" do
let(:node) do
described_class.new("127.0.0.1:27017")
end
before do
node.should_receive(:command).with("admin", ismaster: 1).and_raise(Timeout::Error)
node.refresh
end
it "still sets the refresh time" do
expect(node.refreshed_at).to_not be_nil
end
end
context "when the node has authentication details" do
let(:node) do
described_class.new("127.0.0.1:27017")
end
before do
node.credentials["moped_test"] = [ "user", "pass" ]
end
context "when discovering a peer" do
let(:info) do
{
"ismaster" => true,
"secondary" => false,
"hosts" => [ "127.0.0.1:27017", "127.0.0.1:27018" ],
"me" => "127.0.0.1:27017",
"maxBsonObjectSize" => 16777216,
"ok" => 1.0
}
end
before do
node.should_receive(:command).with("admin", ismaster: 1).and_return(info)
node.refresh
end
let(:peer) do
node.peers.last
end
it "add the authentication details to the peer" do
peer.credentials.should eq(node.credentials)
end
end
end
context "when refreshing a node with a bad address" do
let(:node) do
described_class.new("iamnota.mongoid.org")
end
before do
node.address.stub(:resolve).and_return(true)
end
context "when ensuring primary" do
before do
node.stub(:executing?).with(:ensure_primary).and_return(true)
end
context "and not on the primary" do
before do
node.stub(:command).and_return("secondary" => true)
end
it "raises a ReplicaSetReconfigured error" do
expect {
node.refresh
}.to raise_error(Moped::Errors::ReplicaSetReconfigured)
end
end
end
end
end
end