mongo 1.8.0 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/README.md +14 -29
  2. data/VERSION +1 -1
  3. data/lib/mongo.rb +3 -0
  4. data/lib/mongo/collection.rb +99 -49
  5. data/lib/mongo/cursor.rb +17 -17
  6. data/lib/mongo/db.rb +30 -14
  7. data/lib/mongo/gridfs/grid.rb +5 -3
  8. data/lib/mongo/gridfs/grid_file_system.rb +5 -3
  9. data/lib/mongo/gridfs/grid_io.rb +5 -3
  10. data/lib/mongo/legacy.rb +9 -2
  11. data/lib/mongo/mongo_client.rb +100 -72
  12. data/lib/mongo/mongo_replica_set_client.rb +46 -60
  13. data/lib/mongo/mongo_sharded_client.rb +5 -66
  14. data/lib/mongo/networking.rb +2 -1
  15. data/lib/mongo/util/node.rb +41 -42
  16. data/lib/mongo/util/pool.rb +15 -43
  17. data/lib/mongo/util/pool_manager.rb +16 -65
  18. data/lib/mongo/util/read_preference.rb +82 -0
  19. data/lib/mongo/util/sharding_pool_manager.rb +0 -86
  20. data/lib/mongo/util/ssl_socket.rb +2 -1
  21. data/lib/mongo/util/support.rb +8 -18
  22. data/lib/mongo/util/tcp_socket.rb +5 -4
  23. data/lib/mongo/util/thread_local_variable_manager.rb +29 -0
  24. data/lib/mongo/util/unix_socket.rb +23 -0
  25. data/lib/mongo/util/uri_parser.rb +31 -18
  26. data/lib/mongo/util/write_concern.rb +7 -2
  27. data/mongo.gemspec +1 -1
  28. data/test/auxillary/repl_set_auth_test.rb +2 -2
  29. data/test/bson/bson_test.rb +1 -1
  30. data/test/bson/byte_buffer_test.rb +24 -26
  31. data/test/bson/hash_with_indifferent_access_test.rb +11 -1
  32. data/test/functional/collection_test.rb +16 -16
  33. data/test/functional/connection_test.rb +1 -4
  34. data/test/functional/db_api_test.rb +14 -10
  35. data/test/functional/pool_test.rb +23 -31
  36. data/test/functional/timeout_test.rb +3 -5
  37. data/test/functional/uri_test.rb +10 -5
  38. data/test/replica_set/basic_test.rb +3 -8
  39. data/test/replica_set/client_test.rb +47 -31
  40. data/test/replica_set/complex_connect_test.rb +12 -10
  41. data/test/replica_set/connection_test.rb +8 -151
  42. data/test/replica_set/count_test.rb +9 -5
  43. data/test/replica_set/cursor_test.rb +17 -27
  44. data/test/replica_set/insert_test.rb +5 -10
  45. data/test/replica_set/query_test.rb +4 -9
  46. data/test/replica_set/read_preference_test.rb +200 -0
  47. data/test/replica_set/refresh_test.rb +54 -65
  48. data/test/replica_set/replication_ack_test.rb +16 -14
  49. data/test/sharded_cluster/basic_test.rb +30 -0
  50. data/test/test_helper.rb +33 -15
  51. data/test/threading/basic_test.rb +79 -0
  52. data/test/tools/mongo_config.rb +62 -22
  53. data/test/unit/client_test.rb +36 -14
  54. data/test/unit/collection_test.rb +23 -0
  55. data/test/unit/connection_test.rb +30 -14
  56. data/test/unit/cursor_test.rb +137 -7
  57. data/test/unit/db_test.rb +17 -4
  58. data/test/unit/grid_test.rb +2 -2
  59. data/test/unit/node_test.rb +2 -1
  60. data/test/unit/pool_manager_test.rb +29 -1
  61. data/test/unit/read_test.rb +15 -15
  62. data/test/unit/safe_test.rb +4 -4
  63. data/test/unit/write_concern_test.rb +4 -4
  64. metadata +134 -143
  65. data/examples/admin.rb +0 -43
  66. data/examples/capped.rb +0 -22
  67. data/examples/cursor.rb +0 -48
  68. data/examples/gridfs.rb +0 -44
  69. data/examples/index_test.rb +0 -126
  70. data/examples/info.rb +0 -31
  71. data/examples/queries.rb +0 -74
  72. data/examples/replica_set.rb +0 -26
  73. data/examples/simple.rb +0 -25
  74. data/examples/strict.rb +0 -35
  75. data/examples/types.rb +0 -36
  76. data/examples/web/thin/load.rb +0 -23
  77. data/examples/web/unicorn/load.rb +0 -25
  78. data/test/support/hash_with_indifferent_access.rb +0 -186
  79. data/test/support/keys.rb +0 -45
  80. data/test/threading/threading_with_large_pool_test.rb +0 -90
