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,10 +1,24 @@
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
17
  class GridFileSystemTest < Test::Unit::TestCase
4
18
  context "GridFileSystem:" do
5
19
  setup do
6
20
  @con = standard_connection
7
- @db = @con.db(MONGO_TEST_DB)
21
+ @db = @con.db(TEST_DB)
8
22
  end
9
23
 
10
24
  teardown do
@@ -73,7 +87,7 @@ class GridFileSystemTest < Test::Unit::TestCase
73
87
  end
74
88
 
75
89
  should "return the first n bytes even with an offset" do
76
- data = @grid.open('sample.file', 'r') do |f|
90
+ data = @grid.open('sample.file', 'r') do |f|
77
91
  f.seek(1000)
78
92
  f.read(288888)
79
93
  end
@@ -83,116 +97,114 @@ class GridFileSystemTest < Test::Unit::TestCase
83
97
  end
84
98
 
85
99
  context "When writing:" do
86
- setup do
87
- @data = "BYTES" * 50
88
- @grid = GridFileSystem.new(@db)
89
- @grid.open('sample', 'w') do |f|
90
- f.write @data
91
- end
92
- end
93
-
94
- should "read sample data" do
95
- data = @grid.open('sample', 'r') { |f| f.read }
96
- assert_equal data.length, @data.length
97
- end
98
-
99
- should "return the total number of bytes written" do
100
- data = 'a' * 300000
101
- assert_equal 300000, @grid.open('sample', 'w') {|f| f.write(data) }
102
- end
103
-
104
- should "more read sample data" do
105
- data = @grid.open('sample', 'r') { |f| f.read }
106
- assert_equal data.length, @data.length
107
- end
108
-
109
- should "raise exception if file not found" do
110
- assert_raise GridFileNotFound do
111
- @grid.open('io', 'r') { |f| f.write('hello') }
112
- end
113
- end
114
-
115
- should "raise exception if not opened for write" do
116
- assert_raise GridError do
117
- @grid.open('sample', 'r') { |f| f.write('hello') }
118
- end
119
- end
120
-
121
- context "and when overwriting the file" do
122
- setup do
123
- @old = @grid.open('sample', 'r')
124
-
125
- @new_data = "DATA" * 10
126
- sleep(2)
127
- @grid.open('sample', 'w') do |f|
128
- f.write @new_data
129
- end
130
-
131
- @new = @grid.open('sample', 'r')
132
- end
133
-
134
- should "have a newer upload date" do
135
- assert @new.upload_date > @old.upload_date, "New data is not greater than old date."
136
- end
137
-
138
- should "have a different files_id" do
139
- assert_not_equal @new.files_id, @old.files_id
140
- end
141
-
142
- should "contain the new data" do
143
- assert_equal @new_data, @new.read, "Expected DATA"
144
- end
145
-
146
- context "and on a second overwrite" do
147
- setup do
148
- sleep(2)
149
- @new_data = "NEW" * 1000
150
- @grid.open('sample', 'w') do |f|
151
- f.write @new_data
152
- end
153
-
154
- @ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
155
- end
156
-
157
- should "write a third version of the file" do
158
- assert_equal 3, @db['fs.files'].find({'filename' => 'sample'}).count
159
- assert_equal 3, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
160
- end
161
-
162
- should "remove all versions and their data on delete" do
163
- @grid.delete('sample')
164
- assert_equal 0, @db['fs.files'].find({'filename' => 'sample'}).count
165
- assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
166
- end
167
-
168
- should "delete all versions which exceed the number of versions to keep specified by the option :versions" do
169
- @versions = 1 + rand(4-1)
170
- @grid.open('sample', 'w', :versions => @versions) do |f|
171
- f.write @new_data
172
- end
173
- @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
174
- assert_equal @versions, @new_ids.length
175
- id = @new_ids.first
176
- assert !@ids.include?(id)
177
- assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count
178
- end
179
-
180
- should "delete old versions on write with :delete_old is passed in" do
181
- @grid.open('sample', 'w', :delete_old => true) do |f|
182
- f.write @new_data
183
- end
184
- @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
185
- assert_equal 1, @new_ids.length
186
- id = @new_ids.first
187
- assert !@ids.include?(id)
188
- assert_equal 1, @db['fs.files'].find({'filename' => 'sample'}).count
189
- assert_equal 1, @db['fs.chunks'].find({'files_id' => id}).count
190
- end
191
- end
192
- end
193
- end
194
-
195
- context "When writing chunks:" do
100
+ setup do
101
+ @data = "BYTES" * 50
102
+ @grid = GridFileSystem.new(@db)
103
+ @grid.open('sample', 'w') do |f|
104
+ f.write @data
105
+ end
106
+ end
107
+
108
+ should "read sample data" do
109
+ data = @grid.open('sample', 'r') { |f| f.read }
110
+ assert_equal data.length, @data.length
111
+ end
112
+
113
+ should "return the total number of bytes written" do
114
+ data = 'a' * 300000
115
+ assert_equal 300000, @grid.open('sample', 'w') {|f| f.write(data) }
116
+ end
117
+
118
+ should "more read sample data" do
119
+ data = @grid.open('sample', 'r') { |f| f.read }
120
+ assert_equal data.length, @data.length
121
+ end
122
+
123
+ should "raise exception if file not found" do
124
+ assert_raise GridFileNotFound do
125
+ @grid.open('io', 'r') { |f| f.write('hello') }
126
+ end
127
+ end
128
+
129
+ should "raise exception if not opened for write" do
130
+ assert_raise GridError do
131
+ @grid.open('sample', 'r') { |f| f.write('hello') }
132
+ end
133
+ end
134
+
135
+ context "and when overwriting the file" do
136
+ setup do
137
+ @old = @grid.open('sample', 'r')
138
+
139
+ @new_data = "DATA" * 10
140
+ @grid.open('sample', 'w') do |f|
141
+ f.write @new_data
142
+ end
143
+
144
+ @new = @grid.open('sample', 'r')
145
+ end
146
+
147
+ should "have a newer upload date" do
148
+ assert @new.upload_date > @old.upload_date, "New data is not greater than old date."
149
+ end
150
+
151
+ should "have a different files_id" do
152
+ assert_not_equal @new.files_id, @old.files_id
153
+ end
154
+
155
+ should "contain the new data" do
156
+ assert_equal @new_data, @new.read, "Expected DATA"
157
+ end
158
+
159
+ context "and on a second overwrite" do
160
+ setup do
161
+ @new_data = "NEW" * 1000
162
+ @grid.open('sample', 'w') do |f|
163
+ f.write @new_data
164
+ end
165
+
166
+ @ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
167
+ end
168
+
169
+ should "write a third version of the file" do
170
+ assert_equal 3, @db['fs.files'].find({'filename' => 'sample'}).count
171
+ assert_equal 3, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
172
+ end
173
+
174
+ should "remove all versions and their data on delete" do
175
+ @grid.delete('sample')
176
+ assert_equal 0, @db['fs.files'].find({'filename' => 'sample'}).count
177
+ assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
178
+ end
179
+
180
+ should "delete all versions which exceed the number of versions to keep specified by the option :versions" do
181
+ @versions = 1 + rand(4-1)
182
+ @grid.open('sample', 'w', :versions => @versions) do |f|
183
+ f.write @new_data
184
+ end
185
+ @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
186
+ assert_equal @versions, @new_ids.length
187
+ id = @new_ids.first
188
+ assert !@ids.include?(id)
189
+ assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count
190
+ end
191
+
192
+ should "delete old versions on write with :delete_old is passed in" do
193
+ @grid.open('sample', 'w', :delete_old => true) do |f|
194
+ f.write @new_data
195
+ end
196
+ @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
197
+ assert_equal 1, @new_ids.length
198
+ id = @new_ids.first
199
+ assert !@ids.include?(id)
200
+ assert_equal 1, @db['fs.files'].find({'filename' => 'sample'}).count
201
+ assert_equal 1, @db['fs.chunks'].find({'files_id' => id}).count
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ context "When writing chunks:" do
196
208
  setup do
