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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/LICENSE +1 -1
  4. data/README.md +114 -282
  5. data/Rakefile +18 -4
  6. data/VERSION +1 -1
  7. data/bin/mongo_console +27 -5
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +283 -222
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/{util → connection}/node.rb +58 -6
  12. data/lib/mongo/{util → connection}/pool.rb +61 -37
  13. data/lib/mongo/{util → connection}/pool_manager.rb +72 -22
  14. data/lib/mongo/{util → connection}/sharding_pool_manager.rb +13 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +19 -0
  21. data/lib/mongo/cursor.rb +183 -57
  22. data/lib/mongo/db.rb +302 -138
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/{util → functional}/logging.rb +23 -7
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/{util → functional}/write_concern.rb +21 -9
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +19 -8
  32. data/lib/mongo/gridfs/grid_ext.rb +14 -0
  33. data/lib/mongo/gridfs/grid_file_system.rb +17 -4
  34. data/lib/mongo/gridfs/grid_io.rb +21 -9
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +76 -7
  37. data/lib/mongo/mongo_client.rb +246 -206
  38. data/lib/mongo/mongo_replica_set_client.rb +65 -15
  39. data/lib/mongo/mongo_sharded_client.rb +18 -3
  40. data/lib/mongo/networking.rb +47 -18
  41. data/lib/mongo/{util → utils}/conversions.rb +18 -3
  42. data/lib/mongo/{util → utils}/core_ext.rb +15 -32
  43. data/lib/mongo/{util → utils}/server_version.rb +15 -0
  44. data/lib/mongo/{util → utils}/support.rb +22 -55
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +44 -26
  48. data/mongo.gemspec +2 -2
  49. data/test/functional/authentication_test.rb +31 -10
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +1419 -654
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/functional/conversions_test.rb +46 -2
  56. data/test/functional/cursor_fail_test.rb +17 -9
  57. data/test/functional/cursor_message_test.rb +28 -15
  58. data/test/functional/cursor_test.rb +300 -165
  59. data/test/functional/db_api_test.rb +294 -264
  60. data/test/functional/db_connection_test.rb +15 -3
  61. data/test/functional/db_test.rb +165 -99
  62. data/test/functional/grid_file_system_test.rb +124 -112
  63. data/test/functional/grid_io_test.rb +17 -3
  64. data/test/functional/grid_test.rb +16 -2
  65. data/test/functional/pool_test.rb +99 -10
  66. data/test/functional/safe_test.rb +18 -4
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +14 -0
  69. data/test/functional/timeout_test.rb +27 -27
  70. data/test/functional/uri_test.rb +268 -22
  71. data/test/functional/write_concern_test.rb +19 -5
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +28 -11
  75. data/test/replica_set/basic_test.rb +79 -23
  76. data/test/replica_set/client_test.rb +253 -124
  77. data/test/replica_set/connection_test.rb +59 -37
  78. data/test/replica_set/count_test.rb +18 -2
  79. data/test/replica_set/cursor_test.rb +30 -8
  80. data/test/replica_set/insert_test.rb +109 -2
  81. data/test/replica_set/max_values_test.rb +85 -10
  82. data/test/replica_set/pinning_test.rb +66 -2
  83. data/test/replica_set/query_test.rb +17 -3
  84. data/test/replica_set/read_preference_test.rb +115 -96
  85. data/test/replica_set/refresh_test.rb +59 -9
  86. data/test/replica_set/replication_ack_test.rb +32 -11
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +73 -25
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +47 -196
  96. data/test/threading/basic_test.rb +42 -2
  97. data/test/tools/mongo_config.rb +175 -35
  98. data/test/tools/mongo_config_test.rb +15 -1
  99. data/test/unit/client_test.rb +186 -57
  100. data/test/unit/collection_test.rb +44 -54
  101. data/test/unit/connection_test.rb +160 -71
  102. data/test/unit/cursor_test.rb +37 -3
  103. data/test/unit/db_test.rb +38 -14
  104. data/test/unit/grid_test.rb +15 -1
  105. data/test/unit/mongo_sharded_client_test.rb +30 -14
  106. data/test/unit/node_test.rb +16 -1
  107. data/test/unit/pool_manager_test.rb +21 -4
  108. data/test/unit/read_pref_test.rb +386 -1
  109. data/test/unit/read_test.rb +27 -13
  110. data/test/unit/safe_test.rb +22 -8
  111. data/test/unit/sharding_pool_manager_test.rb +25 -4
  112. data/test/unit/write_concern_test.rb +23 -9
  113. data.tar.gz.sig +0 -0
  114. metadata +80 -54
  115. metadata.gz.sig +0 -0
  116. data/lib/mongo/exceptions.rb +0 -65
  117. data/lib/mongo/util/read_preference.rb +0 -112
  118. data/lib/mongo/util/socket_util.rb +0 -20
  119. data/lib/mongo/util/ssl_socket.rb +0 -51
  120. data/lib/mongo/util/tcp_socket.rb +0 -62
  121. data/lib/mongo/util/thread_local_variable_manager.rb +0 -11
  122. data/lib/mongo/util/unix_socket.rb +0 -23
  123. data/lib/mongo/util/uri_parser.rb +0 -337
  124. data/test/functional/connection_test.rb +0 -449
  125. data/test/functional/threading_test.rb +0 -95
  126. data/test/replica_set/complex_connect_test.rb +0 -64
  127. data/test/shared/authentication.rb +0 -66
  128. data/test/unit/pool_test.rb +0 -9
  129. data/test/unit/util_test.rb +0 -55