@@ -415,10 +415,7 @@ class TestConnection < Test::Unit::TestCase
415
415
  end
416
416
 
417
417
  should "show a proper exception message if an IOError is raised while closing a socket" do
418
- fake_socket = mock('fake_socket')
419
- fake_socket.stubs(:close).raises(IOError.new)
420
- fake_socket.stub_everything
421
- TCPSocket.stubs(:new).returns(fake_socket)
418
+ TCPSocket.any_instance.stubs(:close).raises(IOError.new)
422
419
 
423
420
  @con.primary_pool.checkout_new_socket
424
421
  @con.primary_pool.expects(:warn)
@@ -330,7 +330,7 @@ class DBAPITest < Test::Unit::TestCase
330
330
  @@db.create_collection('foobar')
331
331
 
332
332
  coll = @@db.create_collection('foobar')
333
- assert_equal true, coll.safe
333
+ assert_equal true, Mongo::WriteConcern.gle?(coll.write_concern)
334
334
  end
335
335
 
336
336
 
@@ -404,8 +404,9 @@ class DBAPITest < Test::Unit::TestCase
404
404
 
405
405
  test.insert("hello" => "world")
406
406
  test.insert("hello" => "mike")
407
- test.insert("hello" => "world")
408
- assert @@db.error?
407
+ assert_raise OperationFailure do
408
+ test.insert("hello" => "world")
409
+ end
409
410
  end
410
411
 
411
412
  def test_index_on_subfield
@@ -423,8 +424,9 @@ class DBAPITest < Test::Unit::TestCase
423
424
 
424
425
  test.insert("hello" => {"a" => 4, "b" => 5})
425
426
  test.insert("hello" => {"a" => 7, "b" => 2})
426
- test.insert("hello" => {"a" => 4, "b" => 10})
427
- assert @@db.error?
427
+ assert_raise OperationFailure do
428
+ test.insert("hello" => {"a" => 4, "b" => 10} )
429
+ end
428
430
  end
429
431
 
430
432
  def test_array
@@ -766,24 +768,26 @@ HERE
766
768
 
767
769
  def test_encodings
768
770
  if RUBY_VERSION >= '1.9'
769
- ascii = "hello world"
771
+ default = "hello world"
770
772
  utf8 = "hello world".encode("UTF-8")
771
773
  iso8859 = "hello world".encode("ISO-8859-1")
772
774
 
773
775
  if RUBY_PLATFORM =~ /jruby/
774
- assert_equal "ASCII-8BIT", ascii.encoding.name
776
+ assert_equal "ASCII-8BIT", default.encoding.name
777
+ elsif RUBY_VERSION >= '2.0'
778
+ assert_equal "UTF-8", default.encoding.name
775
779
  else
776
- assert_equal "US-ASCII", ascii.encoding.name
780
+ assert_equal "US-ASCII", default.encoding.name
777
781
  end
778
782
 
779
783
  assert_equal "UTF-8", utf8.encoding.name
780
784
  assert_equal "ISO-8859-1", iso8859.encoding.name
781
785
 
782
786
  @@coll.remove
783
- @@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859)
787
+ @@coll.save("default" => default, "utf8" => utf8, "iso8859" => iso8859)
784
788
  doc = @@coll.find_one()
785
789
 
