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.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -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 +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  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/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  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 +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  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 +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  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 +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  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 +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -1,10 +1,24 @@
1
- require './test/test_helper'
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'
2
16
 
3
17
  class GridIOTest < Test::Unit::TestCase
4
18
 
5
19
  context "GridIO" do
6
20
  setup do
7
- @db = standard_connection.db(MONGO_TEST_DB)
21
+ @db = standard_connection.db(TEST_DB)
8
22
  @files = @db.collection('fs.files')
9
23
  @chunks = @db.collection('fs.chunks')
10
24
  @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]])
@@ -21,9 +35,9 @@ class GridIOTest < Test::Unit::TestCase
21
35
  @mode = 'w'
22
36
  end
23
37
 
24
- should "set default 256k chunk size" do
38
+ should "set default 255k chunk size" do
25
39
  file = GridIO.new(@files, @chunks, @filename, @mode)
26
- assert_equal 256 * 1024, file.chunk_size
40
+ assert_equal 255 * 1024, file.chunk_size
27
41
  end
28
42
 
29
43
  should "set chunk size" do
@@ -45,7 +59,7 @@ class GridIOTest < Test::Unit::TestCase
45
59
  should "read data character by character using" do
46
60
  bytes = 0
47
61
  file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
48
- while char = file.getc
62
+ while file.getc
49
63
  bytes += 1
50
64
  end
51
65
  assert_equal bytes, 1_000_000
@@ -69,6 +83,24 @@ class GridIOTest < Test::Unit::TestCase
69
83
  assert_equal 10, string.length
70
84
  end
71
85
 
86
+ should "read to the end of the file one line at a time" do
87
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
88
+ bytes = 0
89
+ while string = file.gets
90
+ bytes += string.length
91
+ end
92
+ assert_equal 1_000_000, bytes
93
+ end
94
+
95
+ should "read to the end of the file one multi-character separator at a time" do
96
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
97
+ bytes = 0
98
+ while string = file.gets("45")
99
+ bytes += string.length
100
+ end
101
+ assert_equal 1_000_000, bytes
102
+ end
103
+
72
104
  should "read to a given separator" do
73
105
  file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
74
106
  string = file.gets("5")
@@ -93,7 +125,7 @@ class GridIOTest < Test::Unit::TestCase
93
125
 
94
126
  should "tell position, eof, and rewind" do
95
127
  file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
96
- string = file.read(1000)
128
+ file.read(1000)
97
129
  assert_equal 1000, file.pos
98
130
  assert !file.eof?
99
131
  file.read
@@ -104,6 +136,35 @@ class GridIOTest < Test::Unit::TestCase
104
136
  end
105
137
  end
106
138
 
139
+ context "Writing" do
140
+ setup do
141
+ @filename = 'test'
142
+ @length = 50000
143
+ @times = 10
144
+ end
145
+
146
+ should "correctly write multiple chunks from mutiple writes" do
147
+ file = GridIO.new(@files, @chunks, @filename, 'w')
148
+
149
+ @times.times do
150
+ file.write("1" * @length)
151
+ end
152
+
153
+ file.close
154
+
155
+ file = GridIO.new(@files, @chunks, @filename, 'r')
156
+
157
+ total_size = 0
158
+ while !file.eof?
159
+ total_size += file.read(@length).length
160
+ end
161
+
162
+ file.close
163
+
164
+ assert_equal total_size, @times * @length
165
+ end
166
+ end
167
+
107
168
  context "Seeking" do
108
169
  setup do
109
170
  @filename = 'test'
@@ -139,24 +200,24 @@ class GridIOTest < Test::Unit::TestCase
139
200
 
140
201
  context "Grid MD5 check" do
141
202
  should "run in safe mode" do
142
- file = GridIO.new(@files, @chunks, 'smallfile', 'w', :safe => true)
203
+ file = GridIO.new(@files, @chunks, 'smallfile', 'w')
143
204
  file.write("DATA" * 100)
144
205
  assert file.close
145
206
  assert_equal file.server_md5, file.client_md5
146
207
  end
147
208
 
148
209
  should "validate with a large file" do
149
- io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
150
- file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
210
+ io = File.open(File.join(TEST_DATA, 'sample_file.pdf'), 'r')
211
+ file = GridIO.new(@files, @chunks, 'bigfile', 'w')
151
212
  file.write(io)