@@ -1,6 +1,21 @@
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
+
1
15
  require 'test_helper'
2
16
 
3
- class ReadPrefTest < Test::Unit::TestCase
17
+ class ReadPreferenceUnitTest < Test::Unit::TestCase
18
+
4
19
  include ReadPreference
5
20
 
6
21
  def setup
@@ -18,4 +33,374 @@ class ReadPrefTest < Test::Unit::TestCase
18
33
  end
19
34
  end
20
35
 
36
+ def test_sok_mapreduce_out_string_returns_false
37
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
38
+ 'out', 'new-test-collection']
39
+ assert_equal false, ReadPreference::secondary_ok?(command)
40
+ end
41
+
42
+ def test_sok_mapreduce_replace_collection_returns_false
43
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
44
+ 'out', BSON::OrderedHash['replace', 'new-test-collection']]
45
+ assert_equal false, ReadPreference::secondary_ok?(command)
46
+ end
47
+
48
+ def test_sok_mapreduce_inline_collection_returns_false
49
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
50
+ 'out', 'inline']
51
+ assert_equal false, ReadPreference::secondary_ok?(command)
52
+ end
53
+
54
+ def test_sok_inline_symbol_mapreduce_returns_true
55
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
56
+ 'out', BSON::OrderedHash[:inline, 'true']]
57
+ assert_equal true, ReadPreference::secondary_ok?(command)
58
+ end
59
+
60
+ def test_sok_inline_string_mapreduce_returns_true
61
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
62
+ 'out', BSON::OrderedHash['inline', 'true']]
63
+ assert_equal true, ReadPreference::secondary_ok?(command)
64
+ end
65
+
66
+ def test_sok_count_true
67
+ command = BSON::OrderedHash['count', 'test-collection',
68
+ 'query', BSON::OrderedHash['a', 'b']]
69
+ assert_equal true, ReadPreference::secondary_ok?(command)
70
+ end
71
+
72
+ def test_sok_server_status_returns_false
73
+ command = BSON::OrderedHash['serverStatus', 1]
74
+ assert_equal false, ReadPreference::secondary_ok?(command)
75
+ end
76
+
77
+ def test_sok_text_returns_true
78
+ command = BSON::OrderedHash['text', BSON::OrderedHash['search', 'coffee']]
79
+ assert_equal true, ReadPreference::secondary_ok?(command)
80
+ end
81
+
82
+ def test_cmd_reroute_with_secondary
83
+ ReadPreference::expects(:warn).with(regexp_matches(/rerouted to primary/))
84
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
85
+ 'out', 'new-test-collection']
86
+ assert_equal :primary, ReadPreference::cmd_read_pref(:secondary, command)
87
+ end
88
+
89
+ def test_find_and_modify_reroute_with_secondary
90
+ ReadPreference::expects(:warn).with(regexp_matches(/rerouted to primary/))
91
+ command = BSON::OrderedHash['findAndModify', 'test-collection',
92
+ 'query', {}]
93
+ assert_equal :primary, ReadPreference::cmd_read_pref(:secondary, command)
94
+ end
95
+
96
+ def test_cmd_no_reroute_with_secondary
97
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
98
+ 'out', BSON::OrderedHash['inline', 'true']]
99
+ assert_equal :secondary, ReadPreference::cmd_read_pref(:secondary, command)
100
+ end
101
+
102
+ def test_cmd_no_reroute_with_primary
103
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
104
+ 'out', 'new-test-collection']
105
+ assert_equal :primary, ReadPreference::cmd_read_pref(:primary, command)
106
+ end
107
+
108
+ def test_cmd_no_reroute_with_primary_secondary_ok
109
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
110
+ 'out', BSON::OrderedHash['inline', 'true']]
111
+ assert_equal :primary, ReadPreference::cmd_read_pref(:primary, command)
112
+ end
113
+
114
+ def test_parallel_scan_secondary_ok
115
+ command = BSON::OrderedHash['parallelCollectionScan', 'test-collection',
116
+ 'numCursors', 3]
117
+ assert_equal true, ReadPreference::secondary_ok?(command)
118
+ end
119
+
120
+ def test_primary_with_tags_raises_error
121
+ # Confirm that an error is raised if you provide tags and read pref is primary
122
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
123
+ client.stubs(:primary_pool).returns(mock_pool)
124
+ read_pref_tags = {'dc' => 'nyc'}
125
+ read_pref = client.read_preference.merge(:mode => :primary,
126
+ :tags => [read_pref_tags],
127
+ :latency => 6)
128
+ assert_raise Mongo::MongoArgumentError do
129
+ client.select_pool(read_pref)
130
+ end
131
+ end
132
+
133
+ def test_secondary_pref
134
+ # Confirm that a primary is not selected
135
+ primary_pool = mock('pool')
136
+
137
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
138
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 5)
139
+ secondary_pools = [secondary_nyc, secondary_chi]
140
+
141
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
142
+ client.stubs(:primary_pool).returns(primary_pool)
143
+ client.stubs(:secondary_pools).returns(secondary_pools)
144
+
145
+ read_pref = client.read_preference.merge(:mode => :secondary)
146
+ assert_not_equal Hash.new, client.select_pool(read_pref).tags
147
+ end
148
+
149
+ def test_secondary_tags_pref
150
+ # Confirm that a secondary with matching tags is selected
151
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
152
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 5)
153
+ secondary_pools = [secondary_nyc, secondary_chi]
154
+
155
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
156
+ client.stubs(:primary_pool).returns(mock_pool)
157
+ client.stubs(:secondary_pools).returns(secondary_pools)
158
+
159
+ read_pref_tags = {'dc' => 'nyc'}
160
+ read_pref = client.read_preference.merge(:mode => :secondary,
161
+ :tags => [read_pref_tags])
162
+ assert_equal read_pref_tags, client.select_pool(read_pref).tags
163
+ end
164
+
165
+ def test_secondary_tags_with_latency
166
+ # Confirm that between more than 1 secondary matching tags, only the one within
167
+ # max acceptable latency is selected
168
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
169
+ secondary_nyc2 = mock_pool({'dc' => 'nyc'}, 25)
170
+ secondary_pools = [secondary_nyc, secondary_nyc2]
171
+
172
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
173
+ client.stubs(:secondary_pools).returns(secondary_pools)
174
+
175
+ read_pref_tags = {'dc' => 'nyc'}
176
+ read_pref = client.read_preference.merge(:mode => :secondary,
177
+ :tags => [read_pref_tags])
178
+ assert_equal 5, client.select_pool(read_pref).ping_time
179
+ end
180
+
181
+ def test_secondary_latency_pref
182
+ # Confirm that only the latency of pools matching tags is considered
183
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 10)
184
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 5)
185
+ secondary_pools = [secondary_nyc, secondary_chi]
186
+
187
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
188
+ client.stubs(:primary_pool).returns(mock_pool)
189
+ client.stubs(:secondary_pools).returns(secondary_pools)
190
+
191
+ read_pref_tags = {'dc' => 'nyc'}
192
+ read_pref = client.read_preference.merge(:mode => :secondary,
193
+ :tags => [read_pref_tags],
194
+ :latency => 3)
195
+ assert_equal read_pref_tags, client.select_pool(read_pref).tags
196
+ end
197
+
198
+ def test_primary_preferred_primary_available
199
+ # Confirm that the primary is always selected if it's available
200
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
201
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
202
+ secondary_pools = [secondary_nyc, secondary_chi]
203
+ primary_pool = mock_pool
204
+
205
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
206
+ client.stubs(:secondary_pools).returns(secondary_pools)
207
+ client.stubs(:primary_pool).returns(primary_pool)
208
+
209
+ read_pref_tags = {'dc' => 'chicago'}
210
+ read_pref = client.read_preference.merge(:mode => :primary_preferred,
211
+ :tags => [read_pref_tags],
212
+ :latency => 6)
213
+ assert_equal primary_pool, client.select_pool(read_pref)
214
+ end
215
+
216
+ def test_primary_preferred_primary_not_available
217
+ # Confirm that a secondary with matching tags is selected if primary is
218
+ # not available
219
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
220
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
221
+ secondary_pools = [secondary_nyc, secondary_chi]
222
+
223
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
224
+ client.stubs(:secondary_pools).returns(secondary_pools)
225
+
226
+ read_pref_tags = {'dc' => 'chicago'}
227
+ read_pref = client.read_preference.merge(:mode => :primary_preferred,
228
+ :tags => [read_pref_tags],
229
+ :latency => 6)
230
+ assert_equal read_pref_tags, client.select_pool(read_pref).tags
231
+ end
232
+
233
+ def test_primary_preferred_primary_not_available_and_no_matching_tags
234
+ # Confirm that tags are taken into account if primary is not available and
235
+ # secondaries are considered for selection.
236
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
237
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
238
+ secondary_pools = [secondary_nyc, secondary_chi]
239
+
240
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
241
+ client.stubs(:secondary_pools).returns(secondary_pools)
242
+
243
+ read_pref_tags = {'dc' => 'other_city'}
244
+ read_pref = client.read_preference.merge(:mode => :primary_preferred,
245
+ :tags => [read_pref_tags],
246
+ :latency => 6)
247
+ assert_equal nil, client.select_pool(read_pref)
248
+ end
249
+
250
+ def test_secondary_preferred_with_tags
251
+ # Confirm that tags are taken into account
252
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
253
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
254
+ secondary_pools = [secondary_nyc, secondary_chi]
255
+
256
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
257
+ client.stubs(:secondary_pools).returns(secondary_pools)
258
+
259
+ read_pref_tags = {'dc' => 'chicago'}
260
+ read_pref = client.read_preference.merge(:mode => :secondary_preferred,
261
+ :tags => [read_pref_tags],
262
+ :latency => 6)
263
+ assert_equal read_pref_tags, client.select_pool(read_pref).tags
264
+ end
265
+
266
+ def test_secondary_preferred_with_no_matching_tags
267
+ # Confirm that the primary is selected if no secondaries with matching tags
268
+ # are found
269
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
270
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
271
+ secondary_pools = [secondary_nyc, secondary_chi]
272
+
273
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
274
+ client.stubs(:secondary_pools).returns(secondary_pools)
275
+ client.stubs(:primary_pool).returns(mock_pool)
276
+
277
+ read_pref_tags = {'dc' => 'other_city'}
278
+ read_pref = client.read_preference.merge(:mode => :secondary_preferred,
279
+ :tags => [read_pref_tags],
280
+ :latency => 6)
281
+ assert_equal Hash.new, client.select_pool(read_pref).tags
282
+ end
283
+
284
+ def test_nearest_with_tags
285
+ # Confirm that tags are taken into account when selecting nearest
286
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
287
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
288
+ primary_pool = mock_pool
289
+ pools = [secondary_nyc, secondary_chi, primary_pool]
290
+
291
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
292
+ client.stubs(:pools).returns(pools)
293
+
294
+ read_pref_tags = {'dc' => 'nyc'}
295
+ read_pref = client.read_preference.merge(:mode => :nearest,
296
+ :tags => [read_pref_tags],
297
+ :latency => 3)
298
+ assert_equal read_pref_tags, client.select_pool(read_pref).tags
299
+ end
300
+
301
+ def test_nearest
302
+ # Confirm that the nearest is selected when tags aren't specified
303
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
304
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
305
+ primary_pool = mock_pool({}, 1)
306
+ pools = [secondary_nyc, secondary_chi, primary_pool]
307
+
308
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
309
+ client.stubs(:pools).returns(pools)
310
+
311
+ read_pref = client.read_preference.merge(:mode => :nearest,
312
+ :latency => 3)
313
+ assert_equal Hash.new, client.select_pool(read_pref).tags
314
+ end
315
+
316
+ def test_nearest_primary_matching
317
+ # Confirm that a primary matching tags is included in nearest candidates
318
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
319
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
320
+ primary_pool = mock_pool({'dc' => 'boston'}, 1)
321
+ secondary_pools = [secondary_nyc, secondary_chi]
322
+
323
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
324
+ client.stubs(:secondary_pools).returns(secondary_pools)
325
+ client.stubs(:primary_pool).returns(primary_pool)
326
+ client.stubs(:pools).returns(secondary_pools << primary_pool)
327
+
328
+ read_pref_tags = {'dc' => 'boston'}
329
+ read_pref = client.read_preference.merge(:mode => :nearest,
330
+ :tags => [read_pref_tags])
331
+ assert_equal primary_pool, client.select_pool(read_pref)
332
+ end
333
+
334
+ def test_nearest_primary_not_matching
335
+ # Confirm that a primary not matching tags is not included in nearest candidates
336
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 25)
337
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 25)
338
+ primary_pool = mock_pool({'dc' => 'boston'}, 1)
339
+ secondary_pools = [secondary_nyc, secondary_chi]
340
+
341
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
342
+ client.stubs(:secondary_pools).returns(secondary_pools)
343
+ client.stubs(:primary_pool).returns(mock_pool)
344
+ client.stubs(:pools).returns(secondary_pools << primary_pool)
345
+
346
+ read_pref_tags = {'dc' => 'SF'}
347
+ read_pref = client.read_preference.merge(:mode => :nearest,
348
+ :tags => [read_pref_tags])
349
+ assert_equal nil, client.select_pool(read_pref)
350
+ end
351
+
352
+ def test_nearest_primary_not_matching_excluded_from_latency_calculations
353
+ # Confirm that a primary not matching tags is not included in the latency calculations
354
+ secondary1 = mock_pool({'dc' => 'nyc'}, 10)
355
+ secondary2 = mock_pool({'dc' => 'nyc'}, 10)
356
+ primary_pool = mock_pool({'dc' => 'boston'}, 1)
357
+ secondary_pools = [secondary1, secondary2]
358
+
359
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
360
+ client.stubs(:secondary_pools).returns(secondary_pools)
361
+ client.stubs(:primary_pool).returns(mock_pool)
362
+ client.stubs(:pools).returns(secondary_pools << primary_pool)
363
+
364
+ read_pref_tags = {'dc' => 'nyc'}
365
+ read_pref = client.read_preference.merge(:mode => :nearest,
366
+ :tags => [read_pref_tags],
367
+ :latency => 5)
368
+ assert_equal 'nyc', client.select_pool(read_pref).tags['dc']
369
+ end
370
+
371
+ def test_nearest_matching_tags_but_not_available
372
+ # Confirm that even if a server matches a tag, it's not selected if it's down
373
+ secondary_nyc = mock_pool({'dc' => 'nyc'}, 5)
374
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
375
+ primary_pool = mock_pool({'dc' => 'chicago'}, nil)
376
+ secondary_pools = [secondary_nyc, secondary_chi]
377
+
378
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
379
+ client.stubs(:secondary_pools).returns(secondary_pools)
380
+ client.stubs(:primary_pool).returns(primary_pool)
381
+ client.stubs(:pools).returns(secondary_pools << primary_pool)
382
+
383
+ tags = [{'dc' => 'nyc'}, {'dc' => 'chicago'}, {}]
384
+ read_pref = client.read_preference.merge(:mode => :nearest,
385
+ :tags => tags)
386
+ assert_equal secondary_nyc, client.select_pool(read_pref)
387
+ end
388
+
389
+ def test_nearest_multiple_tags
390
+ # Confirm that with multiple tags in the read preference, servers are still selected
391
+ secondary_nyc = mock_pool({}, 5)
392
+ secondary_chi = mock_pool({'dc' => 'chicago'}, 10)
393
+ primary_pool = mock_pool({}, 1)
394
+ secondary_pools = [secondary_nyc, secondary_chi]
395
+
396
+ client = MongoReplicaSetClient.new(["#{TEST_HOST}:#{TEST_PORT}"], :connect => false)
397
+ client.stubs(:secondary_pools).returns(secondary_pools)
398
+ client.stubs(:primary_pool).returns(mock_pool)
399
+ client.stubs(:pools).returns(secondary_pools << primary_pool)
400
+
401
+ tags = [{'dc' => 'nyc'}, {'dc' => 'chicago'}, {}]
402
+ read_pref = client.read_preference.merge(:mode => :nearest,
403
+ :tags => tags)
404
+ assert_equal secondary_chi, client.select_pool(read_pref)
405
+ end
21
406
  end