786
- assert_equal "UTF-8", doc["ascii"].encoding.name
790
+ assert_equal "UTF-8", doc["default"].encoding.name
787
791
  assert_equal "UTF-8", doc["utf8"].encoding.name
788
792
  assert_equal "UTF-8", doc["iso8859"].encoding.name
789
793
  end
@@ -5,51 +5,43 @@ class PoolTest < Test::Unit::TestCase
5
5
  include Mongo
6
6
 
7
7
  def setup
8
- @connection = standard_connection
8
+ @client ||= standard_connection({:pool_size => 500, :pool_timeout => 5})
9
+ @db = @client.db(MONGO_TEST_DB)
10
+ @collection = @db.collection("pool_test")
9
11
  end
10
12
 
11
13
  def test_pool_affinity
12
- @pool = Pool.new(@connection, TEST_HOST, TEST_PORT, :size => 5)
13
-
14
- @threads = []
14
+ pool = Pool.new(@client, TEST_HOST, TEST_PORT, :size => 5)
15
15
 
16
+ threads = []
16
17
  10.times do
17
- @threads << Thread.new do
18
- original_socket = @pool.checkout
19
- @pool.checkin(original_socket)
18
+ threads << Thread.new do
19
+ original_socket = pool.checkout
20
+ pool.checkin(original_socket)
20
21
  5000.times do
21
- socket = @pool.checkout
22
+ socket = pool.checkout
22
23
  assert_equal original_socket, socket
23
- @pool.checkin(socket)
24
+ pool.checkin(socket)
24
25
  end
25
26
  end
26
27
  end
27
28
 
28
- @threads.each { |t| t.join }
29
+ threads.each { |t| t.join }
29
30
  end
30
31
 
31
- def test_pool_thread_pruning
32
- @pool = Pool.new(@connection, TEST_HOST, TEST_PORT, :size => 5)
33
-
34
- @threads = []
35
-
36
- 10.times do
37
- @threads << Thread.new do
38
- 50.times do
39
- socket = @pool.checkout
40
- @pool.checkin(socket)
41
- end
42
- end
32
+ def test_pool_affinity_max_size
33
+ 8000.times {|x| @collection.insert({:value => x})}
34
+ threads = []
35
+ threads << Thread.new do
36
+ @collection.find({"value" => {"$lt" => 100}}).each {|e| e}
37
+ Thread.pass
38
+ sleep(5)
39
+ @collection.find({"value" => {"$gt" => 100}}).each {|e| e}
43
40
  end
44
-
45
- @threads.each { |t| t.join }
46
- assert_equal 10, @pool.instance_variable_get(:@threads_to_sockets).size
47
-
48
- # Thread-socket pool
49
- 10000.times do
50
- @pool.checkin(@pool.checkout)
41
+ sleep(1)
42
+ threads << Thread.new do
43
+ @collection.find({'$where' => "function() {for(i=0;i<8000;i++) {this.value};}"}).each {|e| e}
51
44
  end
52
-
53
- assert_equal 1, @pool.instance_variable_get(:@threads_to_sockets).size
45
+ threads.each(&:join)
54
46
  end
55
47
  end
@@ -2,18 +2,16 @@ require 'test_helper'
2
2
 
3
3
  class TestTimeout < Test::Unit::TestCase
4
4
  def test_op_timeout
5
- connection = standard_connection(:op_timeout => 2)
5
+ connection = standard_connection(:op_timeout => 1)
6
6
 
7
7
  admin = connection.db('admin')
8
8
 
9
- command = BSON::OrderedHash.new
10
- command[:sleep] = 1
11
- command[:secs] = 1
9
+ command = {:eval => "sleep(500)"}
12
10
  # Should not timeout
13
11
  assert admin.command(command)
14
12
 
15
13
  # Should timeout
16
- command[:secs] = 3
14
+ command = {:eval => "sleep(1500)"}
17
15
  assert_raise Mongo::OperationTimeout do
18
16
  admin.command(command)
19
17
  end
@@ -20,8 +20,8 @@ class URITest < Test::Unit::TestCase
20
20
  def test_multiple_uris
