mongo 1.7.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. data/{LICENSE.txt → LICENSE} +0 -0
  2. data/README.md +124 -111
  3. data/Rakefile +9 -325
  4. data/VERSION +1 -0
  5. data/bin/mongo_console +4 -4
  6. data/examples/admin.rb +43 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +44 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +74 -0
  13. data/examples/replica_set.rb +26 -0
  14. data/examples/simple.rb +25 -0
  15. data/examples/strict.rb +35 -0
  16. data/examples/types.rb +36 -0
  17. data/{test/load → examples/web}/thin/load.rb +3 -1
  18. data/{test/load → examples/web}/unicorn/load.rb +5 -3
  19. data/lib/mongo.rb +8 -10
  20. data/lib/mongo/collection.rb +134 -114
  21. data/lib/mongo/cursor.rb +21 -14
  22. data/lib/mongo/db.rb +30 -28
  23. data/lib/mongo/exceptions.rb +1 -1
  24. data/lib/mongo/gridfs/grid.rb +8 -7
  25. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  26. data/lib/mongo/gridfs/grid_file_system.rb +6 -5
  27. data/lib/mongo/gridfs/grid_io.rb +22 -19
  28. data/lib/mongo/legacy.rb +82 -0
  29. data/lib/mongo/{connection.rb → mongo_client.rb} +82 -61
  30. data/lib/mongo/{repl_set_connection.rb → mongo_replica_set_client.rb} +54 -39
  31. data/lib/mongo/{sharded_connection.rb → mongo_sharded_client.rb} +9 -9
  32. data/lib/mongo/networking.rb +25 -20
  33. data/lib/mongo/util/conversions.rb +1 -1
  34. data/lib/mongo/util/core_ext.rb +1 -1
  35. data/lib/mongo/util/logging.rb +20 -4
  36. data/lib/mongo/util/node.rb +16 -16
  37. data/lib/mongo/util/pool.rb +56 -27
  38. data/lib/mongo/util/pool_manager.rb +28 -27
  39. data/lib/mongo/util/server_version.rb +1 -1
  40. data/lib/mongo/util/sharding_pool_manager.rb +8 -8
  41. data/lib/mongo/util/ssl_socket.rb +1 -5
  42. data/lib/mongo/util/support.rb +24 -8
  43. data/lib/mongo/util/tcp_socket.rb +0 -4
  44. data/lib/mongo/util/uri_parser.rb +54 -38
  45. data/lib/mongo/util/write_concern.rb +67 -0
  46. data/mongo.gemspec +21 -32
  47. data/test/auxillary/{1.4_features.rb → 1.4_feature_test.rb} +4 -5
  48. data/test/auxillary/authentication_test.rb +18 -20
  49. data/test/auxillary/autoreconnect_test.rb +3 -5
  50. data/test/auxillary/fork_test.rb +5 -7
  51. data/test/auxillary/repl_set_auth_test.rb +13 -15
  52. data/test/auxillary/slave_connection_test.rb +8 -7
  53. data/test/auxillary/threaded_authentication_test.rb +15 -17
  54. data/test/bson/binary_test.rb +1 -1
  55. data/test/bson/bson_test.rb +60 -36
  56. data/test/bson/byte_buffer_test.rb +1 -1
  57. data/test/bson/hash_with_indifferent_access_test.rb +2 -2
  58. data/test/bson/json_test.rb +1 -2
  59. data/test/bson/object_id_test.rb +1 -2
  60. data/test/bson/ordered_hash_test.rb +1 -1
  61. data/test/bson/timestamp_test.rb +1 -1
  62. data/test/{collection_test.rb → functional/collection_test.rb} +57 -57
  63. data/test/{connection_test.rb → functional/connection_test.rb} +75 -89
  64. data/test/{conversions_test.rb → functional/conversions_test.rb} +1 -1
  65. data/test/{cursor_fail_test.rb → functional/cursor_fail_test.rb} +3 -29
  66. data/test/{cursor_message_test.rb → functional/cursor_message_test.rb} +1 -1
  67. data/test/{cursor_test.rb → functional/cursor_test.rb} +5 -1
  68. data/test/{db_api_test.rb → functional/db_api_test.rb} +8 -9
  69. data/test/{db_connection_test.rb → functional/db_connection_test.rb} +3 -5
  70. data/test/{db_test.rb → functional/db_test.rb} +13 -13
  71. data/test/{grid_file_system_test.rb → functional/grid_file_system_test.rb} +2 -2
  72. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +6 -6
  73. data/test/{grid_test.rb → functional/grid_test.rb} +4 -10
  74. data/test/{pool_test.rb → functional/pool_test.rb} +1 -1
  75. data/test/functional/safe_test.rb +84 -0
  76. data/test/{support_test.rb → functional/support_test.rb} +1 -1
  77. data/test/{threading_test.rb → functional/threading_test.rb} +9 -9
  78. data/test/{timeout_test.rb → functional/timeout_test.rb} +1 -1
  79. data/test/{uri_test.rb → functional/uri_test.rb} +1 -1
  80. data/test/functional/write_concern_test.rb +104 -0
  81. data/test/replica_set/basic_test.rb +139 -0
  82. data/test/replica_set/client_test.rb +255 -0
  83. data/test/replica_set/complex_connect_test.rb +62 -0
  84. data/test/replica_set/connection_test.rb +255 -0
  85. data/test/{replica_sets → replica_set}/count_test.rb +17 -14
  86. data/test/replica_set/cursor_test.rb +75 -0
  87. data/test/{replica_sets → replica_set}/insert_test.rb +19 -16
  88. data/test/replica_set/query_test.rb +64 -0
  89. data/test/replica_set/refresh_test.rb +153 -0
  90. data/test/{replica_sets → replica_set}/replication_ack_test.rb +21 -17
  91. data/test/sharded_cluster/basic_test.rb +31 -50
  92. data/test/support/hash_with_indifferent_access.rb +1 -1
  93. data/test/test_helper.rb +56 -9
  94. data/test/threading/threading_with_large_pool_test.rb +8 -8
  95. data/test/tools/mongo_config.rb +270 -58
  96. data/test/tools/mongo_config_test.rb +146 -0
  97. data/test/unit/client_test.rb +230 -0
  98. data/test/unit/collection_test.rb +45 -32
  99. data/test/unit/connection_test.rb +82 -74
  100. data/test/unit/cursor_test.rb +14 -6
  101. data/test/unit/db_test.rb +8 -8
  102. data/test/unit/grid_test.rb +11 -11
  103. data/test/unit/node_test.rb +24 -24
  104. data/test/unit/pool_manager_test.rb +13 -13
  105. data/test/unit/pool_test.rb +1 -1
  106. data/test/unit/read_test.rb +21 -26
  107. data/test/unit/safe_test.rb +52 -33
  108. data/test/unit/util_test.rb +55 -0
  109. data/test/unit/write_concern_test.rb +161 -0
  110. metadata +158 -171
  111. data/docs/CREDITS.md +0 -123
  112. data/docs/FAQ.md +0 -116
  113. data/docs/GRID_FS.md +0 -158
  114. data/docs/HISTORY.md +0 -392
  115. data/docs/READ_PREFERENCE.md +0 -99
  116. data/docs/RELEASES.md +0 -54
  117. data/docs/REPLICA_SETS.md +0 -113
  118. data/docs/TAILABLE_CURSORS.md +0 -51
  119. data/docs/TUTORIAL.md +0 -356
  120. data/docs/WRITE_CONCERN.md +0 -31
  121. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  122. data/lib/mongo/version.rb +0 -3
  123. data/test/bson/test_helper.rb +0 -30
  124. data/test/replica_sets/basic_test.rb +0 -119
  125. data/test/replica_sets/complex_connect_test.rb +0 -57
  126. data/test/replica_sets/complex_read_preference_test.rb +0 -237
  127. data/test/replica_sets/connect_test.rb +0 -156
  128. data/test/replica_sets/cursor_test.rb +0 -70
  129. data/test/replica_sets/pooled_insert_test.rb +0 -57
  130. data/test/replica_sets/query_test.rb +0 -50
  131. data/test/replica_sets/read_preference_test.rb +0 -234
  132. data/test/replica_sets/refresh_test.rb +0 -156
  133. data/test/replica_sets/refresh_with_threads_test.rb +0 -60
  134. data/test/replica_sets/rs_test_helper.rb +0 -39
  135. data/test/safe_test.rb +0 -68
  136. data/test/sharded_cluster/mongo_config_test.rb +0 -126
  137. data/test/sharded_cluster/sc_test_helper.rb +0 -39
  138. data/test/tools/repl_set_manager.rb +0 -418
