mongo 1.6.2 → 1.6.4

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 (76) hide show
  1. data/README.md +44 -22
  2. data/Rakefile +17 -4
  3. data/docs/GridFS.md +2 -2
  4. data/docs/HISTORY.md +15 -1
  5. data/docs/RELEASES.md +4 -4
  6. data/docs/TUTORIAL.md +12 -0
  7. data/docs/WRITE_CONCERN.md +1 -1
  8. data/lib/mongo/collection.rb +1 -1
  9. data/lib/mongo/connection.rb +35 -47
  10. data/lib/mongo/cursor.rb +10 -9
  11. data/lib/mongo/db.rb +1 -1
  12. data/lib/mongo/gridfs/grid_ext.rb +4 -4
  13. data/lib/mongo/gridfs/grid_file_system.rb +3 -3
  14. data/lib/mongo/gridfs/grid_io.rb +1 -1
  15. data/lib/mongo/networking.rb +5 -0
  16. data/lib/mongo/repl_set_connection.rb +47 -21
  17. data/lib/mongo/util/conversions.rb +23 -0
  18. data/lib/mongo/util/logging.rb +13 -18
  19. data/lib/mongo/util/node.rb +1 -5
  20. data/lib/mongo/util/pool.rb +0 -1
  21. data/lib/mongo/util/ssl_socket.rb +3 -1
  22. data/lib/mongo/util/support.rb +1 -0
  23. data/lib/mongo/util/tcp_socket.rb +15 -32
  24. data/lib/mongo/util/uri_parser.rb +100 -35
  25. data/lib/mongo/version.rb +1 -1
  26. data/test/auxillary/1.4_features.rb +1 -1
  27. data/test/auxillary/authentication_test.rb +1 -1
  28. data/test/auxillary/autoreconnect_test.rb +1 -1
  29. data/test/auxillary/fork_test.rb +1 -1
  30. data/test/auxillary/repl_set_auth_test.rb +1 -1
  31. data/test/auxillary/slave_connection_test.rb +1 -1
  32. data/test/auxillary/threaded_authentication_test.rb +1 -1
  33. data/test/bson/binary_test.rb +1 -1
  34. data/test/bson/bson_test.rb +8 -1
  35. data/test/bson/byte_buffer_test.rb +1 -1
  36. data/test/bson/hash_with_indifferent_access_test.rb +1 -1
  37. data/test/bson/json_test.rb +1 -1
  38. data/test/bson/object_id_test.rb +11 -1
  39. data/test/bson/ordered_hash_test.rb +1 -1
  40. data/test/bson/timestamp_test.rb +1 -1
  41. data/test/collection_test.rb +1 -1
  42. data/test/connection_test.rb +25 -1
  43. data/test/conversions_test.rb +1 -1
  44. data/test/cursor_fail_test.rb +1 -1
  45. data/test/cursor_message_test.rb +1 -1
  46. data/test/cursor_test.rb +1 -1
  47. data/test/db_api_test.rb +57 -3
  48. data/test/db_connection_test.rb +1 -1
  49. data/test/db_test.rb +1 -1
  50. data/test/grid_file_system_test.rb +1 -1
  51. data/test/grid_io_test.rb +30 -1
  52. data/test/grid_test.rb +1 -1
  53. data/test/pool_test.rb +1 -1
  54. data/test/replica_sets/basic_test.rb +10 -0
  55. data/test/replica_sets/connect_test.rb +45 -0
  56. data/test/replica_sets/read_preference_test.rb +2 -1
  57. data/test/replica_sets/refresh_with_threads_test.rb +2 -0
  58. data/test/replica_sets/rs_test_helper.rb +1 -1
  59. data/test/safe_test.rb +1 -1
  60. data/test/support_test.rb +1 -1
  61. data/test/threading/threading_with_large_pool_test.rb +1 -1
  62. data/test/threading_test.rb +1 -1
  63. data/test/timeout_test.rb +1 -1
  64. data/test/tools/repl_set_manager.rb +1 -0
  65. data/test/unit/collection_test.rb +1 -1
  66. data/test/unit/connection_test.rb +89 -1
  67. data/test/unit/cursor_test.rb +1 -1
  68. data/test/unit/db_test.rb +1 -1
  69. data/test/unit/grid_test.rb +1 -1
  70. data/test/unit/node_test.rb +1 -1
  71. data/test/unit/pool_manager_test.rb +1 -1
  72. data/test/unit/pool_test.rb +1 -1
  73. data/test/unit/read_test.rb +1 -1
  74. data/test/unit/safe_test.rb +1 -1
  75. data/test/uri_test.rb +25 -5
  76. metadata +5 -5
@@ -1,3 +1,3 @@
1
1
  module Mongo