21
21
  parser = Mongo::URIParser.new('mongodb://a.example.com:27018,b.example.com')
22
22
  assert_equal 2, parser.nodes.length
23
- assert_equal 'a.example.com:27018', parser.nodes[0]
24
- assert_equal 'b.example.com:27017', parser.nodes[1]
23
+ assert_equal ['a.example.com', 27018], parser.nodes[0]
24
+ assert_equal ['b.example.com', 27017], parser.nodes[1]
25
25
  end
26
26
 
27
27
  def test_complex_passwords
@@ -48,8 +48,8 @@ class URITest < Test::Unit::TestCase
48
48
  def test_multiple_uris_with_auths
49
49
  parser = Mongo::URIParser.new('mongodb://bob:secret@a.example.com:27018,b.example.com/test')
50
50
  assert_equal 2, parser.nodes.length
51
- assert_equal 'a.example.com:27018', parser.nodes[0]
52
- assert_equal 'b.example.com:27017', parser.nodes[1]
51
+ assert_equal ['a.example.com', 27018], parser.nodes[0]
52
+ assert_equal ['b.example.com', 27017], parser.nodes[1]
53
53
  assert_equal 2, parser.auths.length
54
54
  assert_equal "bob", parser.auths[0]["username"]
55
55
  assert_equal "secret", parser.auths[0]["password"]
@@ -97,7 +97,12 @@ class URITest < Test::Unit::TestCase
97
97
  assert parser.journal
98
98
  assert_equal 200, parser.wtimeoutms
99
99
  end
100
-
100
+
101
+ def test_opts_ssl
102
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?ssl=true;w=2;journal=true;fsync=true;wtimeoutMS=200')
103
+ assert parser.ssl
104
+ end
105
+
101
106
  def test_opts_nonsafe_timeout
102
107
  parser = Mongo::URIParser.new('mongodb://localhost:27018?connectTimeoutMS=5500&socketTimeoutMS=500')
103
108
  assert_equal 5.5, parser.connecttimeoutms
@@ -6,11 +6,6 @@ class BasicTest < Test::Unit::TestCase
6
6
  ensure_cluster(:rs)
7
7
  end
8
8
 
9
- def self.shutdown
10
- @@cluster.stop
11
- @@cluster.clobber
12
- end
13
-
14
9
  def test_connect
15
10
  client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :name => @rs.repl_set_name)
16
11
  assert client.connected?
@@ -70,9 +65,9 @@ class BasicTest < Test::Unit::TestCase
70
65
  assert_equal @rs.primary_name, [client.host, client.port].join(':')
71
66
  assert_equal client.host, client.primary_pool.host
72
67
  assert_equal client.port, client.primary_pool.port
73
- assert_equal 2, client.secondaries.length
74
- assert_equal 2, client.arbiters.length
75
- assert_equal 2, client.secondary_pools.length
68
+ assert_equal 1, client.secondaries.length
69
+ assert_equal 1, client.arbiters.length
70
+ assert_equal 1, client.secondary_pools.length
76
71
  assert_equal @rs.repl_set_name, client.replica_set_name
77
72
  assert client.secondary_pools.include?(client.read_pool(:secondary))
78
73
  assert_equal 90, client.refresh_interval
@@ -11,34 +11,12 @@ class ClientTest < Test::Unit::TestCase
11
11
  @client.close if @client
12
12
  end
13
13
 
14
- def self.shutdown
15
- @@cluster.stop
16
- @@cluster.clobber
17
- end
18
-
19
- # To reset after (test) failure
20
- # rake test:cleanup
21
-
22
- def step_down_command
23
- # Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
24
- step_down_command = BSON::OrderedHash.new
25
- step_down_command[:replSetStepDown] = 60
26
- step_down_command[:force] = true
27
- step_down_command
28
- end
29
-
30
14
  # TODO: test connect timeout.
31
15
 
32
16
  def test_connect_with_deprecated_multi
33
- #replica_host_ports = @rs.replicas.collect{|replica| [replica.host, replica.port]}
34
- host = @rs.replicas.first.host
35
17
  silently do