197
209
  data = "B" * 50000
198
210
  @grid = GridFileSystem.new(@db)
@@ -1,10 +1,24 @@
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
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
@@ -1,3 +1,17 @@
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
 
@@ -21,7 +35,7 @@ end
21
35
  class GridTest < Test::Unit::TestCase
22
36
  context "Tests:" do
23
37
  setup do
24
- @db = standard_connection.db(MONGO_TEST_DB)
38
+ @db = standard_connection.db(TEST_DB)
25
39
  @files = @db.collection('test-fs.files')
26
40
  @chunks = @db.collection('test-fs.chunks')
27
41
  end
@@ -248,7 +262,7 @@ class GridTest < Test::Unit::TestCase
248
262
  end
249
263
 
250
264
  should "put and get a large io object if reading less than the chunk size" do
251
- read_and_write_stream('sample_data', 256 * 1024)
265
+ read_and_write_stream('sample_data', 255 * 1024)
252
266
  end
253
267
 
254
268
  should "put and get a large io object if reading more than the chunk size" do
@@ -1,12 +1,25 @@
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
- require 'thread'
3
16
 
4
17
  class PoolTest < Test::Unit::TestCase
5
18
  include Mongo
6
19
 
7
20
  def setup
8
- @client ||= standard_connection({:pool_size => 500, :pool_timeout => 5})
9
- @db = @client.db(MONGO_TEST_DB)
21
+ @client ||= standard_connection({:pool_size => 15, :pool_timeout => 5})
22
+ @db = @client.db(TEST_DB)
10
23
  @collection = @db.collection("pool_test")