2
- VERSION = "1.6.2"
2
+ VERSION = "1.6.4"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'mongo'
3
3
  require 'test/unit'
4
- require './test/test_helper'
4
+ require File.expand_path("../../test_helper", __FILE__)
5
5
 
6
6
  # Demonstrate features in MongoDB 1.4
7
7
  class Features14Test < Test::Unit::TestCase
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'mongo'
3
3
  require 'test/unit'
4
- require './test/test_helper'
4
+ require File.expand_path("../../test_helper", __FILE__)
5
5
 
6
6
  # NOTE: This test requires bouncing the server.
7
7
  # It also requires that a user exists on the admin database.
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'mongo'
3
3
  require 'test/unit'
4
- require './test/test_helper'
4
+ require File.expand_path("../../test_helper", __FILE__)
5
5
 
6
6
  # NOTE: This test requires bouncing the server
7
7
  class AutoreconnectTest < Test::Unit::TestCase
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'mongo'
3
3
  require 'test/unit'
4
- require './test/test_helper'
4
+ require File.expand_path("../../test_helper", __FILE__)
5
5
 
6
6
  class ForkTest < Test::Unit::TestCase
7
7
  include Mongo
@@ -1,5 +1,5 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require './test/test_helper'
2
+ require File.expand_path("../../test_helper", __FILE__)
3
3
  require './test/tools/auth_repl_set_manager'
4
4
  require './test/replica_sets/rs_test_helper'
5
5
 
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  # NOTE: these tests are run only if we can connect to a single MongoDB in slave mode.
4
4
  class SlaveConnectionTest < Test::Unit::TestCase
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'mongo'
3
3
  require 'thread'
4
4
  require 'test/unit'
5
- require './test/test_helper'
5
+ require File.expand_path("../../test_helper", __FILE__)
6
6
 
7
7
  # NOTE: This test requires bouncing the server.
8
8
  # It also requires that a user exists on the admin database.
@@ -1,5 +1,5 @@
1
1
  # encoding:utf-8
2
- require './test/bson/test_helper'
2
+ require File.expand_path("../test_helper", __FILE__)
3
3
 
4
4
  class BinaryTest < Test::Unit::TestCase
5
5
  def setup
@@ -1,5 +1,5 @@
1
1
  # encoding:utf-8
2
- require './test/bson/test_helper'
2
+ require File.expand_path("../test_helper", __FILE__)
3
3
  require 'set'
4
4
 
5
5
  if RUBY_VERSION < '1.9'
@@ -141,6 +141,13 @@ class BSONTest < Test::Unit::TestCase
141
141
  end
142
142
  end
143
143
 
144
+ def test_forced_encoding_with_valid_utf8
145
+ doc = {'doc' => "\xC3\xB6".force_encoding("ISO-8859-1")}
146
+ serialized = @encoder.serialize(doc)
147
+ deserialized = @encoder.deserialize(serialized)
148
+ assert_equal(doc['doc'], deserialized['doc'].force_encoding("ISO-8859-1"))
149
+ end
150
+
144
151
  # Based on a test from sqlite3-ruby
145
152
  def test_default_internal_is_honored
146
153
  before_enc = Encoding.default_internal
@@ -1,5 +1,5 @@
1
1
  # encoding: binary
2
- require './test/bson/test_helper'
2
+ require File.expand_path("../test_helper", __FILE__)
3
3
 
4
4
  class ByteBufferTest < Test::Unit::TestCase
5
5
  include BSON
@@ -1,5 +1,5 @@
1
1
  # encoding:utf-8
2
- require './test/bson/test_helper'
2
+ require File.expand_path("../test_helper", __FILE__)
3
3
  require './test/support/hash_with_indifferent_access'
4
4
 
5
5
  class HashWithIndifferentAccessTest < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require './test/bson/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'rubygems'
3
3
  require 'json'
4
4
 
@@ -1,4 +1,4 @@
1
- require './test/bson/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'rubygems'
3
3
  require 'json'
4
4
 
@@ -135,4 +135,14 @@ class ObjectIdTest < Test::Unit::TestCase
135
135
  id = ObjectId.new
136
136
  assert_equal({"$oid" => id.to_s}, id.as_json)
137
137
  end
138
+
139
+ def test_object_id_array_flatten
140
+ id = ObjectId.new
141
+ assert_equal [ id ], [[ id ]].flatten
142
+ end
143
+
144
+ def test_object_id_array_flatten_bang
145
+ id = ObjectId.new
146
+ assert_equal [ id ], [[ id ]].flatten!
147
+ end
138
148
  end
@@ -1,4 +1,4 @@
1
- require './test/bson/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class OrderedHashTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require './test/bson/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class TimestampTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'rbconfig'
3
3
 
4
4
  class TestCollection < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'logger'
3
3
  require 'stringio'