36
- @client = MongoClient.multi([
37
- # guaranteed to have one data-holding member
38
- [host, @rs.replicas[0].port],
39
- [host, @rs.replicas[1].port],
40
- [host, @rs.replicas[2].port],
41
- ], :name => @rs.repl_set_name)
18
+ # guaranteed to have one data-holding member
19
+ @client = MongoClient.multi(@rs.repl_set_seeds_old, :name => @rs.repl_set_name)
42
20
  end
43
21
  assert !@client.nil?
44
22
  assert @client.connected?
@@ -50,6 +28,28 @@ class ClientTest < Test::Unit::TestCase
50
28
  end
51
29
  end
52
30
 
31
+ def test_reconnect_method_override
32
+ rescue_connection_failure do
33
+ @client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
34
+ end
35
+
36
+ MongoReplicaSetClient.any_instance.expects(:connect)
37
+ MongoClient.any_instance.expects(:connect).never
38
+ assert_nothing_raised Mongo::ConnectionFailure do
39
+ @client.reconnect
40
+ end
41
+ end
42
+
43
+ def test_primary_method_override
44
+ rescue_connection_failure do
45
+ @client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
46
+ end
47
+
48
+ MongoReplicaSetClient.any_instance.expects(:read_primary?)
49
+ MongoClient.any_instance.expects(:read_primary?).never
50
+ @client.primary?
51
+ end
52
+
53
53
  def test_connect_with_first_secondary_node_terminated
54
54
  @rs.secondaries.first.stop
55
55
 
@@ -70,10 +70,10 @@ class ClientTest < Test::Unit::TestCase
70
70
 
71
71
  def test_connect_with_primary_stepped_down
72
72
  @client = MongoReplicaSetClient.new @rs.repl_set_seeds
73
- @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
73
+ @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
74
74
  assert @client[MONGO_TEST_DB]['bar'].find_one
75
75
 
76
- primary = Mongo::MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
76
+ primary = Mongo::MongoClient.new(*@client.primary)
77
77
  assert_raise Mongo::ConnectionFailure do
78
78
  primary['admin'].command(step_down_command)
79
79
  end
@@ -82,38 +82,43 @@ class ClientTest < Test::Unit::TestCase
82
82
  rescue_connection_failure do
83
83
  @client[MONGO_TEST_DB]['bar'].find_one
84
84
  end
85
+ @client[MONGO_TEST_DB]['bar'].find_one
85
86
  end
86
87
 
87
88
  def test_connect_with_primary_killed
88
89
  @client = MongoReplicaSetClient.new @rs.repl_set_seeds
89
90
  assert @client.connected?
90
- @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
91
+ @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
91
92
  assert @client[MONGO_TEST_DB]['bar'].find_one
92
93
 
93
94
  @rs.primary.kill(Signal.list['KILL'])
94
95
 
96
+ sleep(3)
97
+
95
98
  rescue_connection_failure do
96
99
  @client[MONGO_TEST_DB]['bar'].find_one
97
100
  end
101
+ @client[MONGO_TEST_DB]['bar'].find_one
98
102
  end
99
103
 
100
104
  def test_save_with_primary_stepped_down
101
105
  @client = MongoReplicaSetClient.new @rs.repl_set_seeds
102
106
  assert @client.connected?
103
107
 
104
- primary = Mongo::MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
108
+ primary = Mongo::MongoClient.new(*@client.primary)
105
109
  assert_raise Mongo::ConnectionFailure do
106
110
  primary['admin'].command(step_down_command)
107
111
  end
108
112
 
109
113
  rescue_connection_failure do
110
- @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
114
+ @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
111
115
  end
116
+ @client[MONGO_TEST_DB]['bar'].find_one
112
117
  end
113
118
 
114
119
  #def test_connect_with_first_node_removed
115
120
  # @client = MongoReplicaSetClient.new @rs.repl_set_seeds
116
- # @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 3})
121
+ # @client[MONGO_TEST_DB]['bar'].save({:a => 1}, {:w => 2})
117
122
  #