152
213
  assert file.close
153
214
  assert_equal file.server_md5, file.client_md5
154
215
  end
155
216
 
156
217
  should "raise an exception when check fails" do
157
- io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
218
+ io = File.open(File.join(TEST_DATA, 'sample_file.pdf'), 'r')
158
219
  @db.stubs(:command).returns({'md5' => '12345'})
159
- file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
220
+ file = GridIO.new(@files, @chunks, 'bigfile', 'w')
160
221
  file.write(io)
161
222
  assert_raise GridMD5Failure do
162
223
  assert file.close
@@ -1,8 +1,22 @@
1
- require './test/test_helper'
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'
2
16
  include Mongo
3
17
 
4
18
  def read_and_write_stream(filename, read_length, opts={})
5
- io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
19
+ io = File.open(File.join(TEST_DATA, filename), 'r+b')
6
20
  id = @grid.put(io, opts.merge!(:filename => filename + read_length.to_s))
7
21
  file = @grid.get(id)
8
22
  io.rewind
@@ -13,6 +27,7 @@ def read_and_write_stream(filename, read_length, opts={})
13
27
  read_data = ""
14
28
  while(chunk = file.read(read_length))
15
29
  read_data << chunk
30
+ break if chunk.empty?
16
31
  end
17
32
  assert_equal data.length, read_data.length
18
33
  end
@@ -20,7 +35,7 @@ end
20
35
  class GridTest < Test::Unit::TestCase
21
36
  context "Tests:" do
22
37
  setup do
23
- @db = standard_connection.db(MONGO_TEST_DB)
38
+ @db = standard_connection.db(TEST_DB)
24
39
  @files = @db.collection('test-fs.files')
25
40
  @chunks = @db.collection('test-fs.chunks')
26
41
  end
@@ -30,6 +45,21 @@ class GridTest < Test::Unit::TestCase
30
45
  @chunks.remove
31
46
  end
32
47
 
48
+ context "A one-chunk grid-stored file" do
49
+ setup do
50
+ @data = "GRIDDATA" * 5
51
+ @grid = Grid.new(@db, 'test-fs')
52
+ @id = @grid.put(@data, :filename => 'sample',
53
+ :metadata => {'app' => 'photos'})
54
+ end
55
+
56
+ should "retrieve the file" do
57
+ data = @grid.get(@id).data
58
+ assert_equal @data, data
59
+ end
60
+
61
+ end
62
+
33
63
  context "A basic grid-stored file" do
34
64
  setup do
35
65
  @data = "GRIDDATA" * 50000
@@ -43,19 +73,13 @@ class GridTest < Test::Unit::TestCase
43
73
  assert_equal 'sample', file['filename']
44
74
  end
45
75
 
46
- should "not be able to overwrite an exising file" do
47
- assert_raise GridError do
48
- @grid.put(@data, :filename => 'sample', :_id => @id, :safe => true)
49
- end
50
- end
51
-
52
76
  should "return nil if it doesn't exist" do
53
77
  assert_nil @grid.exist?(:metadata => 'foo')
54
78
  end
55
79
 
56
80
  should "retrieve the stored data" do
57
81
  data = @grid.get(@id).data
58
- assert_equal @data, data
82
+ assert_equal @data.length, data.length
59
83
  end
60
84
 
61
85
  should "have a unique index on chunks" do
@@ -126,7 +150,9 @@ class GridTest < Test::Unit::TestCase
126
150
  end
127
151
 
128
152
  should "ignore special keys" do
129
- id = @grid.put(@data, :file_length => 100, :phrase => "blimey")
153
+ id = silently do
154
+ @grid.put(@data, :file_length => 100, :phrase => "blimey")
155
+ end
130
156
  file = @grid.get(id)
131
157
 
132
158
  assert_equal "blimey", file['phrase']
@@ -137,8 +163,9 @@ class GridTest < Test::Unit::TestCase
137
163
  context "Storing data with a length of zero" do
138
164
  setup do
139
165
  @grid = Grid.new(@db, 'test-fs')
140
- @id = @grid.put('', :filename => 'sample',
141
- :metadata => {'app' => 'photos'})
166
+ @id = silently do
167
+ @grid.put('', :filename => 'sample', :metadata => {'app' => 'photos'})
168
+ end
142
169
  end