11
24
  end
12
25
 
@@ -18,7 +31,7 @@ class PoolTest < Test::Unit::TestCase
18
31
  threads << Thread.new do
19
32
  original_socket = pool.checkout
20
33
  pool.checkin(original_socket)
21
- 5000.times do
34
+ 500.times do
22
35
  socket = pool.checkout
23
36
  assert_equal original_socket, socket
24
37
  pool.checkin(socket)
@@ -30,18 +43,94 @@ class PoolTest < Test::Unit::TestCase
30
43
  end
31
44
 
32
45
  def test_pool_affinity_max_size
33
- 8000.times {|x| @collection.insert({:value => x})}
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
+
34
53
  threads = []
35
54
  threads << Thread.new do
36
- @collection.find({"value" => {"$lt" => 100}}).each {|e| e}
55
+ coll.find({"value" => {"$lt" => 100}}).each {|e| e}
37
56
  Thread.pass
38
- sleep(5)
39
- @collection.find({"value" => {"$gt" => 100}}).each {|e| e}
57
+ sleep(0.125)
58
+ coll.find({"value" => {"$gt" => 100}}).each {|e| e}
40
59
  end
41
- sleep(1)
42
60
  threads << Thread.new do
43
- @collection.find({'$where' => "function() {for(i=0;i<8000;i++) {this.value};}"}).each {|e| e}
61
+ coll.find({'$where' => "function() {for(i=0;i<500;i++) {this.value};}"}).each {|e| e}
44
62
  end
45
63
  threads.each(&:join)
46
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
47
136
  end
@@ -1,3 +1,17 @@
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
 
@@ -5,7 +19,7 @@ class SafeTest < Test::Unit::TestCase
5
19
  context "Safe mode propogation: " do
6
20
  setup do
7
21
  @connection = standard_connection({:safe => true}, true) # Legacy