4
4
  require 'thread'
@@ -58,6 +58,30 @@ class TestConnection < Test::Unit::TestCase
58
58
  assert_equal mongo_port, con.primary_pool.port
59
59
  end
60
60
 
61
+ def test_env_mongodb_uri
62
+ begin
63
+ old_mongodb_uri = ENV['MONGODB_URI']
64
+ ENV['MONGODB_URI'] = "mongodb://#{host_port}"
65
+ con = Connection.new
66
+ assert_equal mongo_host, con.primary_pool.host
67
+ assert_equal mongo_port, con.primary_pool.port
68
+ ensure
69
+ ENV['MONGODB_URI'] = old_mongodb_uri
70
+ end
71
+ end
72
+
73
+ def test_from_uri_implicit_mongodb_uri
74
+ begin
75
+ old_mongodb_uri = ENV['MONGODB_URI']
76
+ ENV['MONGODB_URI'] = "mongodb://#{host_port}"
77
+ con = Connection.from_uri
78
+ assert_equal mongo_host, con.primary_pool.host
79
+ assert_equal mongo_port, con.primary_pool.port
80
+ ensure
81
+ ENV['MONGODB_URI'] = old_mongodb_uri
82
+ end
83
+ end
84
+
61
85
  def test_server_version
62
86
  assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
63
87
  end
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'mongo/exceptions'
3
3
  require 'mongo/util/conversions'
4
4
 
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'logger'
3
3
 
4
4
  class CursorFailTest < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'logger'
3
3
 
4
4
  class CursorMessageTest < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'logger'
3
3
 
4
4
  class CursorTest < Test::Unit::TestCase
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class DBAPITest < Test::Unit::TestCase
4
4
  include Mongo
@@ -183,13 +183,67 @@ class DBAPITest < Test::Unit::TestCase
183
183
  # Sorting using empty array; no order guarantee should not blow up.
184
184
  docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
185
185
  assert_equal 4, docs.size
186
+ end
186
187
 
188
+ def test_find_sorting_with_hash
187
189
  # Sorting using ordered hash. You can use an unordered one, but then the
188
190
  # order of the keys won't be guaranteed thus your sort won't make sense.
191
+
192
+ @@coll.remove
193
+ @@coll.insert('a' => 1, 'b' => 2)
194
+ @@coll.insert('a' => 2, 'b' => 1)
195
+ @@coll.insert('a' => 3, 'b' => 2)
196
+ @@coll.insert('a' => 4, 'b' => 1)
197
+
189
198
  oh = BSON::OrderedHash.new
190
199
  oh['a'] = -1
191
- assert_raise InvalidSortValueError do
192
- docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
200
+
201
+ # Sort as a method
202
+ docs = @@coll.find.sort(oh).to_a
203
+ assert_equal 4, docs.size
204
+ assert_equal 4, docs[0]['a']
205
+ assert_equal 3, docs[1]['a']
206
+ assert_equal 2, docs[2]['a']
207
+ assert_equal 1, docs[3]['a']
208
+
209
+ # Sort as an option
210
+ docs = @@coll.find({}, :sort => oh).to_a
211
+ assert_equal 4, docs.size
212
+ assert_equal 4, docs[0]['a']
213
+ assert_equal 3, docs[1]['a']
214
+ assert_equal 2, docs[2]['a']
215
+ assert_equal 1, docs[3]['a']
216
+
217
+ if RUBY_VERSION > '1.9'
218
+ docs = @@coll.find({}, :sort => {:a => -1}).to_a
219
+ assert_equal 4, docs.size
220
+ assert_equal 4, docs[0]['a']
221
+ assert_equal 3, docs[1]['a']
222
+ assert_equal 2, docs[2]['a']
223
+ assert_equal 1, docs[3]['a']
224
+
225
+ docs = @@coll.find.sort(:a => -1).to_a
226
+ assert_equal 4, docs.size
227
+ assert_equal 4, docs[0]['a']
228
+ assert_equal 3, docs[1]['a']
229
+ assert_equal 2, docs[2]['a']
230
+ assert_equal 1, docs[3]['a']
231
+
232
+ docs = @@coll.find.sort(:b => -1, :a => 1).to_a
233
+ assert_equal 4, docs.size
234
+ assert_equal 1, docs[0]['a']
235
+ assert_equal 3, docs[1]['a']
236
+ assert_equal 2, docs[2]['a']
237
+ assert_equal 4, docs[3]['a']
238
+ else
239
+ # Sort as an option
240
+ assert_raise InvalidSortValueError do
241
+ @@coll.find({}, :sort => {:a => -1}).to_a
242
+ end
243
+ # Sort as a method
244
+ assert_raise InvalidSortValueError do
245
+ @@coll.find.sort(:a => -1).to_a
246
+ end
193
247
  end