143
170
 
144
171
  should "return the zero length" do
@@ -151,7 +178,7 @@ class GridTest < Test::Unit::TestCase
151
178
  setup do
152
179
  @grid = Grid.new(@db, 'test-fs')
153
180
  filename = 'sample_data'
154
- @io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
181
+ @io = File.open(File.join(TEST_DATA, filename), 'r')
155
182
  id = @grid.put(@io, :filename => filename)
156
183
  @file = @grid.get(id)
157
184
  @io.rewind
@@ -161,6 +188,46 @@ class GridTest < Test::Unit::TestCase
161
188
  end
162
189
  end
163
190
 
191
+ should "be equal in length" do
192
+ @io.rewind
193
+ assert_equal @io.read.length, @file.read.length
194
+ end
195
+
196
+ should "read the file" do
197
+ read_data = ""
198
+ @file.each do |chunk|
199
+ read_data << chunk
200
+ end
201
+ assert_equal @data.length, read_data.length
202
+ end
203
+
204
+ should "read the file if no block is given" do
205
+ read_data = @file.each
206
+ assert_equal @data.length, read_data.length
207
+ end
208
+ end
209
+
210
+ context "Grid streaming an empty file: " do
211
+ setup do
212
+ @grid = Grid.new(@db, 'test-fs')
213
+ filename = 'empty_data'
214
+ @io = File.open(File.join(TEST_DATA, filename), 'r')
215
+ id = silently do
216
+ @grid.put(@io, :filename => filename)
217
+ end
218
+ @file = @grid.get(id)
219
+ @io.rewind
220
+ @data = @io.read
221
+ if @data.respond_to?(:force_encoding)
222
+ @data.force_encoding("binary")
223
+ end
224
+ end
225
+
226
+ should "be equal in length" do
227
+ @io.rewind
228
+ assert_equal @io.read.length, @file.read.length
229
+ end
230
+
164
231
  should "read the file" do
165
232
  read_data = ""
166
233
  @file.each do |chunk|
@@ -184,12 +251,18 @@ class GridTest < Test::Unit::TestCase
184
251
  read_and_write_stream('small_data.txt', 1, :chunk_size => 2)
185
252
  end
186
253
 
254
+ should "put and get an empty io object" do
255
+ silently do
256
+ read_and_write_stream('empty_data', 1)
257
+ end
258
+ end
259
+
187
260
  should "put and get a small io object" do
188
261
  read_and_write_stream('small_data.txt', 1)
189
262
  end
190
263
 
191
264
  should "put and get a large io object if reading less than the chunk size" do
192
- read_and_write_stream('sample_data', 256 * 1024)
265
+ read_and_write_stream('sample_data', 255 * 1024)
193
266
  end
194
267
 
195
268
  should "put and get a large io object if reading more than the chunk size" do