@@ -1,6 +1,20 @@
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
+
1
15
  require 'test_helper'
2
16
 
3
- class ReadTest < Test::Unit::TestCase
17
+ class ReadUnitTest < Test::Unit::TestCase
4
18
 
5
19
  context "Read mode on standard connection: " do
6
20
  setup do
@@ -32,29 +46,29 @@ class ReadTest < Test::Unit::TestCase
32
46
  end
33
47
 
34
48
  should "propogate to DB" do
35
- db = @client['foo']
49
+ db = @client[TEST_DB]
36
50
  assert_equal @read, db.read
37
51
  assert_equal @tags, db.tag_sets
38
52
  assert_equal @acceptable_latency, db.acceptable_latency
39
53
 
40
- db = @client.db('foo')
54
+ db = @client.db(TEST_DB)
41
55
  assert_equal @read, db.read
42
56
  assert_equal @tags, db.tag_sets
43
57
  assert_equal @acceptable_latency, db.acceptable_latency
44
58
 
45
- db = DB.new('foo', @client)
59
+ db = DB.new(TEST_DB, @client)
46
60
  assert_equal @read, db.read
47
61
  assert_equal @tags, db.tag_sets
48
62
  assert_equal @acceptable_latency, db.acceptable_latency
49
63
  end
50
64
 
