mongo 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class ComplexConnectTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ ensure_cluster(:rs)
7
+ end
8
+
9
+ def self.shutdown
10
+ @@cluster.stop
11
+ @@cluster.clobber
12
+ end
13
+
14
+ def teardown
15
+ @client.close if defined?(@conn) && @conn
16
+ end
17
+
18
+ def test_complex_connect
19
+ host = @rs.servers.first.host
20
+ primary = MongoClient.new(host, @rs.primary.port)
21
+
22
+ @client = MongoReplicaSetClient.new([
23
+ @rs.servers[2].host_port,
24
+ @rs.servers[1].host_port,
25
+ @rs.servers[0].host_port
26
+ ])
27
+
28
+ version = @client.server_version
29
+
30
+ @client['test']['foo'].insert({:a => 1})
31
+ assert @client['test']['foo'].find_one
32
+
33
+ config = primary['local']['system.replset'].find_one
34
+ 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
36
+ config['members'].delete_if do |member|
37
+ member['host'].include?(port_to_delete)
38
+ end
39
+
40
+ assert_raise ConnectionFailure do
41
+ primary['admin'].command({:replSetReconfig => config})
42
+ end
43
+ @rs.start
44
+
45
+ force_stepdown = BSON::OrderedHash.new
46
+ force_stepdown[:replSetStepDown] = 1
47
+ force_stepdown[:force] = true
48
+
49
+ assert_raise ConnectionFailure do
50
+ primary['admin'].command(force_stepdown)
51
+ end
52
+
53
+ # isMaster is currently broken in 2.1+ when called on removed nodes
54
+ if version < "2.1"
55
+ rescue_connection_failure do
56
+ assert @client['test']['foo'].find_one
57
+ end
58
+
59
+ assert @client['test']['foo'].find_one
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,255 @@
1
+ require 'test_helper'
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ ensure_cluster(:rs)
7
+ @connection = nil
8
+ end
9
+
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
+ 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
+ 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)
42
+ end
43
+ assert !@connection.nil?
44
+ assert @connection.connected?
45
+ end
46
+
47
+ def test_connect_bad_name
48
+ assert_raise_error(ReplicaSetConnectionError, "-wrong") do
49
+ @connection = ReplSetConnection.new(@rs.repl_set_seeds, :safe => true, :name => @rs.repl_set_name + "-wrong")
50
+ end
51
+ end
52
+
53
+ def test_connect_with_first_secondary_node_terminated
54
+ @rs.secondaries.first.stop
55
+
56
+ rescue_connection_failure do
57
+ @connection = ReplSetConnection.new @rs.repl_set_seeds
58
+ end
59
+ assert @connection.connected?
60
+ end
61
+
62
+ def test_connect_with_last_secondary_node_terminated
63
+ @rs.secondaries.last.stop
64
+
65
+ rescue_connection_failure do
66
+ @connection = ReplSetConnection.new @rs.repl_set_seeds
67
+ end
68
+ assert @connection.connected?
69
+ end
70
+
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
+ 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}")
186
+ assert !@connection.nil?
187
+ assert @connection.connected?
188
+ end
189
+
190
+ 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}"
192
+ @connection = ReplSetConnection.new
193
+ assert !@connection.nil?
194
+ assert_equal 2, @connection.seeds.length
195
+ assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
196
+ assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
197
+ assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
198
+ assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
199
+ assert_equal @rs.repl_set_name, @connection.replica_set_name
200
+ assert @connection.connected?
201
+ end
202
+
203
+ 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}"
205
+ @connection = Connection.from_uri
206
+ assert !@connection.nil?
207
+ assert_equal 2, @connection.seeds.length
208
+ assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
209
+ assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
210
+ assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
211
+ assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
212
+ assert_equal @rs.repl_set_name, @connection.replica_set_name
213
+ assert @connection.connected?
214
+ end
215
+
216
+ def test_connect_with_new_seed_format
217
+ @connection = ReplSetConnection.new @rs.repl_set_seeds
218
+ assert @connection.connected?
219
+ end
220
+
221
+ def test_connect_with_old_seed_format
222
+ silently do
223
+ @connection = ReplSetConnection.new(@rs.replicas[0].host_port_a, @rs.replicas[1].host_port_a)
224
+ end
225
+ assert @connection.connected?
226
+ end
227
+
228
+ 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")
230
+ assert !@connection.nil?
231
+ assert @connection.connected?
232
+ assert_equal 2, @connection.write_concern[:w]
233
+ assert @connection.write_concern[:fsync]
234
+ assert @connection.read_pool
235
+ end
236
+
237
+ 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"
239
+ @connection = ReplSetConnection.new
240
+ assert !@connection.nil?
241
+ assert @connection.connected?
242
+ assert_equal 2, @connection.write_concern[:w]
243
+ assert @connection.write_concern[:fsync]
244
+ assert @connection.read_pool
245
+ end
246
+
247
+ 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"
249
+ @connection = ReplSetConnection.new({:safe => {:w => 1}})
250
+ assert !@connection.nil?
251
+ assert @connection.connected?
252
+ assert_equal 1, @connection.write_concern[:w]
253
+ end
254
+
255
+ end
@@ -1,40 +1,43 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/replica_sets/rs_test_helper'
1
+ require 'test_helper'
3
2
 