8
- @db = @connection[MONGO_TEST_DB]
22
+ @db = @connection[TEST_DB]
9
23
  @collection = @db['test-safe']
10
24
  @collection.create_index([[:a, 1]], :unique => true)
11
25
  @collection.remove
@@ -59,7 +73,7 @@ class SafeTest < Test::Unit::TestCase
59
73
  context "Safe error objects" do
60
74
  setup do
61
75
  @connection = standard_connection({:safe => true}, true) # Legacy
62
- @db = @connection[MONGO_TEST_DB]
76
+ @db = @connection[TEST_DB]
63
77
  @collection = @db['test']
64
78
  @collection.remove
65
79
  @collection.insert({:a => 1})
@@ -71,8 +85,8 @@ class SafeTest < Test::Unit::TestCase
71
85
  response = @collection.update({:a => 1}, {"$set" => {:a => 2}},
72
86
  :multi => true)
73
87
 
74
- assert response['updatedExisting']
75
- assert_equal 3, response['n']
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
76
90
  end
77
91
 
78
92
  should "return object on remove" do
@@ -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
@@ -1,3 +1,17 @@
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
17
  class SupportTest < Test::Unit::TestCase
@@ -1,26 +1,43 @@
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 TestTimeout < Test::Unit::TestCase
17
+ class TimeoutTest < Test::Unit::TestCase
18
+
4
19
  def test_op_timeout
5
- connection = standard_connection(:op_timeout => 1)
20
+ grant_admin_user_eval_role(standard_connection)
21
+ connection = standard_connection(:op_timeout => 0.5)
6
22
 
7
23
  admin = connection.db('admin')
8
24
 
9
- command = {:eval => "sleep(500)"}
25
+ command = {:eval => "sleep(100)"}
10
26
  # Should not timeout
11
27
  assert admin.command(command)
12
28
 
13
29
  # Should timeout
14
- command = {:eval => "sleep(1500)"}
30
+ command = {:eval => "sleep(1000)"}
15
31
  assert_raise Mongo::OperationTimeout do
16
32
  admin.command(command)
17
33
  end
18
34
  end
19
35
 
20
36
  def test_external_timeout_does_not_leave_socket_in_bad_state
21
- client = Mongo::MongoClient.new
22
- db = client[MONGO_TEST_DB]
23
- coll = db['timeout-tests']
37
+ client = standard_connection
38
+ db = client[TEST_DB]
39
+ coll = db['timeout-tests']
40
+ grant_admin_user_eval_role(client)
24
41
 
25
42
  # prepare the database
26
43
  coll.drop
@@ -28,10 +45,10 @@ class TestTimeout < Test::Unit::TestCase
28
45
 
29
46
  # use external timeout to mangle socket
30
47
  begin
31
- Timeout::timeout(1) do
32
- db.command({:eval => "sleep(1500)"})
48
+ Timeout::timeout(0.5) do
49
+ db.command({:eval => "sleep(1000)"})
33
50
  end
34
- rescue Timeout::Error => ex
51
+ rescue Timeout::Error
35
52
  #puts "Thread timed out and has now mangled the socket"
36
53
  end
37
54
 
@@ -40,21 +57,4 @@ class TestTimeout < Test::Unit::TestCase
40
57
  end
41
58
  end
42
59
 
43
- =begin
44
- def test_ssl_op_timeout
45
- connection = standard_connection(:op_timeout => 1, :ssl => true)
46
-
47
- coll = connection.db(MONGO_TEST_DB).collection("test")
48
- coll.insert({:a => 1})
49
- # Should not timeout
50
- assert coll.find_one({"$where" => "sleep(100); return true;"})
51
-
52
- # Should timeout
53
- assert_raise Mongo::OperationTimeout do
54
- coll.find_one({"$where" => "sleep(5 * 1000); return true;"})
55
- end
56
-
57
- coll.remove
58
- end
59
- =end
60
60
  end