194
248
  end
195
249
 
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class DBConnectionTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'digest/md5'
3
3
  require 'stringio'
4
4
  require 'logger'
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class GridFileSystemTest < Test::Unit::TestCase
4
4
  context "GridFileSystem:" do
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
 
3
3
  class GridIOTest < Test::Unit::TestCase
4
4
 
@@ -122,6 +122,35 @@ class GridIOTest < Test::Unit::TestCase
122
122
  end
123
123
  end
124
124
 
125
+ context "Writing" do
126
+ setup do
127
+ @filename = 'test'
128
+ @length = 50000
129
+ @times = 10
130
+ end
131
+
132
+ should "correctly write multiple chunks from mutiple writes" do
133
+ file = GridIO.new(@files, @chunks, @filename, 'w')
134
+
135
+ @times.times do
136
+ file.write("1" * @length)
137
+ end
138
+
139
+ file.close
140
+
141
+ file = GridIO.new(@files, @chunks, @filename, 'r')
142
+
143
+ total_size = 0
144
+ while !file.eof?
145
+ total_size += file.read(@length).length
146
+ end
147
+
148
+ file.close
149
+
150
+ assert_equal total_size, @times * @length
151
+ end
152
+ end
153
+
125
154
  context "Seeking" do
126
155
  setup do
127
156
  @filename = 'test'
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  include Mongo
3
3
 
4
4
  def read_and_write_stream(filename, read_length, opts={})
@@ -1,4 +1,4 @@
1
- require './test/test_helper'
1
+ require File.expand_path("../test_helper", __FILE__)
2
2
  require 'thread'
3
3
 
4
4
  class PoolTest < Test::Unit::TestCase
@@ -25,6 +25,16 @@ class BasicTest < Test::Unit::TestCase
25
25
  assert @conn.connected?
26
26
  end
27
27
 
28
+ def test_multiple_concurrent_replica_set_connection
29
+ @conn1 = ReplSetConnection.new(build_seeds(3), :name => @rs.name)
30
+ @conn2 = ReplSetConnection.new(build_seeds(3), :name => @rs.name)
31
+ assert @conn1.connected?
32
+ assert @conn2.connected?
33
+
34
+ assert @conn1.manager != @conn2.manager
35
+ assert @conn1.local_manager != @conn2.local_manager
36
+ end
37
+
28
38
  def test_cache_original_seed_nodes
29
39
  seeds = build_seeds(3) << "#{@rs.host}:19356"
30
40
  @conn = ReplSetConnection.new(seeds, :name => @rs.name)
@@ -105,6 +105,34 @@ class ConnectTest < Test::Unit::TestCase
105
105
  assert @conn.is_a?(ReplSetConnection)
106
106
  assert @conn.connected?
107
107
  end
108
+
109
+ def test_connect_with_connection_string_in_env_var
110
+ begin
111
+ old_mongodb_uri = ENV['MONGODB_URI']
112
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
113
+ silently do
114
+ @conn = ReplSetConnection.new
115
+ end
116
+ assert @conn.is_a?(ReplSetConnection)
117
+ assert @conn.connected?
118
+ ensure
119
+ ENV['MONGODB_URI'] = old_mongodb_uri
120
+ end
121
+ end
122
+
123
+ def test_connect_with_connection_string_in_implicit_mongodb_uri
124
+ begin
125
+ old_mongodb_uri = ENV['MONGODB_URI']
126
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
127
+ silently do
128
+ @conn = Connection.from_uri
129
+ end
130
+ assert @conn.is_a?(ReplSetConnection)
131
+ assert @conn.connected?
132
+ ensure
133
+ ENV['MONGODB_URI'] = old_mongodb_uri
134
+ end
135
+ end
108
136
 
109
137
  def test_connect_with_new_seed_format
110
138
  @conn = ReplSetConnection.new build_seeds(3)
@@ -128,4 +156,21 @@ class ConnectTest < Test::Unit::TestCase
128
156
  assert @conn.safe[:fsync]
129
157
  assert @conn.read_pool
130
158
  end
159
+
160
+ def test_connect_with_full_connection_string_in_env_var
161
+ begin
162
+ old_mongodb_uri = ENV['MONGODB_URI']
163
+ ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name};safe=true;w=2;fsync=true;slaveok=true"
164
+ silently do
165
+ @conn = ReplSetConnection.new
166
+ end
167
+ assert @conn.is_a?(ReplSetConnection)
168
+ assert @conn.connected?
169
+ assert_equal 2, @conn.safe[:w]
170
+ assert @conn.safe[:fsync]
171
+ assert @conn.read_pool
172
+ ensure
173
+ ENV['MONGODB_URI'] = old_mongodb_uri
174
+ end
175
+ end
131
176
  end