mongo 1.3.0 → 1.12.5
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/{LICENSE.txt → LICENSE} +1 -1
- data/README.md +122 -271
- data/Rakefile +25 -209
- data/VERSION +1 -0
- data/bin/mongo_console +31 -9
- data/lib/mongo/bulk_write_collection_view.rb +387 -0
- data/lib/mongo/collection.rb +576 -269
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/connection/node.rb +249 -0
- data/lib/mongo/connection/pool.rb +340 -0
- data/lib/mongo/connection/pool_manager.rb +320 -0
- data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection.rb +7 -875
- data/lib/mongo/cursor.rb +403 -117
- data/lib/mongo/db.rb +444 -243
- data/lib/mongo/exception.rb +145 -0
- data/lib/mongo/functional/authentication.rb +455 -0
- data/lib/mongo/functional/logging.rb +85 -0
- data/lib/mongo/functional/read_preference.rb +183 -0
- data/lib/mongo/functional/scram.rb +556 -0
- data/lib/mongo/functional/uri_parser.rb +409 -0
- data/lib/mongo/functional/write_concern.rb +66 -0
- data/lib/mongo/functional.rb +20 -0
- data/lib/mongo/gridfs/grid.rb +30 -24
- data/lib/mongo/gridfs/grid_ext.rb +6 -10
- data/lib/mongo/gridfs/grid_file_system.rb +38 -20
- data/lib/mongo/gridfs/grid_io.rb +84 -75
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/legacy.rb +140 -0
- data/lib/mongo/mongo_client.rb +697 -0
- data/lib/mongo/mongo_replica_set_client.rb +535 -0
- data/lib/mongo/mongo_sharded_client.rb +159 -0
- data/lib/mongo/networking.rb +372 -0
- data/lib/mongo/{util → utils}/conversions.rb +29 -8
- data/lib/mongo/{util → utils}/core_ext.rb +28 -18
- data/lib/mongo/{util → utils}/server_version.rb +4 -6
- data/lib/mongo/{util → utils}/support.rb +29 -31
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo.rb +51 -50
- data/mongo.gemspec +29 -32
- data/test/functional/authentication_test.rb +39 -0
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1198 -0
- data/test/functional/client_test.rb +627 -0
- data/test/functional/collection_test.rb +2175 -0
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
- data/test/functional/cursor_fail_test.rb +57 -0
- data/test/functional/cursor_message_test.rb +56 -0
- data/test/functional/cursor_test.rb +683 -0
- data/test/functional/db_api_test.rb +835 -0
- data/test/functional/db_connection_test.rb +25 -0
- data/test/functional/db_test.rb +348 -0
- data/test/functional/grid_file_system_test.rb +285 -0
- data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
- data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
- data/test/functional/pool_test.rb +136 -0
- data/test/functional/safe_test.rb +98 -0
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +62 -0
- data/test/functional/timeout_test.rb +60 -0
- data/test/functional/uri_test.rb +446 -0
- data/test/functional/write_concern_test.rb +118 -0
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +476 -0
- data/test/replica_set/authentication_test.rb +37 -0
- data/test/replica_set/basic_test.rb +189 -0
- data/test/replica_set/client_test.rb +393 -0
- data/test/replica_set/connection_test.rb +138 -0
- data/test/replica_set/count_test.rb +66 -0
- data/test/replica_set/cursor_test.rb +220 -0
- data/test/replica_set/insert_test.rb +157 -0
- data/test/replica_set/max_values_test.rb +151 -0
- data/test/replica_set/pinning_test.rb +105 -0
- data/test/replica_set/query_test.rb +73 -0
- data/test/replica_set/read_preference_test.rb +219 -0
- data/test/replica_set/refresh_test.rb +211 -0
- data/test/replica_set/replication_ack_test.rb +95 -0
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +203 -0
- data/test/shared/authentication/basic_auth_shared.rb +260 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
- data/test/shared/authentication/gssapi_shared.rb +176 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/authentication/scram_shared.rb +92 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +53 -94
- data/test/threading/basic_test.rb +120 -0
- data/test/tools/mongo_config.rb +708 -0
- data/test/tools/mongo_config_test.rb +160 -0
- data/test/unit/client_test.rb +381 -0
- data/test/unit/collection_test.rb +89 -53
- data/test/unit/connection_test.rb +282 -32
- data/test/unit/cursor_test.rb +206 -8
- data/test/unit/db_test.rb +55 -13
- data/test/unit/grid_test.rb +43 -16
- data/test/unit/mongo_sharded_client_test.rb +48 -0
- data/test/unit/node_test.rb +93 -0
- data/test/unit/pool_manager_test.rb +111 -0
- data/test/unit/read_pref_test.rb +406 -0
- data/test/unit/read_test.rb +159 -0
- data/test/unit/safe_test.rb +69 -36
- data/test/unit/sharding_pool_manager_test.rb +84 -0
- data/test/unit/write_concern_test.rb +175 -0
- data.tar.gz.sig +3 -0
- metadata +227 -216
- metadata.gz.sig +0 -0
- data/docs/CREDITS.md +0 -123
- data/docs/FAQ.md +0 -116
- data/docs/GridFS.md +0 -158
- data/docs/HISTORY.md +0 -244
- data/docs/RELEASES.md +0 -33
- data/docs/REPLICA_SETS.md +0 -72
- data/docs/TUTORIAL.md +0 -247
- data/docs/WRITE_CONCERN.md +0 -28
- data/lib/mongo/exceptions.rb +0 -71
- data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
- data/lib/mongo/repl_set_connection.rb +0 -342
- data/lib/mongo/test.rb +0 -20
- data/lib/mongo/util/pool.rb +0 -177
- data/lib/mongo/util/uri_parser.rb +0 -185
- data/test/async/collection_test.rb +0 -224
- data/test/async/connection_test.rb +0 -24
- data/test/async/cursor_test.rb +0 -162
- data/test/async/worker_pool_test.rb +0 -99
- data/test/auxillary/1.4_features.rb +0 -166
- data/test/auxillary/authentication_test.rb +0 -68
- data/test/auxillary/autoreconnect_test.rb +0 -41
- data/test/auxillary/fork_test.rb +0 -30
- data/test/auxillary/repl_set_auth_test.rb +0 -58
- data/test/auxillary/slave_connection_test.rb +0 -36
- data/test/auxillary/threaded_authentication_test.rb +0 -101
- data/test/bson/binary_test.rb +0 -15
- data/test/bson/bson_test.rb +0 -649
- data/test/bson/byte_buffer_test.rb +0 -208
- data/test/bson/hash_with_indifferent_access_test.rb +0 -38
- data/test/bson/json_test.rb +0 -17
- data/test/bson/object_id_test.rb +0 -154
- data/test/bson/ordered_hash_test.rb +0 -204
- data/test/bson/timestamp_test.rb +0 -24
- data/test/collection_test.rb +0 -910
- data/test/connection_test.rb +0 -309
- data/test/cursor_fail_test.rb +0 -75
- data/test/cursor_message_test.rb +0 -43
- data/test/cursor_test.rb +0 -483
- data/test/db_api_test.rb +0 -726
- data/test/db_connection_test.rb +0 -15
- data/test/db_test.rb +0 -287
- data/test/grid_file_system_test.rb +0 -243
- data/test/load/resque/load.rb +0 -21
- data/test/load/resque/processor.rb +0 -26
- data/test/load/thin/load.rb +0 -24
- data/test/load/unicorn/load.rb +0 -23
- data/test/load/unicorn/unicorn.rb +0 -29
- data/test/replica_sets/connect_test.rb +0 -94
- data/test/replica_sets/connection_string_test.rb +0 -32
- data/test/replica_sets/count_test.rb +0 -35
- data/test/replica_sets/insert_test.rb +0 -53
- data/test/replica_sets/pooled_insert_test.rb +0 -55
- data/test/replica_sets/query_secondaries.rb +0 -96
- data/test/replica_sets/query_test.rb +0 -51
- data/test/replica_sets/replication_ack_test.rb +0 -66
- data/test/replica_sets/rs_test_helper.rb +0 -27
- data/test/safe_test.rb +0 -68
- data/test/support/hash_with_indifferent_access.rb +0 -186
- data/test/support/keys.rb +0 -45
- data/test/support_test.rb +0 -18
- data/test/threading/threading_with_large_pool_test.rb +0 -90
- data/test/threading_test.rb +0 -87
- data/test/tools/auth_repl_set_manager.rb +0 -14
- data/test/tools/load.rb +0 -58
- data/test/tools/repl_set_manager.rb +0 -266
- data/test/tools/sharding_manager.rb +0 -202
- data/test/tools/test.rb +0 -4
- data/test/unit/pool_test.rb +0 -9
- data/test/unit/repl_set_connection_test.rb +0 -59
- data/test/uri_test.rb +0 -91
@@ -0,0 +1,393 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetClientTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
@client = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def teardown
|
25
|
+
@client.close if @client
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_reconnection
|
29
|
+
@client = MongoReplicaSetClient.from_uri(@uri, :op_timeout => TEST_OP_TIMEOUT)
|
30
|
+
assert @client.connected?
|
31
|
+
|
32
|
+
manager = @client.local_manager
|
33
|
+
|
34
|
+
@client.close
|
35
|
+
assert !@client.connected?
|
36
|
+
assert !@client.local_manager
|
37
|
+
|
38
|
+
@client.connect
|
39
|
+
assert @client.connected?
|
40
|
+
assert_equal @client.local_manager, manager
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_reconnect_method
|
44
|
+
@client = MongoReplicaSetClient.from_uri(@uri, :op_timeout => TEST_OP_TIMEOUT)
|
45
|
+
assert @client.connected?
|
46
|
+
|
47
|
+
manager = @client.local_manager
|
48
|
+
|
49
|
+
@client.close
|
50
|
+
assert !@client.connected?
|
51
|
+
assert !@client.local_manager
|
52
|
+
|
53
|
+
@client.reconnect
|
54
|
+
assert @client.connected?
|
55
|
+
assert_equal @client.local_manager, manager
|
56
|
+
end
|
57
|
+
|
58
|
+
# TODO: test connect timeout.
|
59
|
+
|
60
|
+
def test_connect_with_deprecated_multi
|
61
|
+
silently do
|
62
|
+
# guaranteed to have one data-holding member
|
63
|
+
@client = MongoClient.multi(@rs.repl_set_seeds_old, :name => @rs.repl_set_name)
|
64
|
+
end
|
65
|
+
assert !@client.nil?
|
66
|
+
assert @client.connected?
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_connect_bad_name
|
70
|
+
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
|
71
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :name => @rs.repl_set_name + "-wrong")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_connect_with_first_secondary_node_terminated
|
76
|
+
@rs.secondaries.first.stop
|
77
|
+
|
78
|
+
rescue_connection_failure do
|
79
|
+
@client = MongoReplicaSetClient.new @rs.repl_set_seeds
|
80
|
+
end
|
81
|
+
assert @client.connected?
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_connect_with_last_secondary_node_terminated
|
85
|
+
@rs.secondaries.last.stop
|
86
|
+
|
87
|
+
rescue_connection_failure do
|
88
|
+
@client = MongoReplicaSetClient.new @rs.repl_set_seeds
|
89
|
+
end
|
90
|
+
assert @client.connected?
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_connect_with_primary_stepped_down
|
94
|
+
@client = MongoReplicaSetClient.from_uri(@uri)
|
95
|
+
@client[TEST_DB]['bar'].save({:a => 1}, {:w => 3})
|
96
|
+
assert @client[TEST_DB]['bar'].find_one
|
97
|
+
|
98
|
+
primary = Mongo::MongoClient.new(*@client.primary)
|
99
|
+
authenticate_client(primary)
|
100
|
+
assert_raise Mongo::ConnectionFailure do
|
101
|
+
perform_step_down(primary)
|
102
|
+
end
|
103
|
+
assert @client.connected?
|
104
|
+
|
105
|
+
rescue_connection_failure do
|
106
|
+
@client[TEST_DB]['bar'].find_one
|
107
|
+
end
|
108
|
+
@client[TEST_DB]['bar'].find_one
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_connect_with_primary_killed
|
112
|
+
@client = MongoReplicaSetClient.from_uri(@uri)
|
113
|
+
assert @client.connected?
|
114
|
+
@client[TEST_DB]['bar'].save({:a => 1}, {:w => 3})
|
115
|
+
assert @client[TEST_DB]['bar'].find_one
|
116
|
+
|
117
|
+
@rs.primary.kill(Signal.list['KILL'])
|
118
|
+
|
119
|
+
sleep(3)
|
120
|
+
|
121
|
+
rescue_connection_failure do
|
122
|
+
@client[TEST_DB]['bar'].find_one
|
123
|
+
end
|
124
|
+
@client[TEST_DB]['bar'].find_one
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_save_with_primary_stepped_down
|
128
|
+
@client = MongoReplicaSetClient.from_uri(@uri)
|
129
|
+
assert @client.connected?
|
130
|
+
|
131
|
+
primary = Mongo::MongoClient.new(*@client.primary)
|
132
|
+
authenticate_client(primary)
|
133
|
+
assert_raise Mongo::ConnectionFailure do
|
134
|
+
perform_step_down(primary)
|
135
|
+
end
|
136
|
+
|
137
|
+
rescue_connection_failure do
|
138
|
+
@client[TEST_DB]['bar'].save({:a => 1}, {:w => 2})
|
139
|
+
end
|
140
|
+
@client[TEST_DB]['bar'].find_one
|
141
|
+
end
|
142
|
+
|
143
|
+
# def test_connect_with_first_node_removed
|
144
|
+
# @client = MongoReplicaSetClient.from_uri(@uri)
|
145
|
+
# @client[TEST_DB]['bar'].save({:a => 1}, {:w => 3})
|
146
|
+
|
147
|
+
# # Make sure everyone's views of optimes are caught up
|
148
|
+
# loop do
|
149
|
+
# break if @rs.repl_set_get_status.all? do |status|
|
150
|
+
# members = status['members']
|
151
|
+
# primary_optime = members.find{|m| m['state'] == 1}['optime'].seconds
|
152
|
+
# members.any?{|m| m['state'] == 2 && primary_optime - m['optime'].seconds < 5}
|
153
|
+
# end
|
154
|
+
# sleep 1
|
155
|
+
# end
|
156
|
+
|
157
|
+
# old_primary = [@client.primary_pool.host, @client.primary_pool.port]
|
158
|
+
# old_primary_conn = Mongo::MongoClient.new(*old_primary)
|
159
|
+
|
160
|
+
# assert_raise Mongo::ConnectionFailure do
|
161
|
+
# perform_step_down(old_primary_conn)
|
162
|
+
# end
|
163
|
+
|
164
|
+
# # Wait for new primary
|
165
|
+
# rescue_connection_failure do
|
166
|
+
# sleep 1 until @rs.primary
|
167
|
+
# end
|
168
|
+
|
169
|
+
# new_primary = [@rs.primary.host, @rs.primary.port]
|
170
|
+
# new_primary_conn = Mongo::MongoClient.new(*new_primary)
|
171
|
+
|
172
|
+
# assert new_primary != old_primary
|
173
|
+
|
174
|
+
# config = nil
|
175
|
+
|
176
|
+
# # Remove old primary from replset
|
177
|
+
# rescue_connection_failure do
|
178
|
+
# config = @client['local']['system.replset'].find_one
|
179
|
+
# end
|
180
|
+
|
181
|
+
# old_member = config['members'].select {|m| m['host'] == old_primary.join(':')}.first
|
182
|
+
# config['members'].reject! {|m| m['host'] == old_primary.join(':')}
|
183
|
+
# config['version'] += 1
|
184
|
+
|
185
|
+
# begin
|
186
|
+
# new_primary_conn['admin'].command({'replSetReconfig' => config})
|
187
|
+
# rescue Mongo::ConnectionFailure
|
188
|
+
# end
|
189
|
+
|
190
|
+
# # Wait for the dust to settle
|
191
|
+
# rescue_connection_failure do
|
192
|
+
# assert @client[TEST_DB]['bar'].find_one
|
193
|
+
# end
|
194
|
+
|
195
|
+
# begin
|
196
|
+
# # Make sure a new connection skips the old primary
|
197
|
+
# @new_conn = MongoReplicaSetClient.new @rs.repl_set_seeds
|
198
|
+
# @new_conn.connect
|
199
|
+
# new_nodes = @new_conn.secondaries + [@new_conn.primary]
|
200
|
+
# assert !new_nodes.include?(old_primary)
|
201
|
+
# ensure
|
202
|
+
# # Add the old primary back
|
203
|
+
# config['members'] << old_member
|
204
|
+
# config['version'] += 1
|
205
|
+
|
206
|
+
# begin
|
207
|
+
# new_primary_conn['admin'].command({'replSetReconfig' => config})
|
208
|
+
# rescue Mongo::ConnectionFailure
|
209
|
+
# end
|
210
|
+
# end
|
211
|
+
# end
|
212
|
+
|
213
|
+
def test_connect_with_hung_first_node
|
214
|
+
hung_node = nil
|
215
|
+
begin
|
216
|
+
hung_node = IO.popen('nc -lk 127.0.0.1 29999 >/dev/null 2>&1')
|
217
|
+
|
218
|
+
Timeout.timeout(3) do
|
219
|
+
@client = MongoReplicaSetClient.new(['localhost:29999'] + @rs.repl_set_seeds,
|
220
|
+
:connect_timeout => 2)
|
221
|
+
@client.connect
|
222
|
+
end
|
223
|
+
assert ['localhost:29999'] != @client.primary
|
224
|
+
assert !@client.secondaries.include?('localhost:29999')
|
225
|
+
ensure
|
226
|
+
begin
|
227
|
+
Process.kill("KILL", hung_node.pid) if hung_node
|
228
|
+
rescue
|
229
|
+
# the process ended, was killed already, or the system doesn't support nc
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_read_pref_primary_with_tags
|
235
|
+
parser = Mongo::URIParser.new("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}" +
|
236
|
+
"?replicaset=#{@rs.repl_set_name}&readPreference=primary&" +
|
237
|
+
"readPreferenceTags=dc:ny,rack:1")
|
238
|
+
assert_raise_error Mongo::MongoArgumentError do
|
239
|
+
parser.connection.read_pool
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_read_pref_with_tags
|
244
|
+
parser = Mongo::URIParser.new("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}" +
|
245
|
+
"?replicaset=#{@rs.repl_set_name}&" +
|
246
|
+
"readPreferenceTags=dc:ny,rack:1")
|
247
|
+
assert_raise_error Mongo::MongoArgumentError do
|
248
|
+
parser.connection.read_pool
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_connect_with_connection_string
|
253
|
+
@client = MongoClient.from_uri("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}")
|
254
|
+
assert !@client.nil?
|
255
|
+
assert @client.connected?
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_connect_with_connection_string_in_env_var
|
259
|
+
uri = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}"
|
260
|
+
with_preserved_env_uri(uri) do
|
261
|
+
@client = MongoReplicaSetClient.new
|
262
|
+
assert !@client.nil?
|
263
|
+
assert_equal 2, @client.seeds.length
|
264
|
+
assert_equal @rs.replicas[0].host, @client.seeds[0][0]
|
265
|
+
assert_equal @rs.replicas[1].host, @client.seeds[1][0]
|
266
|
+
assert_equal @rs.replicas[0].port, @client.seeds[0][1]
|
267
|
+
assert_equal @rs.replicas[1].port, @client.seeds[1][1]
|
268
|
+
assert_equal @rs.repl_set_name, @client.replica_set_name
|
269
|
+
assert @client.connected?
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_connect_with_connection_string_in_implicit_mongodb_uri
|
274
|
+
uri = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name}"
|
275
|
+
with_preserved_env_uri(uri) do
|
276
|
+
@client = MongoClient.from_uri
|
277
|
+
assert !@client.nil?
|
278
|
+
assert_equal 2, @client.seeds.length
|
279
|
+
assert_equal @rs.replicas[0].host, @client.seeds[0][0]
|
280
|
+
assert_equal @rs.replicas[1].host, @client.seeds[1][0]
|
281
|
+
assert_equal @rs.replicas[0].port, @client.seeds[0][1]
|
282
|
+
assert_equal @rs.replicas[1].port, @client.seeds[1][1]
|
283
|
+
assert_equal @rs.repl_set_name, @client.replica_set_name
|
284
|
+
assert @client.connected?
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_connect_with_new_seed_format
|
289
|
+
@client = MongoReplicaSetClient.new @rs.repl_set_seeds
|
290
|
+
assert @client.connected?
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_connect_with_old_seed_format
|
294
|
+
silently do
|
295
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds_old)
|
296
|
+
end
|
297
|
+
assert @client.connected?
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_connect_with_full_connection_string
|
301
|
+
@client = MongoClient.from_uri("mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};w=2;fsync=true;slaveok=true")
|
302
|
+
assert !@client.nil?
|
303
|
+
assert @client.connected?
|
304
|
+
assert_equal 2, @client.write_concern[:w]
|
305
|
+
assert @client.write_concern[:fsync]
|
306
|
+
assert @client.read_pool
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_connect_with_full_connection_string_in_env_var
|
310
|
+
uri = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};w=2;fsync=true;slaveok=true"
|
311
|
+
with_preserved_env_uri(uri) do
|
312
|
+
@client = MongoReplicaSetClient.new
|
313
|
+
assert !@client.nil?
|
314
|
+
assert @client.connected?
|
315
|
+
assert_equal 2, @client.write_concern[:w]
|
316
|
+
assert @client.write_concern[:fsync]
|
317
|
+
assert @client.read_pool
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_connect_options_override_env_var
|
322
|
+
uri = "mongodb://#{@rs.replicas[0].host_port},#{@rs.replicas[1].host_port}?replicaset=#{@rs.repl_set_name};w=2;fsync=true;slaveok=true"
|
323
|
+
with_preserved_env_uri(uri) do
|
324
|
+
@client = MongoReplicaSetClient.new({:w => 0})
|
325
|
+
assert !@client.nil?
|
326
|
+
assert @client.connected?
|
327
|
+
assert_equal 0, @client.write_concern[:w]
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_ipv6
|
332
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
|
333
|
+
with_ipv6_enabled(@client) do
|
334
|
+
assert MongoReplicaSetClient.new(["[::1]:#{@rs.replicas[0].port}"])
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_ipv6_with_uri
|
339
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
|
340
|
+
with_ipv6_enabled(@client) do
|
341
|
+
uri = "mongodb://[::1]:#{@rs.replicas[0].port},[::1]:#{@rs.replicas[1].port}"
|
342
|
+
with_preserved_env_uri(uri) do
|
343
|
+
assert MongoReplicaSetClient.new
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
def test_ipv6_with_uri_opts
|
349
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
|
350
|
+
with_ipv6_enabled(@client) do
|
351
|
+
uri = "mongodb://[::1]:#{@rs.replicas[0].port},[::1]:#{@rs.replicas[1].port}/?safe=true;"
|
352
|
+
with_preserved_env_uri(uri) do
|
353
|
+
assert MongoReplicaSetClient.new
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_ipv6_with_different_formats
|
359
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds)
|
360
|
+
with_ipv6_enabled(@client) do
|
361
|
+
uri = "mongodb://[::1]:#{@rs.replicas[0].port},localhost:#{@rs.replicas[1].port}"
|
362
|
+
with_preserved_env_uri(uri) do
|
363
|
+
assert MongoReplicaSetClient.new
|
364
|
+
end
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_find_and_modify_with_secondary_read_preference
|
369
|
+
@client = MongoReplicaSetClient.from_uri(@uri)
|
370
|
+
collection = @client[TEST_DB].collection('test', :read => :secondary)
|
371
|
+
id = BSON::ObjectId.new
|
372
|
+
collection << { :a => id, :processed => false }
|
373
|
+
|
374
|
+
collection.find_and_modify(
|
375
|
+
:query => { 'a' => id },
|
376
|
+
:update => { "$set" => { :processed => true }}
|
377
|
+
)
|
378
|
+
assert_equal true, collection.find_one({ 'a' => id }, :read => :primary)['processed']
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_op_timeout_option
|
382
|
+
client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :connect => false,
|
383
|
+
:op_timeout => nil)
|
384
|
+
assert_equal nil, client.op_timeout
|
385
|
+
|
386
|
+
client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :connect => false,
|
387
|
+
:op_timeout => 50)
|
388
|
+
assert_equal 50, client.op_timeout
|
389
|
+
|
390
|
+
client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :connect => false)
|
391
|
+
assert_equal Mongo::MongoClient::DEFAULT_OP_TIMEOUT, client.op_timeout
|
392
|
+
end
|
393
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetConnectionTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_connect_with_deprecated_multi
|
24
|
+
silently do
|
25
|
+
@connection = Connection.multi(@rs.repl_set_seeds_old, :name => @rs.repl_set_name)
|
26
|
+
end
|
27
|
+
assert !@connection.nil?
|
28
|
+
assert @connection.connected?
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_connect_bad_name
|
32
|
+
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
|
33
|
+
@connection = ReplSetConnection.new(@rs.repl_set_seeds, :safe => true, :name => @rs.repl_set_name + "-wrong")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_connect_with_first_secondary_node_terminated
|
38
|
+
@rs.secondaries.first.stop
|
39
|
+
|
40
|
+
rescue_connection_failure do
|
41
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
42
|
+
end
|
43
|
+
assert @connection.connected?
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_connect_with_last_secondary_node_terminated
|
47
|
+
@rs.secondaries.last.stop
|
48
|
+
|
49
|
+
rescue_connection_failure do
|
50
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
51
|
+
end
|
52
|
+
assert @connection.connected?
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_connect_with_connection_string
|
56
|
+
@connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}")
|
57
|
+
assert !@connection.nil?
|
58
|
+
assert @connection.connected?
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_connect_with_connection_string_in_env_var
|
62
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
|
63
|
+
with_preserved_env_uri(uri) do
|
64
|
+
@connection = ReplSetConnection.new
|
65
|
+
assert !@connection.nil?
|
66
|
+
assert_equal 3, @connection.seeds.length
|
67
|
+
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
|
68
|
+
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
|
69
|
+
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
|
70
|
+
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
|
71
|
+
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
|
72
|
+
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
|
73
|
+
assert_equal @rs.repl_set_name, @connection.replica_set_name
|
74
|
+
assert @connection.connected?
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_connect_with_connection_string_in_implicit_mongodb_uri
|
79
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
|
80
|
+
with_preserved_env_uri(uri) do
|
81
|
+
@connection = Connection.from_uri
|
82
|
+
assert !@connection.nil?
|
83
|
+
assert_equal 3, @connection.seeds.length
|
84
|
+
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
|
85
|
+
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
|
86
|
+
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
|
87
|
+
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
|
88
|
+
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
|
89
|
+
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
|
90
|
+
assert_equal @rs.repl_set_name, @connection.replica_set_name
|
91
|
+
assert @connection.connected?
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_connect_with_new_seed_format
|
96
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
97
|
+
assert @connection.connected?
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_connect_with_old_seed_format
|
101
|
+
silently do
|
102
|
+
@connection = ReplSetConnection.new(@rs.repl_set_seeds_old)
|
103
|
+
end
|
104
|
+
assert @connection.connected?
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_connect_with_full_connection_string
|
108
|
+
@connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true")
|
109
|
+
assert !@connection.nil?
|
110
|
+
assert @connection.connected?
|
111
|
+
assert_equal 2, @connection.write_concern[:w]
|
112
|
+
assert @connection.write_concern[:fsync]
|
113
|
+
assert @connection.read_pool
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_connect_with_full_connection_string_in_env_var
|
117
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
|
118
|
+
with_preserved_env_uri(uri) do
|
119
|
+
@connection = ReplSetConnection.new
|
120
|
+
assert !@connection.nil?
|
121
|
+
assert @connection.connected?
|
122
|
+
assert_equal 2, @connection.write_concern[:w]
|
123
|
+
assert @connection.write_concern[:fsync]
|
124
|
+
assert @connection.read_pool
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_connect_options_override_env_var
|
129
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
|
130
|
+
with_preserved_env_uri(uri) do
|
131
|
+
@connection = ReplSetConnection.new({:safe => {:w => 1}})
|
132
|
+
assert !@connection.nil?
|
133
|
+
assert @connection.connected?
|
134
|
+
assert_equal 1, @connection.write_concern[:w]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetCountTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => :primary_preferred, :op_timeout => TEST_OP_TIMEOUT)
|
22
|
+
authenticate_client(@client)
|
23
|
+
assert @client.primary_pool
|
24
|
+
@primary = MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
|
25
|
+
authenticate_client(@primary)
|
26
|
+
@db = @client.db(TEST_DB)
|
27
|
+
@db.drop_collection("test-sets")
|
28
|
+
@coll = @db.collection("test-sets")
|
29
|
+
end
|
30
|
+
|
31
|
+
def teardown
|
32
|
+
@client.close if @conn
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_correct_count_after_insertion_reconnect
|
36
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
37
|
+
assert_equal 1, @coll.count
|
38
|
+
|
39
|
+
# Kill the current master node
|
40
|
+
@rs.primary.stop
|
41
|
+
|
42
|
+
rescue_connection_failure do
|
43
|
+
@coll.insert({:a => 30})
|
44
|
+
end
|
45
|
+
|
46
|
+
@coll.insert({:a => 40})
|
47
|
+
assert_equal 3, @coll.count, "Second count failed"
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_count_command_sent_to_primary
|
51
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
52
|
+
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
53
|
+
assert_equal 1, @coll.count
|
54
|
+
count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
55
|
+
assert_equal 2, count_after - count_before
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_count_with_read
|
59
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
60
|
+
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
61
|
+
assert_equal 1, @coll.count(:read => :secondary)
|
62
|
+
assert_equal 1, @coll.find({}, :read => :secondary).count()
|
63
|
+
count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
64
|
+
assert_equal 1, count_after - count_before
|
65
|
+
end
|
66
|
+
end
|