mongo 1.8.6 → 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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/LICENSE +1 -1
- data/README.md +114 -282
- data/Rakefile +18 -4
- data/VERSION +1 -1
- data/bin/mongo_console +27 -5
- data/lib/mongo/bulk_write_collection_view.rb +387 -0
- data/lib/mongo/collection.rb +283 -222
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/{util → connection}/node.rb +58 -6
- data/lib/mongo/{util → connection}/pool.rb +61 -37
- data/lib/mongo/{util → connection}/pool_manager.rb +72 -22
- data/lib/mongo/{util → connection}/sharding_pool_manager.rb +13 -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 +19 -0
- data/lib/mongo/cursor.rb +183 -57
- data/lib/mongo/db.rb +302 -138
- data/lib/mongo/exception.rb +145 -0
- data/lib/mongo/functional/authentication.rb +455 -0
- data/lib/mongo/{util → functional}/logging.rb +23 -7
- 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/{util → functional}/write_concern.rb +21 -9
- data/lib/mongo/functional.rb +20 -0
- data/lib/mongo/gridfs/grid.rb +19 -8
- data/lib/mongo/gridfs/grid_ext.rb +14 -0
- data/lib/mongo/gridfs/grid_file_system.rb +17 -4
- data/lib/mongo/gridfs/grid_io.rb +21 -9
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/legacy.rb +76 -7
- data/lib/mongo/mongo_client.rb +246 -206
- data/lib/mongo/mongo_replica_set_client.rb +65 -15
- data/lib/mongo/mongo_sharded_client.rb +18 -3
- data/lib/mongo/networking.rb +47 -18
- data/lib/mongo/{util → utils}/conversions.rb +18 -3
- data/lib/mongo/{util → utils}/core_ext.rb +15 -32
- data/lib/mongo/{util → utils}/server_version.rb +15 -0
- data/lib/mongo/{util → utils}/support.rb +22 -55
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo.rb +44 -26
- data/mongo.gemspec +2 -2
- data/test/functional/authentication_test.rb +31 -10
- 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 +1419 -654
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +46 -2
- data/test/functional/cursor_fail_test.rb +17 -9
- data/test/functional/cursor_message_test.rb +28 -15
- data/test/functional/cursor_test.rb +300 -165
- data/test/functional/db_api_test.rb +294 -264
- data/test/functional/db_connection_test.rb +15 -3
- data/test/functional/db_test.rb +165 -99
- data/test/functional/grid_file_system_test.rb +124 -112
- data/test/functional/grid_io_test.rb +17 -3
- data/test/functional/grid_test.rb +16 -2
- data/test/functional/pool_test.rb +99 -10
- data/test/functional/safe_test.rb +18 -4
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +14 -0
- data/test/functional/timeout_test.rb +27 -27
- data/test/functional/uri_test.rb +268 -22
- data/test/functional/write_concern_test.rb +19 -5
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +476 -0
- data/test/replica_set/authentication_test.rb +28 -11
- data/test/replica_set/basic_test.rb +79 -23
- data/test/replica_set/client_test.rb +253 -124
- data/test/replica_set/connection_test.rb +59 -37
- data/test/replica_set/count_test.rb +18 -2
- data/test/replica_set/cursor_test.rb +30 -8
- data/test/replica_set/insert_test.rb +109 -2
- data/test/replica_set/max_values_test.rb +85 -10
- data/test/replica_set/pinning_test.rb +66 -2
- data/test/replica_set/query_test.rb +17 -3
- data/test/replica_set/read_preference_test.rb +115 -96
- data/test/replica_set/refresh_test.rb +59 -9
- data/test/replica_set/replication_ack_test.rb +32 -11
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +73 -25
- 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 +47 -196
- data/test/threading/basic_test.rb +42 -2
- data/test/tools/mongo_config.rb +175 -35
- data/test/tools/mongo_config_test.rb +15 -1
- data/test/unit/client_test.rb +186 -57
- data/test/unit/collection_test.rb +44 -54
- data/test/unit/connection_test.rb +160 -71
- data/test/unit/cursor_test.rb +37 -3
- data/test/unit/db_test.rb +38 -14
- data/test/unit/grid_test.rb +15 -1
- data/test/unit/mongo_sharded_client_test.rb +30 -14
- data/test/unit/node_test.rb +16 -1
- data/test/unit/pool_manager_test.rb +21 -4
- data/test/unit/read_pref_test.rb +386 -1
- data/test/unit/read_test.rb +27 -13
- data/test/unit/safe_test.rb +22 -8
- data/test/unit/sharding_pool_manager_test.rb +25 -4
- data/test/unit/write_concern_test.rb +23 -9
- data.tar.gz.sig +0 -0
- metadata +80 -54
- metadata.gz.sig +0 -0
- data/lib/mongo/exceptions.rb +0 -65
- data/lib/mongo/util/read_preference.rb +0 -112
- data/lib/mongo/util/socket_util.rb +0 -20
- data/lib/mongo/util/ssl_socket.rb +0 -51
- data/lib/mongo/util/tcp_socket.rb +0 -62
- data/lib/mongo/util/thread_local_variable_manager.rb +0 -11
- data/lib/mongo/util/unix_socket.rb +0 -23
- data/lib/mongo/util/uri_parser.rb +0 -337
- data/test/functional/connection_test.rb +0 -449
- data/test/functional/threading_test.rb +0 -95
- data/test/replica_set/complex_connect_test.rb +0 -64
- data/test/shared/authentication.rb +0 -66
- data/test/unit/pool_test.rb +0 -9
- data/test/unit/util_test.rb +0 -55
|
@@ -1,449 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
require 'logger'
|
|
3
|
-
require 'stringio'
|
|
4
|
-
require 'thread'
|
|
5
|
-
|
|
6
|
-
class TestConnection < Test::Unit::TestCase
|
|
7
|
-
|
|
8
|
-
include Mongo
|
|
9
|
-
include BSON
|
|
10
|
-
|
|
11
|
-
def setup
|
|
12
|
-
@client = standard_connection
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def teardown
|
|
16
|
-
@client.close
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def test_connection_failure
|
|
20
|
-
assert_raise Mongo::ConnectionFailure do
|
|
21
|
-
MongoClient.new('localhost', 27347)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def test_host_port_accessors
|
|
26
|
-
assert_equal @client.host, TEST_HOST
|
|
27
|
-
assert_equal @client.port, TEST_PORT
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def test_server_info
|
|
31
|
-
server_info = @client.server_info
|
|
32
|
-
assert server_info.keys.include?("version")
|
|
33
|
-
assert Mongo::Support.ok?(server_info)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def test_ping
|
|
37
|
-
ping = @client.ping
|
|
38
|
-
assert ping['ok']
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def test_connection_uri
|
|
42
|
-
con = MongoClient.from_uri("mongodb://#{host_port}")
|
|
43
|
-
assert_equal mongo_host, con.primary_pool.host
|
|
44
|
-
assert_equal mongo_port, con.primary_pool.port
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def test_uri_with_extra_opts
|
|
48
|
-
con = MongoClient.from_uri("mongodb://#{host_port}", :pool_size => 10, :slave_ok => true)
|
|
49
|
-
assert_equal 10, con.pool_size
|
|
50
|
-
assert con.slave_ok?
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def test_env_mongodb_uri
|
|
54
|
-
begin
|
|
55
|
-
old_mongodb_uri = ENV['MONGODB_URI']
|
|
56
|
-
ENV['MONGODB_URI'] = "mongodb://#{host_port}"
|
|
57
|
-
con = MongoClient.new
|
|
58
|
-
assert_equal mongo_host, con.primary_pool.host
|
|
59
|
-
assert_equal mongo_port, con.primary_pool.port
|
|
60
|
-
ensure
|
|
61
|
-
ENV['MONGODB_URI'] = old_mongodb_uri
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def test_from_uri_implicit_mongodb_uri
|
|
66
|
-
begin
|
|
67
|
-
old_mongodb_uri = ENV['MONGODB_URI']
|
|
68
|
-
ENV['MONGODB_URI'] = "mongodb://#{host_port}"
|
|
69
|
-
con = MongoClient.from_uri
|
|
70
|
-
assert_equal mongo_host, con.primary_pool.host
|
|
71
|
-
assert_equal mongo_port, con.primary_pool.port
|
|
72
|
-
ensure
|
|
73
|
-
ENV['MONGODB_URI'] = old_mongodb_uri
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def test_db_from_uri_exists_no_options
|
|
78
|
-
begin
|
|
79
|
-
db_name = "_database"
|
|
80
|
-
|
|
81
|
-
old_mongodb_uri = ENV['MONGODB_URI']
|
|
82
|
-
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}"
|
|
83
|
-
con = MongoClient.from_uri
|
|
84
|
-
db = con.db
|
|
85
|
-
assert_equal db.name, db_name
|
|
86
|
-
ensure
|
|
87
|
-
ENV['MONGODB_URI'] = old_mongodb_uri
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def test_db_from_uri_exists_options
|
|
92
|
-
begin
|
|
93
|
-
db_name = "_database"
|
|
94
|
-
|
|
95
|
-
old_mongodb_uri = ENV['MONGODB_URI']
|
|
96
|
-
ENV['MONGODB_URI'] = "mongodb://#{host_port}/#{db_name}?"
|
|
97
|
-
con = MongoClient.from_uri
|
|
98
|
-
db = con.db
|
|
99
|
-
assert_equal db.name, db_name
|
|
100
|
-
ensure
|
|
101
|
-
ENV['MONGODB_URI'] = old_mongodb_uri
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def test_db_from_uri_exists_no_db_name
|
|
106
|
-
begin
|
|
107
|
-
old_mongodb_uri = ENV['MONGODB_URI']
|
|
108
|
-
ENV['MONGODB_URI'] = "mongodb://#{host_port}/"
|
|
109
|
-
con = MongoClient.from_uri
|
|
110
|
-
db = con.db
|
|
111
|
-
assert_equal db.name, Mongo::MongoClient::DEFAULT_DB_NAME
|
|
112
|
-
ensure
|
|
113
|
-
ENV['MONGODB_URI'] = old_mongodb_uri
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def test_server_version
|
|
118
|
-
assert_match(/\d\.\d+(\.\d+)?/, @client.server_version.to_s)
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def test_invalid_database_names
|
|
122
|
-
assert_raise TypeError do @client.db(4) end
|
|
123
|
-
|
|
124
|
-
assert_raise Mongo::InvalidNSName do @client.db('') end
|
|
125
|
-
assert_raise Mongo::InvalidNSName do @client.db('te$t') end
|
|
126
|
-
assert_raise Mongo::InvalidNSName do @client.db('te.t') end
|
|
127
|
-
assert_raise Mongo::InvalidNSName do @client.db('te\\t') end
|
|
128
|
-
assert_raise Mongo::InvalidNSName do @client.db('te/t') end
|
|
129
|
-
assert_raise Mongo::InvalidNSName do @client.db('te st') end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def test_options_passed_to_db
|
|
133
|
-
@pk_mock = Object.new
|
|
134
|
-
db = @client.db('test', :pk => @pk_mock, :strict => true)
|
|
135
|
-
assert_equal @pk_mock, db.pk_factory
|
|
136
|
-
assert db.strict?
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def test_database_info
|
|
140
|
-
@client.drop_database(MONGO_TEST_DB)
|
|
141
|
-
@client.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
|
|
142
|
-
|
|
143
|
-
info = @client.database_info
|
|
144
|
-
assert_not_nil info
|
|
145
|
-
assert_kind_of Hash, info
|
|
146
|
-
assert_not_nil info[MONGO_TEST_DB]
|
|
147
|
-
assert info[MONGO_TEST_DB] > 0
|
|
148
|
-
|
|
149
|
-
@client.drop_database(MONGO_TEST_DB)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def test_copy_database
|
|
153
|
-
@client.db('old').collection('copy-test').insert('a' => 1)
|
|
154
|
-
@client.copy_database('old', 'new', host_port)
|
|
155
|
-
old_object = @client.db('old').collection('copy-test').find.next_document
|
|
156
|
-
new_object = @client.db('new').collection('copy-test').find.next_document
|
|
157
|
-
assert_equal old_object, new_object
|
|
158
|
-
@client.drop_database('old')
|
|
159
|
-
@client.drop_database('new')
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
def test_copy_database_with_auth
|
|
163
|
-
@client.db('old').collection('copy-test').insert('a' => 1)
|
|
164
|
-
@client.db('old').add_user('bob', 'secret')
|
|
165
|
-
|
|
166
|
-
assert_raise Mongo::OperationFailure do
|
|
167
|
-
@client.copy_database('old', 'new', host_port, 'bob', 'badpassword')
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
result = @client.copy_database('old', 'new', host_port, 'bob', 'secret')
|
|
171
|
-
assert Mongo::Support.ok?(result)
|
|
172
|
-
|
|
173
|
-
@client.drop_database('old')
|
|
174
|
-
@client.drop_database('new')
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
def test_database_names
|
|
178
|
-
@client.drop_database(MONGO_TEST_DB)
|
|
179
|
-
@client.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
|
|
180
|
-
|
|
181
|
-
names = @client.database_names
|
|
182
|
-
assert_not_nil names
|
|
183
|
-
assert_kind_of Array, names
|
|
184
|
-
assert names.length >= 1
|
|
185
|
-
assert names.include?(MONGO_TEST_DB)
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
def test_logging
|
|
189
|
-
output = StringIO.new
|
|
190
|
-
logger = Logger.new(output)
|
|
191
|
-
logger.level = Logger::DEBUG
|
|
192
|
-
standard_connection(:logger => logger).db(MONGO_TEST_DB)
|
|
193
|
-
assert output.string.include?("admin['$cmd'].find")
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
def test_logging_duration
|
|
197
|
-
output = StringIO.new
|
|
198
|
-
logger = Logger.new(output)
|
|
199
|
-
logger.level = Logger::DEBUG
|
|
200
|
-
standard_connection(:logger => logger).db(MONGO_TEST_DB)
|
|
201
|
-
assert_match(/\(\d+.\d{1}ms\)/, output.string)
|
|
202
|
-
assert output.string.include?("admin['$cmd'].find")
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
def test_connection_logger
|
|
206
|
-
output = StringIO.new
|
|
207
|
-
logger = Logger.new(output)
|
|
208
|
-
logger.level = Logger::DEBUG
|
|
209
|
-
connection = standard_connection(:logger => logger)
|
|
210
|
-
assert_equal logger, connection.logger
|
|
211
|
-
|
|
212
|
-
connection.logger.debug 'testing'
|
|
213
|
-
assert output.string.include?('testing')
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
def test_drop_database
|
|
217
|
-
db = @client.db('ruby-mongo-will-be-deleted')
|
|
218
|
-
coll = db.collection('temp')
|
|
219
|
-
coll.remove
|
|
220
|
-
coll.insert(:name => 'temp')
|
|
221
|
-
assert_equal 1, coll.count()
|
|
222
|
-
assert @client.database_names.include?('ruby-mongo-will-be-deleted')
|
|
223
|
-
|
|
224
|
-
@client.drop_database('ruby-mongo-will-be-deleted')
|
|
225
|
-
assert !@client.database_names.include?('ruby-mongo-will-be-deleted')
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
def test_nodes
|
|
229
|
-
silently do
|
|
230
|
-
@client = MongoClient.multi([['foo', 27017], ['bar', 27018]], :connect => false)
|
|
231
|
-
end
|
|
232
|
-
seeds = @client.seeds
|
|
233
|
-
assert_equal 2, seeds.length
|
|
234
|
-
assert_equal ['foo', 27017], seeds[0]
|
|
235
|
-
assert_equal ['bar', 27018], seeds[1]
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
def test_fsync_lock
|
|
239
|
-
assert !@client.locked?
|
|
240
|
-
@client.lock!
|
|
241
|
-
assert @client.locked?
|
|
242
|
-
assert [1, true].include?(@client['admin']['$cmd.sys.inprog'].find_one['fsyncLock'])
|
|
243
|
-
assert_match(/unlock/, @client.unlock!['info'])
|
|
244
|
-
unlocked = false
|
|
245
|
-
counter = 0
|
|
246
|
-
while counter < 5
|
|
247
|
-
if @client['admin']['$cmd.sys.inprog'].find_one['fsyncLock'].nil?
|
|
248
|
-
unlocked = true
|
|
249
|
-
break
|
|
250
|
-
else
|
|
251
|
-
sleep(1)
|
|
252
|
-
counter += 1
|
|
253
|
-
end
|
|
254
|
-
end
|
|
255
|
-
assert !@client.locked?
|
|
256
|
-
assert unlocked, "mongod failed to unlock"
|
|
257
|
-
end
|
|
258
|
-
|
|
259
|
-
def test_max_bson_size_value
|
|
260
|
-
conn = standard_connection(:connect => false)
|
|
261
|
-
|
|
262
|
-
admin_db = Object.new
|
|
263
|
-
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1, 'maxBsonObjectSize' => 15_000_000})
|
|
264
|
-
conn.expects(:[]).with('admin').returns(admin_db)
|
|
265
|
-
conn.connect
|
|
266
|
-
assert_equal 15_000_000, conn.max_bson_size
|
|
267
|
-
|
|
268
|
-
conn = standard_connection
|
|
269
|
-
if conn.server_version > "1.7.2"
|
|
270
|
-
assert_equal conn['admin'].command({:ismaster => 1})['maxBsonObjectSize'], conn.max_bson_size
|
|
271
|
-
end
|
|
272
|
-
end
|
|
273
|
-
|
|
274
|
-
def test_max_message_size_value
|
|
275
|
-
conn = standard_connection(:connect => false)
|
|
276
|
-
|
|
277
|
-
admin_db = Object.new
|
|
278
|
-
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1, 'maxMessageSizeBytes' => 20_000_000})
|
|
279
|
-
conn.expects(:[]).with('admin').returns(admin_db)
|
|
280
|
-
conn.connect
|
|
281
|
-
|
|
282
|
-
assert_equal 20_000_000, conn.max_message_size
|
|
283
|
-
|
|
284
|
-
conn = standard_connection
|
|
285
|
-
maxMessageSizeBytes = conn['admin'].command({:ismaster => 1})['maxMessageSizeBytes']
|
|
286
|
-
if conn.server_version.to_s[/([^-]+)/,1] >= "2.4.0"
|
|
287
|
-
assert_equal 48_000_000, maxMessageSizeBytes
|
|
288
|
-
elsif conn.server_version > "2.3.2"
|
|
289
|
-
assert_equal conn.max_bson_size, maxMessageSizeBytes
|
|
290
|
-
end
|
|
291
|
-
end
|
|
292
|
-
|
|
293
|
-
def test_max_bson_size_with_no_reported_max_size
|
|
294
|
-
conn = standard_connection(:connect => false)
|
|
295
|
-
|
|
296
|
-
admin_db = Object.new
|
|
297
|
-
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
298
|
-
conn.expects(:[]).with('admin').returns(admin_db)
|
|
299
|
-
|
|
300
|
-
conn.connect
|
|
301
|
-
assert_equal Mongo::DEFAULT_MAX_BSON_SIZE, conn.max_bson_size
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
def test_max_message_size_with_no_reported_max_size
|
|
305
|
-
conn = standard_connection(:connect => false)
|
|
306
|
-
|
|
307
|
-
admin_db = Object.new
|
|
308
|
-
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
309
|
-
conn.expects(:[]).with('admin').returns(admin_db)
|
|
310
|
-
|
|
311
|
-
conn.connect
|
|
312
|
-
assert_equal Mongo::DEFAULT_MAX_BSON_SIZE * Mongo::MESSAGE_SIZE_FACTOR, conn.max_message_size
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
def test_connection_activity
|
|
316
|
-
conn = standard_connection
|
|
317
|
-
assert conn.active?
|
|
318
|
-
|
|
319
|
-
conn.primary_pool.close
|
|
320
|
-
assert !conn.active?
|
|
321
|
-
|
|
322
|
-
# Simulate a dropped connection.
|
|
323
|
-
dropped_socket = mock('dropped_socket')
|
|
324
|
-
dropped_socket.stubs(:read).raises(Errno::ECONNRESET)
|
|
325
|
-
dropped_socket.stubs(:send).raises(Errno::ECONNRESET)
|
|
326
|
-
dropped_socket.stub_everything
|
|
327
|
-
|
|
328
|
-
conn.primary_pool.host = 'localhost'
|
|
329
|
-
conn.primary_pool.port = Mongo::MongoClient::DEFAULT_PORT
|
|
330
|
-
conn.primary_pool.instance_variable_set("@pids", {dropped_socket => Process.pid})
|
|
331
|
-
conn.primary_pool.instance_variable_set("@sockets", [dropped_socket])
|
|
332
|
-
|
|
333
|
-
assert !conn.active?
|
|
334
|
-
end
|
|
335
|
-
|
|
336
|
-
context "Saved authentications" do
|
|
337
|
-
setup do
|
|
338
|
-
@client = standard_connection
|
|
339
|
-
@auth = {:db_name => 'test', :username => 'bob', :password => 'secret'}
|
|
340
|
-
@client.add_auth(@auth[:db_name], @auth[:username], @auth[:password])
|
|
341
|
-
end
|
|
342
|
-
|
|
343
|
-
teardown do
|
|
344
|
-
@client.clear_auths
|
|
345
|
-
end
|
|
346
|
-
|
|
347
|
-
should "save the authentication" do
|
|
348
|
-
assert_equal @auth, @client.auths[0]
|
|
349
|
-
end
|
|
350
|
-
|
|
351
|
-
should "not allow multiple authentications for the same db" do
|
|
352
|
-
auth = {:db_name => 'test', :username => 'mickey', :password => 'm0u53'}
|
|
353
|
-
assert_raise Mongo::MongoArgumentError do
|
|
354
|
-
@client.add_auth(auth[:db_name], auth[:username], auth[:password])
|
|
355
|
-
end
|
|
356
|
-
end
|
|
357
|
-
|
|
358
|
-
should "remove auths by database" do
|
|
359
|
-
@client.remove_auth('non-existent database')
|
|
360
|
-
assert_equal 1, @client.auths.length
|
|
361
|
-
|
|
362
|
-
@client.remove_auth('test')
|
|
363
|
-
assert_equal 0, @client.auths.length
|
|
364
|
-
end
|
|
365
|
-
|
|
366
|
-
should "remove all auths" do
|
|
367
|
-
@client.clear_auths
|
|
368
|
-
assert_equal 0, @client.auths.length
|
|
369
|
-
end
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
context "Socket pools" do
|
|
373
|
-
context "checking out writers" do
|
|
374
|
-
setup do
|
|
375
|
-
@con = standard_connection(:pool_size => 10, :pool_timeout => 10)
|
|
376
|
-
@coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
should "close the connection on send_message for major exceptions" do
|
|
380
|
-
@con.expects(:checkout_writer).raises(SystemStackError)
|
|
381
|
-
@con.expects(:close)
|
|
382
|
-
begin
|
|
383
|
-
@coll.insert({:foo => "bar"})
|
|
384
|
-
rescue SystemStackError
|
|
385
|
-
end
|
|
386
|
-
end
|
|
387
|
-
|
|
388
|
-
should "close the connection on send_message_with_gle for major exceptions" do
|
|
389
|
-
@con.expects(:checkout_writer).raises(SystemStackError)
|
|
390
|
-
@con.expects(:close)
|
|
391
|
-
begin
|
|
392
|
-
@coll.insert({:foo => "bar"}, :w => 1)
|
|
393
|
-
rescue SystemStackError
|
|
394
|
-
end
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
should "close the connection on receive_message for major exceptions" do
|
|
398
|
-
@con.expects(:checkout_reader).raises(SystemStackError)
|
|
399
|
-
@con.expects(:close)
|
|
400
|
-
begin
|
|
401
|
-
@coll.find.next
|
|
402
|
-
rescue SystemStackError
|
|
403
|
-
end
|
|
404
|
-
end
|
|
405
|
-
end
|
|
406
|
-
end
|
|
407
|
-
|
|
408
|
-
context "Connection exceptions" do
|
|
409
|
-
setup do
|
|
410
|
-
@con = standard_connection(:pool_size => 10, :pool_timeout => 10)
|
|
411
|
-
@coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
|
|
412
|
-
end
|
|
413
|
-
|
|
414
|
-
should "release connection if an exception is raised on send_message" do
|
|
415
|
-
@con.stubs(:send_message_on_socket).raises(ConnectionFailure)
|
|
416
|
-
assert_equal 0, @con.primary_pool.checked_out.size
|
|
417
|
-
assert_raise ConnectionFailure do
|
|
418
|
-
@coll.insert({:test => "insert"})
|
|
419
|
-
end
|
|
420
|
-
assert_equal 0, @con.primary_pool.checked_out.size
|
|
421
|
-
end
|
|
422
|
-
|
|
423
|
-
should "release connection if an exception is raised on write concern :w => 1" do
|
|
424
|
-
@con.stubs(:receive).raises(ConnectionFailure)
|
|
425
|
-
assert_equal 0, @con.primary_pool.checked_out.size
|
|
426
|
-
assert_raise ConnectionFailure do
|
|
427
|
-
@coll.insert({:test => "insert"}, :w => 1)
|
|
428
|
-
end
|
|
429
|
-
assert_equal 0, @con.primary_pool.checked_out.size
|
|
430
|
-
end
|
|
431
|
-
|
|
432
|
-
should "release connection if an exception is raised on receive_message" do
|
|
433
|
-
@con.stubs(:receive).raises(ConnectionFailure)
|
|
434
|
-
assert_equal 0, @con.read_pool.checked_out.size
|
|
435
|
-
assert_raise ConnectionFailure do
|
|
436
|
-
@coll.find.to_a
|
|
437
|
-
end
|
|
438
|
-
assert_equal 0, @con.read_pool.checked_out.size
|
|
439
|
-
end
|
|
440
|
-
|
|
441
|
-
should "show a proper exception message if an IOError is raised while closing a socket" do
|
|
442
|
-
TCPSocket.any_instance.stubs(:close).raises(IOError.new)
|
|
443
|
-
|
|
444
|
-
@con.primary_pool.checkout_new_socket
|
|
445
|
-
@con.primary_pool.expects(:warn)
|
|
446
|
-
assert @con.primary_pool.close
|
|
447
|
-
end
|
|
448
|
-
end
|
|
449
|
-
end
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class TestThreading < Test::Unit::TestCase
|
|
4
|
-
|
|
5
|
-
include Mongo
|
|
6
|
-
|
|
7
|
-
@@client = standard_connection(:pool_size => 10, :pool_timeout => 30)
|
|
8
|
-
@@db = @@client[MONGO_TEST_DB]
|
|
9
|
-
@@coll = @@db.collection('thread-test-collection')
|
|
10
|
-
|
|
11
|
-
def set_up_safe_data
|
|
12
|
-
@@db.drop_collection('duplicate')
|
|
13
|
-
@@db.drop_collection('unique')
|
|
14
|
-
@duplicate = @@db.collection('duplicate')
|
|
15
|
-
@unique = @@db.collection('unique')
|
|
16
|
-
|
|
17
|
-
@duplicate.insert("test" => "insert")
|
|
18
|
-
@duplicate.insert("test" => "update")
|
|
19
|
-
@unique.insert("test" => "insert")
|
|
20
|
-
@unique.insert("test" => "update")
|
|
21
|
-
@unique.create_index("test", :unique => true)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def test_safe_update
|
|
25
|
-
times = []
|
|
26
|
-
set_up_safe_data
|
|
27
|
-
threads = []
|
|
28
|
-
100.times do |i|
|
|
29
|
-
threads[i] = Thread.new do
|
|
30
|
-
100.times do
|
|
31
|
-
if i % 2 == 0
|
|
32
|
-
assert_raise Mongo::OperationFailure do
|
|
33
|
-
t1 = Time.now
|
|
34
|
-
@unique.update({"test" => "insert"}, {"$set" => {"test" => "update"}})
|
|
35
|
-
times << Time.now - t1
|
|
36
|
-
end
|
|
37
|
-
else
|
|
38
|
-
t1 = Time.now
|
|
39
|
-
@duplicate.update({"test" => "insert"}, {"$set" => {"test" => "update"}})
|
|
40
|
-
times << Time.now - t1
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
100.times do |i|
|
|
47
|
-
threads[i].join
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def test_safe_insert
|
|
52
|
-
set_up_safe_data
|
|
53
|
-
threads = []
|
|
54
|
-
100.times do |i|
|
|
55
|
-
threads[i] = Thread.new do
|
|
56
|
-
if i % 2 == 0
|
|
57
|
-
assert_raise Mongo::OperationFailure do
|
|
58
|
-
@unique.insert({"test" => "insert"})
|
|
59
|
-
end
|
|
60
|
-
else
|
|
61
|
-
@duplicate.insert({"test" => "insert"})
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
100.times do |i|
|
|
67
|
-
threads[i].join
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def test_threading
|
|
72
|
-
@@coll.drop
|
|
73
|
-
@@coll = @@db.collection('thread-test-collection')
|
|
74
|
-
|
|
75
|
-
1000.times do |i|
|
|
76
|
-
@@coll.insert({"x" => i})
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
threads = []
|
|
80
|
-
|
|
81
|
-
10.times do |i|
|
|
82
|
-
threads[i] = Thread.new do
|
|
83
|
-
sum = 0
|
|
84
|
-
@@coll.find().each do |document|
|
|
85
|
-
sum += document["x"]
|
|
86
|
-
end
|
|
87
|
-
assert_equal 499500, sum
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
10.times do |i|
|
|
92
|
-
threads[i].join
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
class ComplexConnectTest < Test::Unit::TestCase
|
|
4
|
-
|
|
5
|
-
def setup
|
|
6
|
-
ensure_cluster(:rs)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def teardown
|
|
10
|
-
@client.close if defined?(@conn) && @conn
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def test_complex_connect
|
|
14
|
-
host = @rs.servers.first.host
|
|
15
|
-
primary = MongoClient.new(host, @rs.primary.port)
|
|
16
|
-
|
|
17
|
-
@client = MongoReplicaSetClient.new([
|
|
18
|
-
@rs.servers[2].host_port,
|
|
19
|
-
@rs.servers[1].host_port,
|
|
20
|
-
@rs.servers[0].host_port
|
|
21
|
-
])
|
|
22
|
-
|
|
23
|
-
version = @client.server_version
|
|
24
|
-
|
|
25
|
-
@client['test']['foo'].insert({:a => 1})
|
|
26
|
-
assert @client['test']['foo'].find_one
|
|
27
|
-
|
|
28
|
-
config = primary['local']['system.replset'].find_one
|
|
29
|
-
old_config = config.dup
|
|
30
|
-
config['version'] += 1
|
|
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
|
-
|
|
35
|
-
config['members'].delete_if do |member|
|
|
36
|
-
member['host'].include?(port_to_delete)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
assert_raise ConnectionFailure do
|
|
40
|
-
primary['admin'].command({:replSetReconfig => config})
|
|
41
|
-
end
|
|
42
|
-
@rs.start
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
assert_raise ConnectionFailure do
|
|
46
|
-
primary['admin'].command(step_down_command)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# isMaster is currently broken in 2.1+ when called on removed nodes
|
|
50
|
-
puts version
|
|
51
|
-
if version < "2.1"
|
|
52
|
-
rescue_connection_failure do
|
|
53
|
-
assert @client['test']['foo'].find_one
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
assert @client['test']['foo'].find_one
|
|
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
|
|
63
|
-
end
|
|
64
|
-
end
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
module AuthenticationTests
|
|
2
|
-
|
|
3
|
-
def init_auth
|
|
4
|
-
# enable authentication by creating and logging in as admin user
|
|
5
|
-
@admin = @client['admin']
|
|
6
|
-
@admin.add_user('admin', 'password')
|
|
7
|
-
@admin.authenticate('admin', 'password')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def teardown
|
|
11
|
-
@admin.logout
|
|
12
|
-
@admin.authenticate('admin','password')
|
|
13
|
-
@admin['system.users'].remove
|
|
14
|
-
@db['system.users'].remove
|
|
15
|
-
@db['test'].remove
|
|
16
|
-
@admin.logout
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def test_add_user
|
|
20
|
-
@db.add_user('bob','user')
|
|
21
|
-
assert @db['system.users'].find_one({:user => 'bob'})
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def test_remove_user
|
|
25
|
-
@db.remove_user('bob')
|
|
26
|
-
assert_nil @db['system.users'].find_one({:user => 'bob'})
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def test_remove_non_existent_user
|
|
30
|
-
assert_equal @db.remove_user('joe'), false
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def test_authenticate
|
|
34
|
-
@db.add_user('peggy', 'user')
|
|
35
|
-
assert @db.authenticate('peggy', 'user')
|
|
36
|
-
@db.remove_user('peggy')
|
|
37
|
-
@db.logout
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def test_authenticate_non_existent_user
|
|
41
|
-
assert_raise Mongo::AuthenticationError do
|
|
42
|
-
@db.authenticate('frank', 'thetank')
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def test_logout
|
|
47
|
-
@db.add_user('peggy', 'user')
|
|
48
|
-
assert @db.authenticate('peggy', 'user')
|
|
49
|
-
assert @db.logout
|
|
50
|
-
@db.remove_user('peggy')
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def test_authenticate_with_special_characters
|
|
54
|
-
assert @db.add_user('foo:bar','@foo')
|
|
55
|
-
assert @db.authenticate('foo:bar','@foo')
|
|
56
|
-
@db.remove_user('foo:bar')
|
|
57
|
-
@db.logout
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def test_authenticate_read_only
|
|
61
|
-
@db.add_user('randy', 'readonly', true)
|
|
62
|
-
assert @db.authenticate('randy', 'readonly')
|
|
63
|
-
@db.remove_user('randy')
|
|
64
|
-
@db.logout
|
|
65
|
-
end
|
|
66
|
-
end
|