4
3
  class ReplicaSetCountTest < Test::Unit::TestCase
5
4
 
6
5
  def setup
7
- ensure_rs
8
- @conn = ReplSetConnection.new(build_seeds(3), :read => :primary_preferred)
9
- assert @conn.primary_pool
10
- @primary = Connection.new(@conn.primary_pool.host, @conn.primary_pool.port)
11
- @db = @conn.db(MONGO_TEST_DB)
6
+ ensure_cluster(:rs)
7
+ @client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => :primary_preferred)
8
+ assert @client.primary_pool
9
+ @primary = MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
10
+ @db = @client.db(MONGO_TEST_DB)
12
11
  @db.drop_collection("test-sets")
13
12
  @coll = @db.collection("test-sets")
14
13
  end
15
14
 
16
15
  def teardown
17
- @rs.restart_killed_nodes
18
- @conn.close if @conn
16
+ @client.close if @conn
17
+ end
18
+
19
+ def self.shutdown
20
+ @@cluster.stop
21
+ @@cluster.clobber
19
22
  end
20
23
 
21
24
  def test_correct_count_after_insertion_reconnect
22
- @coll.insert({:a => 20}, :safe => {:w => 2, :wtimeout => 10000})
25
+ @coll.insert({:a => 20}, :w => 2, :wtimeout => 10000)
23
26
  assert_equal 1, @coll.count
24
27
 
25
28
  # Kill the current master node
26
- @node = @rs.kill_primary
29
+ @rs.primary.stop
27
30
 
28
31
  rescue_connection_failure do
29
- @coll.insert({:a => 30}, :safe => true)
32
+ @coll.insert({:a => 30})
30
33
  end
31
34
 
32
- @coll.insert({:a => 40}, :safe => true)
35
+ @coll.insert({:a => 40})
33
36
  assert_equal 3, @coll.count, "Second count failed"
34
37
  end
35
38
 
36
39
  def test_count_command_sent_to_primary
37
- @coll.insert({:a => 20}, :safe => {:w => 2, :wtimeout => 10000})
40
+ @coll.insert({:a => 20}, :w => 2, :wtimeout => 10000)
38
41
  count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
39
42
  assert_equal 1, @coll.count
40
43
  count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class ReplicaSetCursorTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ ensure_cluster(:rs)
7
+ end
8
+
9
+ def self.shutdown
10
+ @@cluster.stop
11
+ @@cluster.clobber
12
+ end
13
+
14
+ def test_cursors_get_closed
15
+ setup_client
16
+ assert_cursor_count
17
+ end
18
+
19
+ #def test_cursors_get_closed_secondary
20
+ # setup_client(:secondary)
21
+ # assert_cursor_count
22
+ #end
23
+
24
+ private
25
+
26
+ def setup_client(read=:primary)
27
+ # Setup ReplicaSet Connection
28
+ @client = MongoReplicaSetClient.new(
29
+ @rs.repl_set_seeds,
30
+ :read => read
31
+ )
32
+
33
+ @db = @client.db(MONGO_TEST_DB)
34
+ @db.drop_collection("cursor_tests")
35
+ @coll = @db.collection("cursor_tests")
36
+
37
+ @coll.insert({:a => 1}, :w => 3)
38
+ @coll.insert({:b => 2}, :w => 3)
39
+ @coll.insert({:c => 3}, :w => 3)
40
+
41
+ # Pin reader
42
+ @coll.find_one
43
+
44
+ # Setup Direct Connections
45
+ @primary = Mongo::MongoClient.new(*@client.manager.primary)
46
+ @read = Mongo::MongoClient.new(*@client.manager.read)
47
+ end
48
+
49
+ def cursor_count(client)
50
+ client['cursor_tests'].command({:cursorInfo => 1})['totalOpen']
51
+ end
52
+
53
+ def query_count(client)
54
+ client['admin'].command({:serverStatus => 1})['opcounters']['query']
55
+ end
56
+
57
+ def assert_cursor_count
58
+ before_primary = cursor_count(@primary)
59
+ before_read = cursor_count(@read)
60
+ before_query = query_count(@read)
61
+
62
+ @coll.find.limit(2).to_a
63
+ sleep(1)
64
+
65
+ after_primary = cursor_count(@primary)
66
+ after_read = cursor_count(@read)
67
+ after_query = query_count(@read)
68
+
69
+ assert_equal before_primary, after_primary
70
+ assert_equal before_read, after_read
71
+ assert_equal 1, after_query - before_query
72
+ end
73
+
74
+ end
75
+