118
123
  # old_primary = [@client.primary_pool.host, @client.primary_pool.port]
119
124
  # old_primary_conn = Mongo::MongoClient.new(*old_primary)
@@ -220,7 +225,7 @@ class ClientTest < Test::Unit::TestCase
220
225
 
221
226
  def test_connect_with_old_seed_format
222
227
  silently do
223
- @client = MongoReplicaSetClient.new(@rs.replicas[0].host_port_a, @rs.replicas[1].host_port_a)
228
+ @client = MongoReplicaSetClient.new(@rs.repl_set_seeds_old)
224
229
  end
225
230
  assert @client.connected?
226
231
  end
@@ -252,4 +257,15 @@ class ClientTest < Test::Unit::TestCase
252
257
  assert_equal 0, @client.write_concern[:w]
253
258
  end
254
259
 
260
+ def test_find_and_modify_with_secondary_read_preference
261
+ @client = MongoReplicaSetClient.new
262
+ collection = @client[MONGO_TEST_DB].collection('test', :read => :secondary)
263
+ collection << { :a => 1, :processed => false}
264
+
265
+ collection.find_and_modify(
266
+ :query => {},
267
+ :update => {"$set" => {:processed => true}}
268
+ )
269
+ assert_equal collection.find_one({}, :fields => {:_id => 0}, :read => :primary), {'a' => 1, 'processed' => true}
270
+ end
255
271
  end
@@ -6,11 +6,6 @@ class ComplexConnectTest < Test::Unit::TestCase
6
6
  ensure_cluster(:rs)
7
7
  end
8
8
 
9
- def self.shutdown
10
- @@cluster.stop
11
- @@cluster.clobber
12
- end
13
-
14
9
  def teardown
15
10
  @client.close if defined?(@conn) && @conn
16
11
  end
@@ -31,8 +26,12 @@ class ComplexConnectTest < Test::Unit::TestCase
31
26
  assert @client['test']['foo'].find_one
32
27
 
33
28
  config = primary['local']['system.replset'].find_one
29
+ old_config = config.dup
34
30
  config['version'] += 1
35
- port_to_delete = @rs.servers.collect(&:port).find{|port| port != primary.port}.to_s # eliminate exception: can't find self in new replset config
31
+
32
+ # eliminate exception: can't find self in new replset config
33
+ port_to_delete = @rs.servers.collect(&:port).find{|port| port != primary.port}.to_s
34
+
36
35
  config['members'].delete_if do |member|
37
36
  member['host'].include?(port_to_delete)
38
37
  end
@@ -42,15 +41,13 @@ class ComplexConnectTest < Test::Unit::TestCase
42
41
  end
43
42
  @rs.start
44
43
 
45
- force_stepdown = BSON::OrderedHash.new
46
- force_stepdown[:replSetStepDown] = 1
47
- force_stepdown[:force] = true
48
44
 
49
45
  assert_raise ConnectionFailure do
50
- primary['admin'].command(force_stepdown)
46
+ primary['admin'].command(step_down_command)
51
47
  end
52
48
 
53
49
  # isMaster is currently broken in 2.1+ when called on removed nodes
50
+ puts version
54
51
  if version < "2.1"
55
52
  rescue_connection_failure do
56
53
  assert @client['test']['foo'].find_one
@@ -58,5 +55,10 @@ class ComplexConnectTest < Test::Unit::TestCase
58
55
 
59
56
  assert @client['test']['foo'].find_one
60
57
  end
58
+
59
+ primary = MongoClient.new(host, @rs.primary.port)
60
+ assert_raise ConnectionFailure do
61
+ primary['admin'].command({:replSetReconfig => old_config})
62
+ end
61
63
  end
62
64
  end
@@ -4,41 +4,11 @@ class ConnectionTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  ensure_cluster(:rs)
7
- @connection = nil
8
7
  end
9
8
 