51
65
  should "allow db override" do
52
- db = DB.new('foo', @client, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
66
+ db = DB.new(TEST_DB, @client, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
53
67
  assert_equal :primary, db.read
54
68
  assert_equal @bad_tags, db.tag_sets
55
69
  assert_equal 25, db.acceptable_latency
56
70
 
57
- db = @client.db('foo', :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
71
+ db = @client.db(TEST_DB, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
58
72
  assert_equal :primary, db.read
59
73
  assert_equal @bad_tags, db.tag_sets
60
74
  assert_equal 25, db.acceptable_latency
@@ -62,33 +76,33 @@ class ReadTest < Test::Unit::TestCase
62
76
 
63
77
  context "on DB: " do
64
78
  setup do
65
- @db = @client['foo']
79
+ @db = @client[TEST_DB]
66
80
  end
67
81
 
68
82
  should "propogate to collection" do
69
- col = @db.collection('bar')
83
+ col = @db.collection('read-unit-test')
70
84
  assert_equal @read, col.read
71
85
  assert_equal @tags, col.tag_sets
72
86
  assert_equal @acceptable_latency, col.acceptable_latency
73
87
 
74
- col = @db['bar']
88
+ col = @db['read-unit-test']
75
89
  assert_equal @read, col.read
76
90
  assert_equal @tags, col.tag_sets
77
91
  assert_equal @acceptable_latency, col.acceptable_latency
78
92
 
79
- col = Collection.new('bar', @db)
93
+ col = Collection.new('read-unit-test', @db)
80
94
  assert_equal @read, col.read
81
95
  assert_equal @tags, col.tag_sets
82
96
  assert_equal @acceptable_latency, col.acceptable_latency
83
97
  end
84
98
 
85
99
  should "allow override on collection" do
86
- col = @db.collection('bar', :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
100
+ col = @db.collection('read-unit-test', :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
87
101
  assert_equal :primary, col.read
88
102
  assert_equal @bad_tags, col.tag_sets
89
103
  assert_equal 25, col.acceptable_latency
90
104
 
91
- col = Collection.new('bar', @db, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
105
+ col = Collection.new('read-unit-test', @db, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
92
106
  assert_equal :primary, col.read
93
107
  assert_equal @bad_tags, col.tag_sets
94
108
  assert_equal 25, col.acceptable_latency
@@ -97,7 +111,7 @@ class ReadTest < Test::Unit::TestCase
97
111
 
98
112
  context "on read mode ops" do
99
113
  setup do
100
- @col = @client['foo']['bar']
114
+ @col = @client[TEST_DB]['read-unit-test']
101
115
  @mock_socket = new_mock_socket
102
116
  end
103
117
 
@@ -1,6 +1,20 @@
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
+
1
15
  require 'test_helper'
2
16
 
3
- class SafeTest < Test::Unit::TestCase
17
+ class SafeUnitTest < Test::Unit::TestCase
4
18
 
5
19
  context "Write-Concern modes on Mongo::Connection " do
6
20
  setup do
@@ -9,28 +23,28 @@ class SafeTest < Test::Unit::TestCase
9
23
  end
10
24
 
11
25
  should "propogate to DB" do
12
- db = @connection['foo']
26
+ db = @connection[TEST_DB]
13
27
  assert_equal @safe_value[:w], db.write_concern[:w]
14
28
 
15
29
 
16
- db = @connection.db('foo')
30
+ db = @connection.db(TEST_DB)
17
31
  assert_equal @safe_value[:w], db.write_concern[:w]
18
32
 
19
- db = DB.new('foo', @connection)
33
+ db = DB.new(TEST_DB, @connection)
20
34
  assert_equal @safe_value[:w], db.write_concern[:w]
21
35
  end
22
36
 
23
37
  should "allow db override" do
24
- db = DB.new('foo', @connection, :safe => false)
38
+ db = DB.new(TEST_DB, @connection, :safe => false)
25
39
  assert_equal 0, db.write_concern[:w]
26
40
 
27
- db = @connection.db('foo', :safe => false)
41
+ db = @connection.db(TEST_DB, :safe => false)
28
42
  assert_equal 0, db.write_concern[:w]
29
43
  end
30
44
 
31
45
  context "on DB: " do
32
46
  setup do
33
- @db = @connection['foo']
47
+ @db = @connection[TEST_DB]
34
48
  end
35
49
 
36
50
  should "propogate to collection" do
@@ -55,7 +69,7 @@ class SafeTest < Test::Unit::TestCase
55
69
 
56
70
  context "on operations supporting safe mode" do
57
71
  setup do
58
- @col = @connection['foo']['bar']
72
+ @col = @connection[TEST_DB]['bar']
59
73
  end
60
74
 
61
75
  should "use default value on insert" do
@@ -1,7 +1,21 @@
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
+
1
15
  require 'test_helper'
2
16
  include Mongo
3
17
 
4
- class ShardingPoolManagerTest < Test::Unit::TestCase
18
+ class ShardingPoolManagerUnitTest < Test::Unit::TestCase
5
19
 
6
20
  context "Initialization: " do
7
21
 
@@ -17,6 +31,7 @@ class ShardingPoolManagerTest < Test::Unit::TestCase
17
31
  @client.stubs(:socket_class).returns(TCPSocket)
18
32
  @client.stubs(:mongos?).returns(true)
19
33
  @client.stubs(:[]).returns(@db)
34
+ @client.stubs(:socket_opts)
20
35
 
21
36
  @client.stubs(:replica_set_name).returns(nil)
22
37
  @client.stubs(:log)
@@ -30,8 +45,10 @@ class ShardingPoolManagerTest < Test::Unit::TestCase
30
45
  @ismaster = {
31
46
  'hosts' => @hosts,
32
47
  'arbiters' => @arbiters,
48
+ 'maxBsonObjectSize' => 1024,
33
49
  'maxMessageSizeBytes' => 1024 * 2.5,
34
- 'maxBsonObjectSize' => 1024
50
+ 'maxWireVersion' => 1,
51
+ 'minWireVersion' => 0
35
52
  }
36
53
  end
37
54
 
@@ -43,8 +60,10 @@ class ShardingPoolManagerTest < Test::Unit::TestCase
43
60
 
44
61
  # Subsequent calls to configure pools.
45
62
  @ismaster.merge({'ismaster' => true}),
46
- @ismaster.merge({'secondary' => true, 'maxMessageSizeBytes' => 700}),
47
63
  @ismaster.merge({'secondary' => true, 'maxBsonObjectSize' => 500}),
64
+ @ismaster.merge({'secondary' => true, 'maxMessageSizeBytes' => 700}),
65
+ @ismaster.merge({'secondary' => true, 'maxWireVersion' => 0}),
66
+ @ismaster.merge({'secondary' => true, 'minWireVersion' => 0}),
48
67
  @ismaster.merge({'arbiterOnly' => true})
49
68
  )
50
69
 
@@ -57,7 +76,9 @@ class ShardingPoolManagerTest < Test::Unit::TestCase
57
76
 
58
77
  assert manager.seeds.include? formatted_seed
59
78
  assert_equal 500, manager.max_bson_size
60
- assert_equal 700 , manager.max_message_size
79
+ assert_equal 700, manager.max_message_size
80
+ assert_equal 0, manager.max_wire_version
81
+ assert_equal 0, manager.min_wire_version
61
82
  end
62
83
  end
63
84
  end