@@ -0,0 +1,136 @@
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 PoolTest < Test::Unit::TestCase
18
+ include Mongo
19
+
20
+ def setup
21
+ @client ||= standard_connection({:pool_size => 15, :pool_timeout => 5})
22
+ @db = @client.db(TEST_DB)
23
+ @collection = @db.collection("pool_test")
24
+ end
25
+
26
+ def test_pool_affinity
27
+ pool = Pool.new(@client, TEST_HOST, TEST_PORT, :size => 5)
28
+
29
+ threads = []
30
+ 10.times do
31
+ threads << Thread.new do
32
+ original_socket = pool.checkout
33
+ pool.checkin(original_socket)
34
+ 500.times do
35
+ socket = pool.checkout
36
+ assert_equal original_socket, socket
37
+ pool.checkin(socket)
38
+ end
39
+ end
40
+ end
41
+
42
+ threads.each { |t| t.join }
43
+ end
44
+
45
+ def test_pool_affinity_max_size
46
+ client = standard_connection({:pool_size => 15, :pool_timeout => 5,
47
+ :op_timeout => nil})
48
+ coll = client[TEST_DB]['pool_test']
49
+ docs = []
50
+ 8000.times {|x| docs << {:value => x}}
51
+ coll.insert(docs)
52
+
53
+ threads = []
54
+ threads << Thread.new do
55
+ coll.find({"value" => {"$lt" => 100}}).each {|e| e}
56
+ Thread.pass
57
+ sleep(0.125)
58
+ coll.find({"value" => {"$gt" => 100}}).each {|e| e}
59
+ end
60
+ threads << Thread.new do
61
+ coll.find({'$where' => "function() {for(i=0;i<500;i++) {this.value};}"}).each {|e| e}
62
+ end
63
+ threads.each(&:join)
64
+ end
65
+
66
+ def test_auth_network_error
67
+ # Make sure there's no semaphore leak if we get a network error
68
+ # when authenticating a new socket with cached credentials.
69
+
70
+ # Get a client with one socket so we detect if it's leaked.
71
+ client = MongoClient.new(TEST_HOST, TEST_PORT, :pool_size => 1, :pool_timeout => 1)
72
+ assert_equal 1, client.pool_size
73
+
74
+ # Set up the client with a pool
75
+ client[TEST_DB].command(:ping => 1)
76
+
77
+ # Close the one socket in the pool
78
+ pool = client.primary_pool
79
+ socket = pool.instance_variable_get(:@sockets).first
80
+ socket.close
81
+
82
+ # Simulate an authenticate() call on a different socket.
83
+ # Cache the creds on the client.
84
+ creds = {
85
+ :db_name => TEST_DB,
86
+ :username => TEST_USER,
87
+ :password => TEST_USER_PWD,
88
+ :source => TEST_DB,
89
+ :extra => {}
90
+ }
91
+ client.auths << creds
92
+
93
+ # The client authenticates its socket with the
94
+ # new credential, but gets a socket.error.
95
+ client[TEST_DB]['ruby-test'].find_one
96
+
97
+ # # No semaphore leak, the pool is allowed to make a new socket.
98
+ assert_equal 1, pool.instance_variable_get(:@sockets).size
99
+ end
100
+
101
+ def test_socket_cleanup
102
+ # Get a client with one socket so we detect if it's leaked.
103
+ client = MongoClient.new(TEST_HOST, TEST_PORT, :pool_size => 1, :pool_timeout => 1)
104
+ assert_equal 1, client.pool_size
105
+
106
+ # Set up the client with a pool
107
+ client[TEST_DB].command(:ping => 1)
108
+
109
+ # Simulate an authenticate() call on a different socket.
110
+ # Cache the creds on the client.
111
+ creds = {
112
+ :db_name => TEST_DB,
113
+ :username => TEST_USER,
114
+ :password => TEST_USER_PWD,
115
+ :source => TEST_DB,
116
+ :extra => {}
117
+ }
118
+ client.auths << creds
119
+
120
+ # Mock the socket to raise a ConnectionFailure when applying auths
121
+ pool = client.primary_pool
122
+ socket = pool.instance_variable_get(:@sockets).first
123
+ socket.expects(:send).raises(ConnectionFailure)
124
+
125
+ # Checkout a socket from the pool to force it to get a new socket
126
+ pool.checkout
127
+ new_socket = pool.instance_variable_get(:@sockets).first
128
+
129
+ # Make sure the pool is cleaned up properly
130
+ assert_not_equal socket, new_socket
131
+ assert_equal 1, pool.instance_variable_get(:@sockets).size
132
+ assert_equal 1, pool.instance_variable_get(:@thread_ids_to_sockets).size
133
+ assert !pool.instance_variable_get(:@checked_out).include?(socket)
134
+ assert !pool.instance_variable_get(:@sockets).include?(socket)
135
+ end
136
+ end
@@ -0,0 +1,98 @@
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
+ include Mongo
17
+
18
+ class SafeTest < Test::Unit::TestCase
19
+ context "Safe mode propogation: " do
20
+ setup do
21
+ @connection = standard_connection({:safe => true}, true) # Legacy
22
+ @db = @connection[TEST_DB]
23
+ @collection = @db['test-safe']
24
+ @collection.create_index([[:a, 1]], :unique => true)
25
+ @collection.remove
26
+ end
27
+
28
+ should "propogate safe option on insert" do
29
+ @collection.insert({:a => 1})
30
+
31
+ assert_raise_error(OperationFailure, "duplicate key") do
32
+ @collection.insert({:a => 1})
33
+ end
34
+ end
35
+
36
+ should "allow safe override on insert" do
37
+ @collection.insert({:a => 1})
38
+ @collection.insert({:a => 1}, :safe => false)
39
+ end
40
+
41
+ should "allow safe override on save" do
42
+ @collection.insert({:a => 1})
43
+ id = @collection.insert({:a => 2})
44
+ assert_nothing_raised do
45
+ @collection.save({:_id => id.to_s, :a => 1}, :safe => false)
46
+ end
47
+ end
48
+
49
+ should "propogate safe option on save" do
50
+ @collection.insert({:a => 1})
51
+ id = @collection.insert({:a => 2})
52
+ assert_raise(OperationFailure) do
53
+ @collection.save({:_id => id.to_s, :a => 1})
54
+ end
55
+ end
56
+
57
+ should "propogate safe option on update" do
58
+ @collection.insert({:a => 1})
59
+ @collection.insert({:a => 2})
60
+
61
+ assert_raise_error(OperationFailure, "duplicate key") do
62
+ @collection.update({:a => 2}, {:a => 1})
63
+ end
64
+ end
65
+
66
+ should "allow safe override on update" do
67
+ @collection.insert({:a => 1})
68
+ @collection.insert({:a => 2})
69
+ @collection.update({:a => 2}, {:a => 1}, :safe => false)
70
+ end
71
+ end
72
+
73
+ context "Safe error objects" do
74
+ setup do
75
+ @connection = standard_connection({:safe => true}, true) # Legacy
76
+ @db = @connection[TEST_DB]
77
+ @collection = @db['test']
78
+ @collection.remove
79
+ @collection.insert({:a => 1})
80
+ @collection.insert({:a => 1})
81
+ @collection.insert({:a => 1})
82
+ end
83
+
84
+ should "return object on update" do
85
+ response = @collection.update({:a => 1}, {"$set" => {:a => 2}},
86
+ :multi => true)
87
+
88
+ assert(response['updatedExisting'] || @db.connection.wire_version_feature?(Mongo::MongoClient::BATCH_COMMANDS)) # TODO - review new write command return values
89
+ assert(response['n'] == 3 || @db.connection.wire_version_feature?(Mongo::MongoClient::BATCH_COMMANDS)) # TODO - update command top pending
90
+ end
91
+
92
+ should "return object on remove" do
93
+ response = @collection.remove({})
94
+ assert_equal 3, response['n']
95
+ end
96
+ end
97
+
98
+ end
@@ -0,0 +1,29 @@
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
+ require 'shared/ssl_shared'
17
+
18
+ class SSLTest < Test::Unit::TestCase
19
+ include Mongo
20
+ include SSLTests
21
+
22
+ def setup
23
+ @client_class = MongoClient
24
+ @uri_info = 'server'
25
+ @connect_info = ['server', 27017]
26
+ @bad_connect_info = ['localhost', 27017]
27
+ end
28
+
29
+ end
@@ -0,0 +1,62 @@
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 SupportTest < Test::Unit::TestCase
18
+
19
+ def test_command_response_succeeds
20
+ assert Support.ok?('ok' => 1)
21
+ assert Support.ok?('ok' => 1.0)
22
+ assert Support.ok?('ok' => true)
23
+ end
24
+
25
+ def test_command_response_fails
26
+ assert !Support.ok?('ok' => 0)
27
+ assert !Support.ok?('ok' => 0.0)
28
+ assert !Support.ok?('ok' => 0.0)
29
+ assert !Support.ok?('ok' => 'str')
30
+ assert !Support.ok?('ok' => false)
31
+ end
32
+
33
+ def test_array_of_pairs
34
+ hps = [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]]
35
+ assert_equal [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]], Support.normalize_seeds(hps)
36
+ end
37
+
38
+ def test_array_of_strings
39
+ hps = ["localhost:27017", "localhost:27018", "localhost:27019"]
40
+ assert_equal [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]], Support.normalize_seeds(hps)
41
+ end
42
+
43
+ def test_single_string_with_host_port
44
+ hps = "localhost:27017"
45
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
46
+ end
47
+
48
+ def test_single_string_missing_port
49
+ hps = "localhost"
50
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
51
+ end
52
+
53
+ def test_single_element_array_missing_port
54
+ hps = ["localhost"]
55
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
56
+ end
57
+
58
+ def test_pair_doesnt_get_converted
59
+ hps = ["localhost", 27017]
60
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
61
+ end
62
+ end