10
- def teardown
11
- @connection.close if @connection
12
- end
13
-
14
- def self.shutdown
15
- @@cluster.stop
16
- @@cluster.clobber
17
- end
18
-
19
- # To reset after (test) failure
20
- # rake test:cleanup
21
-
22
- def step_down_command
23
- # Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
24
- step_down_command = BSON::OrderedHash.new
25
- step_down_command[:replSetStepDown] = 60
26
- step_down_command[:force] = true
27
- step_down_command
28
- end
29
-
30
- # TODO: test connect timeout.
31
-
32
9
  def test_connect_with_deprecated_multi
33
- #replica_host_ports = @rs.replicas.collect{|replica| [replica.host, replica.port]}
34
- host = @rs.replicas.first.host
35
10
  silently do
36
- @connection = Connection.multi([
37
- # guaranteed to have one data-holding member
38
- [host, @rs.replicas[0].port],
39
- [host, @rs.replicas[1].port],
40
- [host, @rs.replicas[2].port],
41
- ], :name => @rs.repl_set_name)
11
+ @connection = Connection.multi(@rs.repl_set_seeds_old, :name => @rs.repl_set_name)
42
12
  end
43
13
  assert !@connection.nil?
44
14
  assert @connection.connected?
@@ -68,127 +38,14 @@ class ConnectionTest < Test::Unit::TestCase
68
38
  assert @connection.connected?
69
39
  end
70
40
 
71
- def test_connect_with_primary_stepped_down
72
- @connection = ReplSetConnection.new @rs.repl_set_seeds
73
- @connection[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
74
- assert @connection[MONGO_TEST_DB]['bar'].find_one
75
-
76
- primary = Mongo::Connection.new(@connection.primary_pool.host, @connection.primary_pool.port)
77
- assert_raise Mongo::ConnectionFailure do
78
- primary['admin'].command(step_down_command)
79
- end
80
- assert @connection.connected?
81
-
82
- rescue_connection_failure do
83
- @connection[MONGO_TEST_DB]['bar'].find_one
84
- end
85
- end
86
-
87
- def test_connect_with_primary_killed
88
- @connection = ReplSetConnection.new @rs.repl_set_seeds
89
- assert @connection.connected?
90
- @connection[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
91
- assert @connection[MONGO_TEST_DB]['bar'].find_one
92
-
93
- @rs.primary.kill(Signal.list['KILL'])
94
-
95
- rescue_connection_failure do
96
- @connection[MONGO_TEST_DB]['bar'].find_one
97
- end
98
- end
99
-
100
- def test_save_with_primary_stepped_down
101
- @connection = ReplSetConnection.new @rs.repl_set_seeds
102
- assert @connection.connected?
103
-
104
- primary = Mongo::Connection.new(@connection.primary_pool.host, @connection.primary_pool.port)
105
- assert_raise Mongo::ConnectionFailure do
106
- primary['admin'].command(step_down_command)
107
- end
108
-
109
- rescue_connection_failure do
110
- @connection[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
111
- end
112
- end
113
-
114
- #def test_connect_with_first_node_removed
115
- # @connection = ReplSetConnection.new @rs.repl_set_seeds
116
- # @connection[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
117
- #
118
- # old_primary = [@connection.primary_pool.host, @connection.primary_pool.port]
119
- # old_primary_conn = Mongo::Connection.new(*old_primary)
120
- # assert_raise Mongo::ConnectionFailure do
121
- # old_primary_conn['admin'].command(step_down_command)
122
- # end
123
- #
124
- # # Wait for new primary
125
- # rescue_connection_failure do
126
- # sleep 1 until @rs.get_node_with_state(1)
127
- # end
128
- #
129
- # new_primary = @rs.get_all_host_pairs_with_state(1).first
130
- # new_primary_conn = Mongo::Connection.new(*new_primary)
131
- #
132
- # config = nil
133
- #
134
- # # Remove old primary from replset
135
- # rescue_connection_failure do
136
- # config = @connection['local']['system.replset'].find_one
137
- # end
138
- #
139
- # old_member = config['members'].select {|m| m['host'] == old_primary.join(':')}.first
140
- # config['members'].reject! {|m| m['host'] == old_primary.join(':')}
141
- # config['version'] += 1
142
- #
143
- # begin
144
- # new_primary_conn['admin'].command({'replSetReconfig' => config})
145
- # rescue Mongo::ConnectionFailure
146
- # end
147
- #
148
- # # Wait for the dust to settle
149
- # rescue_connection_failure do
150
- # assert @connection[MONGO_TEST_DB]['bar'].find_one
151
- # end
152
- #
153
- # # Make sure a new connection skips the old primary
154
- # @new_conn = ReplSetConnection.new @rs.repl_set_seeds
155
- # @new_conn.connect
156
- # new_nodes = [@new_conn.primary] + @new_conn.secondaries
157
- # assert !(new_nodes).include?(old_primary)
158
- #
159
- # # Add the old primary back
160
- # config['members'] << old_member
161
- # config['version'] += 1
162
- #
163
- # begin
164
- # new_primary_conn['admin'].command({'replSetReconfig' => config})
165
- # rescue Mongo::ConnectionFailure
166
- # end
167
- #end
168
-
169
- #def test_connect_with_hung_first_node
170
- # hung_node = nil
171
- # begin
172
- # hung_node = IO.popen('nc -lk 127.0.0.1 29999 >/dev/null 2>&1')
173
- #
174
- # @connection = ReplSetConnection.new(['localhost:29999'] + @rs.repl_set_seeds,
175
- # :connect_timeout => 2)
176
- # @connection.connect
177
- # assert ['localhost:29999'] != @connection.primary
178
- # assert !@connection.secondaries.include?('localhost:29999')
179
- # ensure
180
- # Process.kill("KILL", hung_node.pid) if hung_node
181
- # end
182
- #end
183
-
184
41
  def test_connect_with_connection_string
185
- @connection = Connection.from_uri("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}")
42
+ @connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}")
186
43
  assert !@connection.nil?
187
44
  assert @connection.connected?
188
45
  end
189
46
 
190
47
  def test_connect_with_connection_string_in_env_var
191
- ENV['MONGODB_URI'] = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}"
48
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
192
49
  @connection = ReplSetConnection.new
193
50
  assert !@connection.nil?
194
51
  assert_equal 2, @connection.seeds.length
@@ -201,7 +58,7 @@ class ConnectionTest < Test::Unit::TestCase
201
58
  end
202
59
 
203
60
  def test_connect_with_connection_string_in_implicit_mongodb_uri
204
- ENV['MONGODB_URI'] = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}"
61
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
205
62
  @connection = Connection.from_uri