@@ -1,156 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
3
-
4
- class ConnectTest < Test::Unit::TestCase
5
- def setup
6
- @old_mongodb_uri = ENV['MONGODB_URI']
7
- ensure_rs
8
- end
9
-
10
- def teardown
11
- @rs.restart_killed_nodes
12
- @conn.close if defined?(@conn) && @conn
13
- ENV['MONGODB_URI'] = @old_mongodb_uri
14
- end
15
-
16
- def step_down_command
17
- # Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
18
- step_down_command = BSON::OrderedHash.new
19
- step_down_command[:replSetStepDown] = 60
20
- step_down_command[:force] = true
21
- step_down_command
22
- end
23
-
24
- # TODO: test connect timeout.
25
-
26
- def test_connect_with_deprecated_multi
27
- silently do
28
- @conn = Connection.multi([[@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]]], :name => @rs.name)
29
- end
30
- assert @conn.is_a?(ReplSetConnection)
31
- assert @conn.connected?
32
- end
33
-
34
- def test_connect_bad_name
35
- assert_raise_error(ReplicaSetConnectionError, "-wrong") do
36
- @conn = ReplSetConnection.new(build_seeds(3), :name => @rs.name + "-wrong")
37
- end
38
- end
39
-
40
- def test_connect_with_secondary_node_killed
41
- @rs.kill_secondary
42
-
43
- rescue_connection_failure do
44
- @conn = ReplSetConnection.new build_seeds(3)
45
- end
46
- assert @conn.connected?
47
- end
48
-
49
- def test_connect_with_third_node_killed
50
- @rs.kill(@rs.get_node_from_port(@rs.ports[2]))
51
-
52
- rescue_connection_failure do
53
- @conn = ReplSetConnection.new build_seeds(3)
54
- end
55
- assert @conn.connected?
56
- end
57
-
58
- def test_connect_with_primary_stepped_down
59
- @conn = ReplSetConnection.new build_seeds(3)
60
- @conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
61
- assert @conn[MONGO_TEST_DB]['bar'].find_one
62
-
63
- primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
64
- assert_raise Mongo::ConnectionFailure do
65
- primary['admin'].command(step_down_command)
66
- end
67
- assert @conn.connected?
68
-
69
- rescue_connection_failure do
70
- @conn[MONGO_TEST_DB]['bar'].find_one
71
- end
72
- end
73
-
74
- def test_save_with_primary_stepped_down
75
- @conn = ReplSetConnection.new build_seeds(3)
76
-
77
- primary = Mongo::Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
78
- assert_raise Mongo::ConnectionFailure do
79
- primary['admin'].command(step_down_command)
80
- end
81
-
82
- rescue_connection_failure do
83
- @conn[MONGO_TEST_DB]['bar'].save({:a => 1}, {:safe => {:w => 3}})
84
- end
85
- end
86
-
87
- def test_connect_with_connection_string
88
- @conn = Connection.from_uri("mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}")
89
- assert @conn.is_a?(ReplSetConnection)
90
- assert @conn.connected?
91
- end
92
-
93
- def test_connect_with_connection_string_in_env_var
94
- ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
95
- @conn = ReplSetConnection.new
96
- assert @conn.is_a?(ReplSetConnection)
97
- assert_equal 2, @conn.seeds.length
98
- assert_equal @rs.host, @conn.seeds[0][0]
99
- assert_equal @rs.host, @conn.seeds[1][0]
100
- assert_equal @rs.ports[0], @conn.seeds[0][1]
101
- assert_equal @rs.ports[1], @conn.seeds[1][1]
102
- assert @conn.connected?
103
- end
104
-
105
- def test_connect_with_connection_string_in_implicit_mongodb_uri
106
- ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
107
- @conn = Connection.from_uri
108
- assert @conn.is_a?(ReplSetConnection)
109
- assert_equal 2, @conn.seeds.length
110
- assert_equal @rs.host, @conn.seeds[0][0]
111
- assert_equal @rs.host, @conn.seeds[1][0]
112
- assert_equal @rs.ports[0], @conn.seeds[0][1]
113
- assert_equal @rs.ports[1], @conn.seeds[1][1]
114
- assert_equal @rs.name, @conn.replica_set_name
115
- assert @conn.connected?
116
- end
117
-
118
- def test_connect_with_new_seed_format
119
- @conn = ReplSetConnection.new build_seeds(3)
120
- assert @conn.connected?
121
- end
122
-
123
- def test_connect_with_old_seed_format
124
- silently do
125
- @conn = ReplSetConnection.new([@rs.host, @rs.ports[0]], [@rs.host, @rs.ports[1]], [@rs.host, @rs.ports[2]])
126
- end
127
- assert @conn.connected?
128
- end
129
-
130
- def test_connect_with_full_connection_string
131
- @conn = Connection.from_uri("mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true")
132
- assert @conn.is_a?(ReplSetConnection)
133
- assert @conn.connected?
134
- assert_equal 2, @conn.safe[:w]
135
- assert @conn.safe[:fsync]
136
- assert @conn.read_pool
137
- end
138
-
139
- def test_connect_with_full_connection_string_in_env_var
140
- ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true"
141
- @conn = ReplSetConnection.new
142
- assert @conn.is_a?(ReplSetConnection)
143
- assert @conn.connected?
144
- assert_equal 2, @conn.safe[:w]
145
- assert @conn.safe[:fsync]
146
- assert @conn.read_pool
147
- end
148
-
149
- def test_connect_options_override_env_var
150
- ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true"
151
- @conn = ReplSetConnection.new({:safe => false})
152
- assert @conn.is_a?(ReplSetConnection)
153
- assert @conn.connected?
154
- assert_equal @conn.safe, false
155
- end
156
- end
@@ -1,70 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
3
-
4
- class ReplicaSetCursorTest < Test::Unit::TestCase
5
- def setup
6
- ensure_rs
7
- end
8
-
9
- def test_cursors_get_closed
10
- setup_connection
11
- assert_cursor_count
12
- end
13
-
14
- def test_cursors_get_closed_secondary
15
- setup_connection(:secondary)
16
- assert_cursor_count
17
- end
18
-
19
- private
20
-
21
- def setup_connection(read=:primary)
22
- # Setup ReplicaSet Connection
23
- @replconn = Mongo::ReplSetConnection.new(
24
- build_seeds(3),
25
- :read => read
26
- )
27
-
28
- @db = @replconn.db(MONGO_TEST_DB)
29
- @db.drop_collection("cursor_tests")
30
- @coll = @db.collection("cursor_tests")
31
-
32
- @coll.insert({:a => 1}, :safe => true, :w => 3)
33
- @coll.insert({:b => 2}, :safe => true, :w => 3)
34
- @coll.insert({:c => 3}, :safe => true, :w => 3)
35
-
36
- # Pin reader
37
- @coll.find_one
38
-
39
- # Setup Direct Connections
40
- @primary = Mongo::Connection.new(*@replconn.manager.primary)
41
- @read = Mongo::Connection.new(*@replconn.manager.read)
42
- end
43
-
44
- def cursor_count(connection)
45
- connection['cursor_tests'].command({:cursorInfo => 1})['totalOpen']
46
- end
47
-
48
- def query_count(connection)
49
- connection['admin'].command({:serverStatus => 1})['opcounters']['query']
50
- end
51
-
52
- def assert_cursor_count
53
- before_primary = cursor_count(@primary)
54
- before_read = cursor_count(@read)
55
- before_query = query_count(@read)
56
-
57
- @coll.find.limit(2).to_a
58
- sleep(1)
59
-
60
- after_primary = cursor_count(@primary)
61
- after_read = cursor_count(@read)
62
- after_query = query_count(@read)
63
-
64
- assert_equal before_primary, after_primary
65
- assert_equal before_read, after_read
66
- assert_equal 1, after_query - before_query
67
- end
68
-
69
- end
70
-
@@ -1,57 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
3
-
4
- # NOTE: This test expects a replica set of three nodes to be running
5
- # on the local host.
6
- class ReplicaSetPooledInsertTest < Test::Unit::TestCase
7
-
8
- def setup
9
- ensure_rs
10
- @conn = ReplSetConnection.new(build_seeds(3), :pool_size => 10, :pool_timeout => 5, :refresh_mode => false)
11
- @db = @conn.db(MONGO_TEST_DB)
12
- @db.drop_collection("test-sets")
13
- @coll = @db.collection("test-sets")
14
- end
15
-
16
- def teardown
17
- @rs.restart_killed_nodes
18
- @conn.close if @conn
19
- end
20
-
21
- def test_insert
22
- expected_results = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
23
- @coll.save({:a => -1}, :safe => {:w => 2})
24
-
25
- @rs.kill_primary
26
-
27
- threads = []
28
- 10.times do |i|
29
- threads[i] = Thread.new do
30
- rescue_connection_failure do
31
- @coll.save({:a => i}, :safe => {:w => 2})
32
- end
33
- end
34
- end
35
-
36
- threads.each {|t| t.join}
37
-
38
- # Restart the old master and wait for sync
39
- @rs.restart_killed_nodes
40
- sleep(5)
41
- results = []
42
-
43
- rescue_connection_failure do
44
- @coll.find.each {|r| results << r}
45
- expected_results.each do |a|
46
- assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
47
- end
48
- end
49
-
50
- @coll.save({:a => 10}, :safe => {:w => 2})
51
- @coll.find.each {|r| results << r}
52
- (expected_results + [10]).each do |a|
53
- assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
54
- end
55
- end
56
-
57
- end
@@ -1,50 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
3
-
4
- class ReplicaSetQueryTest < Test::Unit::TestCase
5
-
6
- def setup
7
- ensure_rs
8
- @conn = ReplSetConnection.new build_seeds(1)
9
- @db = @conn.db(MONGO_TEST_DB)
10
- @db.drop_collection("test-sets")
11
- @coll = @db.collection("test-sets")
12
- end
13
-
14
- def teardown
15
- @rs.restart_killed_nodes
16
- @conn.close if @conn
17
- end
18
-
19
- def test_query
20
- @coll.save({:a => 20}, :safe => {:w => 3})
21
- @coll.save({:a => 30}, :safe => {:w => 3})
22
- @coll.save({:a => 40}, :safe => {:w => 3})
23
- results = []
24
- @coll.find.each {|r| results << r}
25
- [20, 30, 40].each do |a|
26
- assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
27
- end
28
-
29
- #puts "Benchmark before failover: #{benchmark_queries}"
30
-
31
- @rs.kill_primary
32
-
33
- results = []
34
- rescue_connection_failure do
35
- @coll.find.each {|r| results << r}
36
- [20, 30, 40].each do |a|
37
- assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
38
- end
39
-
40
- #puts "Benchmark after failover: #{benchmark_queries}"
41
- end
42
- end
43
-
44
- def benchmark_queries
45
- t1 = Time.now
46
- 10000.times { @coll.find_one }
47
- Time.now - t1
48
- end
49
-
50
- end
@@ -1,234 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
3
- require 'logger'
4
-
5
- class ReadPreferenceTest < Test::Unit::TestCase
6
-
7
- def setup
8
- ensure_rs(:secondary_count => 1, :arbiter_count => 1)
9
-
10
- # Insert data
11
- conn = Connection.new(@rs.host, @rs.primary[1])
12
- db = conn.db(MONGO_TEST_DB)
13
- coll = db.collection("test-sets")
14
- coll.save({:a => 20}, :safe => {:w => 2})
15
- end
16
-
17
- def test_read_primary
18
- conn = make_connection
19
- rescue_connection_failure do
20
- assert conn.read_primary?
21
- assert conn.primary?
22
- end
23
-
24
- conn = make_connection(:primary_preferred)
25
- rescue_connection_failure do
26
- assert conn.read_primary?
27
- assert conn.primary?
28
- end
29
-
30
- conn = make_connection(:secondary)
31
- rescue_connection_failure do
32
- assert !conn.read_primary?
33
- assert !conn.primary?
34
- end
35
-
36
- conn = make_connection(:secondary_preferred)
37
- rescue_connection_failure do
38
- assert !conn.read_primary?
39
- assert !conn.primary?
40
- end
41
- end
42
-
43
- def test_connection_pools
44
- conn = make_connection
45
- assert conn.primary_pool, "No primary pool!"
46
- assert conn.read_pool, "No read pool!"
47
- assert conn.primary_pool.port == conn.read_pool.port,
48
- "Primary port and read port are not the same!"
49
-
50
- conn = make_connection(:primary_preferred)
51
- assert conn.primary_pool, "No primary pool!"
52
- assert conn.read_pool, "No read pool!"
53
- assert conn.primary_pool.port == conn.read_pool.port,
54
- "Primary port and read port are not the same!"
55
-
56
- conn = make_connection(:secondary)
57
- assert conn.primary_pool, "No primary pool!"
58
- assert conn.read_pool, "No read pool!"
59
- assert conn.primary_pool.port != conn.read_pool.port,
60
- "Primary port and read port are the same!"
61
-
62
- conn = make_connection(:secondary_preferred)
63
- assert conn.primary_pool, "No primary pool!"
64
- assert conn.read_pool, "No read pool!"
65
- assert conn.primary_pool.port != conn.read_pool.port,
66
- "Primary port and read port are the same!"
67
- end
68
-
69
- def test_read_routing
70
- prepare_routing_test
71
-
72
- # Test that reads are going to the right members
73
- assert_query_route(@primary, @primary_direct)
74
- assert_query_route(@primary_preferred, @primary_direct)
75
- assert_query_route(@secondary, @secondary_direct)
76
- assert_query_route(@secondary_preferred, @secondary_direct)
77
- end
78
-
79
- def test_read_routing_with_primary_down
80
- prepare_routing_test
81
-
82
- # Test that reads are going to the right members
83
- assert_query_route(@primary, @primary_direct)
84
- assert_query_route(@primary_preferred, @primary_direct)
85
- assert_query_route(@secondary, @secondary_direct)
86
- assert_query_route(@secondary_preferred, @secondary_direct)
87
-
88
- # Kill the primary so only a single secondary exists
89
- @rs.kill_primary
90
-
91
- # Test that reads are going to the right members
92
- assert_raise_error ConnectionFailure do
93
- @primary[MONGO_TEST_DB]['test-sets'].find_one
94
- end
95
- assert_query_route(@primary_preferred, @secondary_direct)
96
- assert_query_route(@secondary, @secondary_direct)
97
- assert_query_route(@secondary_preferred, @secondary_direct)
98
-
99
- # Restore set
100
- @rs.restart_killed_nodes
101
- sleep(1)
102
- @repl_cons.each { |con| con.refresh }
103
- sleep(1)
104
- @primary_direct = Connection.new(
105
- @rs.host,
106
- @primary.read_pool.port
107
- )
108
-
109
- # Test that reads are going to the right members
110
- assert_query_route(@primary, @primary_direct)
111
- assert_query_route(@primary_preferred, @primary_direct)
112
- assert_query_route(@secondary, @secondary_direct)
113
- assert_query_route(@secondary_preferred, @secondary_direct)
114
- end
115
-
116
- def test_read_routing_with_secondary_down
117
- prepare_routing_test
118
-
119
- # Test that reads are going to the right members
120
- assert_query_route(@primary, @primary_direct)
121
- assert_query_route(@primary_preferred, @primary_direct)
122
- assert_query_route(@secondary, @secondary_direct)
123
- assert_query_route(@secondary_preferred, @secondary_direct)
124
-
125
- # Kill the secondary so that only primary exists
126
- @rs.kill_secondary
127
-
128
- # Test that reads are going to the right members
129
- assert_query_route(@primary, @primary_direct)
130
- assert_query_route(@primary_preferred, @primary_direct)
131
- assert_raise_error ConnectionFailure do
132
- @secondary[MONGO_TEST_DB]['test-sets'].find_one
133
- end
134
- assert_query_route(@secondary_preferred, @primary_direct)
135
-
136
- # Restore set
137
- @rs.restart_killed_nodes
138
- sleep(1)
139
- @repl_cons.each { |con| con.refresh }
140
- sleep(1)
141
- @secondary_direct = Connection.new(
142
- @rs.host,
143
- @secondary.read_pool.port,
144
- :slave_ok => true
145
- )
146
-
147
- # Test that reads are going to the right members
148
- assert_query_route(@primary, @primary_direct)
149
- assert_query_route(@primary_preferred, @primary_direct)
150
- assert_query_route(@secondary, @secondary_direct)
151
- assert_query_route(@secondary_preferred, @secondary_direct)
152
- end
153
-
154
- def test_write_conecern
155
- @conn = make_connection(:secondary_preferred)
156
- @db = @conn[MONGO_TEST_DB]
157
- @coll = @db.collection("test-sets", :safe => {
158
- :w => 2, :wtimeout => 20000
159
- })
160
- @coll.save({:a => 20})
161
- @coll.save({:a => 30})
162
- @coll.save({:a => 40})
163
-
164
- # pin the read pool
165
- @coll.find_one
166
- @secondary = Connection.new(@rs.host, @conn.read_pool.port, :slave_ok => true)
167
-
168
- results = []
169
- @coll.find.each {|r| results << r["a"]}
170
-
171
- assert results.include?(20)
172
- assert results.include?(30)
173
- assert results.include?(40)
174
-
175
- @rs.kill_primary
176
-
177
- results = []
178
- rescue_connection_failure do
179
- @coll.find.each {|r| results << r}
180
- [20, 30, 40].each do |a|
181
- assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
182
- end
183
- end
184
- @rs.restart_killed_nodes
185
- end
186
-
187
- def test_write_lots_of_data
188
- @conn = make_connection(:secondary_preferred)
189
- @db = @conn[MONGO_TEST_DB]
190
- @coll = @db.collection("test-sets", {:safe => {:w => 2}})
191
-
192
- 6000.times do |n|
193
- @coll.save({:a => n})
194
- end
195
-
196
- cursor = @coll.find()
197
- cursor.next
198
- cursor.close
199
- end
200
-
201
- private
202
-
203
- def prepare_routing_test
204
- # Setup replica set connections
205
- @primary = make_connection(:primary)
206
- @primary_preferred = make_connection(:primary_preferred)
207
- @secondary = make_connection(:secondary)
208
- @secondary_preferred = make_connection(:secondary_preferred)
209
- @repl_cons = [@primary, @primary_preferred, @secondary, @secondary_preferred]
210
-
211
- # Setup direct connections
212
- @primary_direct = Connection.new(@rs.host, @primary.read_pool.port)
213
- @secondary_direct = Connection.new(@rs.host, @secondary.read_pool.port, :slave_ok => true)
214
- end
215
-
216
- def make_connection(mode = :primary, opts = {})
217
- opts.merge!({:read => mode})
218
- ReplSetConnection.new(build_seeds(3), opts)
219
- end
220
-
221
- def query_count(connection)
222
- connection['admin'].command({:serverStatus => 1})['opcounters']['query']
223
- end
224
-
225
- def assert_query_route(test_connection, expected_target)
226
- #puts "#{test_connection.read_pool.port} #{expected_target.read_pool.port}"
227
- queries_before = query_count(expected_target)
228
- assert_nothing_raised do
229
- test_connection['MONGO_TEST_DB']['test-sets'].find_one
230
- end
231
- queries_after = query_count(expected_target)
232
- assert_equal 1, queries_after - queries_before
233
- end
234
- end