206
63
  assert !@connection.nil?
207
64
  assert_equal 2, @connection.seeds.length
@@ -220,13 +77,13 @@ class ConnectionTest < Test::Unit::TestCase
220
77
 
221
78
  def test_connect_with_old_seed_format
222
79
  silently do
223
- @connection = ReplSetConnection.new(@rs.replicas[0].host_port_a, @rs.replicas[1].host_port_a)
80
+ @connection = ReplSetConnection.new(@rs.repl_set_seeds_old)
224
81
  end
225
82
  assert @connection.connected?
226
83
  end
227
84
 
228
85
  def test_connect_with_full_connection_string
229
- @connection = Connection.from_uri("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true")
86
+ @connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true")
230
87
  assert !@connection.nil?
231
88
  assert @connection.connected?
232
89
  assert_equal 2, @connection.write_concern[:w]
@@ -235,7 +92,7 @@ class ConnectionTest < Test::Unit::TestCase
235
92
  end
236
93
 
237
94
  def test_connect_with_full_connection_string_in_env_var
238
- ENV['MONGODB_URI'] = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
95
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
239
96
  @connection = ReplSetConnection.new
240
97
  assert !@connection.nil?
241
98
  assert @connection.connected?
@@ -245,7 +102,7 @@ class ConnectionTest < Test::Unit::TestCase
245
102
  end
246
103
 
247
104
  def test_connect_options_override_env_var
248
- ENV['MONGODB_URI'] = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
105
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
249
106
  @connection = ReplSetConnection.new({:safe => {:w => 1}})
250
107
  assert !@connection.nil?
251
108
  assert @connection.connected?