mongo 1.1.5 → 1.3.0

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 +15 -15
  2. data/Rakefile +38 -17
  3. data/docs/FAQ.md +4 -0
  4. data/docs/HISTORY.md +59 -0
  5. data/docs/RELEASES.md +33 -0
  6. data/docs/REPLICA_SETS.md +13 -16
  7. data/lib/mongo/collection.rb +157 -69
  8. data/lib/mongo/connection.rb +189 -65
  9. data/lib/mongo/cursor.rb +43 -29
  10. data/lib/mongo/db.rb +63 -43
  11. data/lib/mongo/exceptions.rb +4 -1
  12. data/lib/mongo/gridfs/grid.rb +1 -1
  13. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  14. data/lib/mongo/gridfs/grid_file_system.rb +1 -1
  15. data/lib/mongo/gridfs/grid_io.rb +89 -8
  16. data/lib/mongo/gridfs/grid_io_fix.rb +1 -1
  17. data/lib/mongo/repl_set_connection.rb +72 -20
  18. data/lib/mongo/test.rb +20 -0
  19. data/lib/mongo/util/conversions.rb +1 -1
  20. data/lib/mongo/util/core_ext.rb +11 -1
  21. data/lib/mongo/util/pool.rb +67 -15
  22. data/lib/mongo/util/server_version.rb +1 -1
  23. data/lib/mongo/util/support.rb +1 -1
  24. data/lib/mongo/util/uri_parser.rb +127 -13
  25. data/lib/mongo.rb +38 -2
  26. data/test/async/collection_test.rb +224 -0
  27. data/test/async/connection_test.rb +24 -0
  28. data/test/async/cursor_test.rb +162 -0
  29. data/test/async/worker_pool_test.rb +99 -0
  30. data/test/auxillary/fork_test.rb +30 -0
  31. data/test/auxillary/repl_set_auth_test.rb +58 -0
  32. data/test/auxillary/threaded_authentication_test.rb +101 -0
  33. data/test/bson/bson_test.rb +140 -28
  34. data/test/bson/byte_buffer_test.rb +18 -0
  35. data/test/bson/object_id_test.rb +14 -1
  36. data/test/bson/ordered_hash_test.rb +7 -0
  37. data/test/bson/timestamp_test.rb +24 -0
  38. data/test/collection_test.rb +104 -15
  39. data/test/connection_test.rb +78 -2
  40. data/test/conversions_test.rb +10 -11
  41. data/test/cursor_fail_test.rb +1 -1
  42. data/test/cursor_message_test.rb +1 -1
  43. data/test/cursor_test.rb +33 -4
  44. data/test/db_api_test.rb +30 -52
  45. data/test/db_test.rb +3 -3
  46. data/test/grid_file_system_test.rb +0 -1
  47. data/test/grid_io_test.rb +72 -1
  48. data/test/grid_test.rb +16 -16
  49. data/test/load/resque/load.rb +21 -0
  50. data/test/load/resque/processor.rb +26 -0
  51. data/test/load/thin/load.rb +24 -0
  52. data/test/load/unicorn/load.rb +23 -0
  53. data/test/load/unicorn/unicorn.rb +29 -0
  54. data/test/replica_sets/connect_test.rb +11 -1
  55. data/test/replica_sets/connection_string_test.rb +32 -0
  56. data/test/replica_sets/query_secondaries.rb +16 -0
  57. data/test/replica_sets/query_test.rb +10 -0
  58. data/test/replica_sets/replication_ack_test.rb +2 -0
  59. data/test/replica_sets/rs_test_helper.rb +9 -11
  60. data/test/support/hash_with_indifferent_access.rb +0 -13
  61. data/test/support_test.rb +0 -1
  62. data/test/test_helper.rb +27 -8
  63. data/test/tools/auth_repl_set_manager.rb +14 -0
  64. data/test/tools/load.rb +58 -0
  65. data/test/tools/repl_set_manager.rb +34 -9
  66. data/test/tools/sharding_manager.rb +202 -0
  67. data/test/tools/test.rb +3 -12
  68. data/test/unit/collection_test.rb +20 -24
  69. data/test/unit/connection_test.rb +4 -18
  70. data/test/unit/cursor_test.rb +16 -6
  71. data/test/unit/db_test.rb +10 -11
  72. data/test/unit/repl_set_connection_test.rb +0 -23
  73. data/test/unit/safe_test.rb +3 -3
  74. data/test/uri_test.rb +91 -0
  75. metadata +49 -12
  76. data/docs/1.0_UPGRADE.md +0 -21
@@ -22,12 +22,22 @@ class TestConnection < Test::Unit::TestCase
22
22
  end
23
23
  end
24
24
 
25
+ def test_host_port_accessors
26
+ assert_equal @conn.host, TEST_HOST
27
+ assert_equal @conn.port, TEST_PORT
28
+ end
29
+
25
30
  def test_server_info
26
31
  server_info = @conn.server_info
27
32
  assert server_info.keys.include?("version")
28
33
  assert Mongo::Support.ok?(server_info)
29
34
  end
30
35
 
36
+ def test_ping
37
+ ping = @conn.ping
38
+ assert ping['ok']
39
+ end
40
+
31
41
  def test_connection_uri
32
42
  con = Connection.from_uri("mongodb://#{host_port}")
33
43
  assert_equal mongo_host, con.primary_pool.host
@@ -35,7 +45,7 @@ class TestConnection < Test::Unit::TestCase
35
45
  end
36
46
 
37
47
  def test_server_version
38
- assert_match /\d\.\d+(\.\d+)?/, @conn.server_version.to_s
48
+ assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
39
49
  end
40
50
 
41
51
  def test_invalid_database_names
@@ -149,7 +159,7 @@ class TestConnection < Test::Unit::TestCase
149
159
  @conn.lock!
150
160
  assert @conn.locked?
151
161
  assert_equal 1, @conn['admin']['$cmd.sys.inprog'].find_one['fsyncLock'], "Not fsync-locked"
152
- assert_equal "unlock requested", @conn.unlock!['info']
162
+ assert_match(/unlock/, @conn.unlock!['info'])
153
163
  unlocked = false
154
164
  counter = 0
155
165
  while counter < 5
@@ -165,6 +175,62 @@ class TestConnection < Test::Unit::TestCase
165
175
  assert unlocked, "mongod failed to unlock"
166
176
  end
167
177
 
178
+ def test_max_bson_size_value
179
+ conn = standard_connection
180
+ if conn.server_version > "1.7.2"
181
+ assert_equal conn['admin'].command({:ismaster => 1})['maxBsonObjectSize'], conn.max_bson_size
182
+ end
183
+
184
+ conn.connect
185
+ assert_equal BSON::BSON_CODER.max_bson_size, conn.max_bson_size
186
+ doc = {'n' => 'a' * (BSON_CODER.max_bson_size - 11)}
187
+ assert_raise InvalidDocument do
188
+ assert BSON::BSON_CODER.serialize(doc)
189
+ end
190
+
191
+ limit = 7 * 1024 * 1024
192
+ conn.stubs(:max_bson_size).returns(limit)
193
+ conn.connect
194
+ assert_equal limit, conn.max_bson_size
195
+ assert_equal limit, BSON::BSON_CODER.max_bson_size
196
+ doc = {'n' => 'a' * ((limit) - 11)}
197
+ assert_raise_error InvalidDocument, "limited to #{limit}" do
198
+ assert BSON::BSON_CODER.serialize(doc)
199
+ end
200
+ end
201
+
202
+ def test_max_bson_size_with_old_mongod
203
+ conn = standard_connection(:connect => false)
204
+
205
+ admin_db = Object.new
206
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}).twice
207
+ conn.expects(:[]).with('admin').returns(admin_db).twice
208
+
209
+ conn.connect
210
+ assert_equal Mongo::DEFAULT_MAX_BSON_SIZE, BSON::BSON_CODER.max_bson_size
211
+ end
212
+
213
+ def test_connection_activity
214
+ conn = standard_connection
215
+ assert conn.active?
216
+
217
+ conn.primary_pool.close
218
+ assert !conn.active?
219
+
220
+ # Simulate a dropped connection.
221
+ dropped_socket = Mocha::Mock.new
222
+ dropped_socket.stubs(:read).raises(Errno::ECONNRESET)
223
+ dropped_socket.stubs(:send).raises(Errno::ECONNRESET)
224
+ dropped_socket.stub_everything
225
+
226
+ conn.primary_pool.host = 'localhost'
227
+ conn.primary_pool.port = Mongo::Connection::DEFAULT_PORT
228
+ conn.primary_pool.instance_variable_set("@pids", {dropped_socket => Process.pid})
229
+ conn.primary_pool.instance_variable_set("@sockets", [dropped_socket])
230
+
231
+ assert !conn.active?
232
+ end
233
+
168
234
  context "Saved authentications" do
169
235
  setup do
170
236
  @conn = standard_connection
@@ -229,5 +295,15 @@ class TestConnection < Test::Unit::TestCase
229
295
  end
230
296
  assert_equal 0, @con.primary_pool.checked_out.size
231
297
  end
298
+
299
+ should "show a proper exception message if an IOError is raised while closing a socket" do
300
+ fake_socket = Mocha::Mock.new
301
+ fake_socket.stubs(:close).raises(IOError.new)
302
+ fake_socket.stub_everything
303
+ TCPSocket.expects(:new).returns(fake_socket)
304
+
305
+ @con.primary_pool.checkout_new_socket
306
+ assert_equal [], @con.primary_pool.close
307
+ end
232
308
  end
233
309
  end
@@ -1,7 +1,6 @@
1
1
  require './test/test_helper'
2
2
  require 'mongo/exceptions'
3
3
  require 'mongo/util/conversions'
4
- require 'bson/ordered_hash'
5
4
 
6
5
  class ConversionsTest < Test::Unit::TestCase
7
6
  include Mongo::Conversions
@@ -40,11 +39,11 @@ class ConversionsTest < Test::Unit::TestCase
40
39
  end
41
40
 
42
41
  def test_sort_value_when_value_is_negative_one
43
- assert_equal -1, sort_value(-1)
42
+ assert_equal(-1, sort_value(-1))
44
43
  end
45
44
 
46
45
  def test_sort_value_when_value_is_negative_one_as_a_string
47
- assert_equal -1, sort_value("-1")
46
+ assert_equal(-1, sort_value("-1"))
48
47
  end
49
48
 
50
49
  def test_sort_value_when_value_is_ascending
@@ -80,35 +79,35 @@ class ConversionsTest < Test::Unit::TestCase
80
79
  end
81
80
 
82
81
  def test_sort_value_when_value_is_descending
83
- assert_equal -1, sort_value("descending")
82
+ assert_equal(-1, sort_value("descending"))
84
83
  end
85
84
 
86
85
  def test_sort_value_when_value_is_desc
87
- assert_equal -1, sort_value("desc")
86
+ assert_equal(-1, sort_value("desc"))
88
87
  end
89
88
 
90
89
  def test_sort_value_when_value_is_uppercase_descending
91
- assert_equal -1, sort_value("DESCENDING")
90
+ assert_equal(-1, sort_value("DESCENDING"))
92
91
  end
93
92
 
94
93
  def test_sort_value_when_value_is_uppercase_desc
95
- assert_equal -1, sort_value("DESC")
94
+ assert_equal(-1, sort_value("DESC"))
96
95
  end
97
96
 
98
97
  def test_sort_value_when_value_is_symbol_descending
99
- assert_equal -1, sort_value(:descending)
98
+ assert_equal(-1, sort_value(:descending))
100
99
  end
101
100
 
102
101
  def test_sort_value_when_value_is_symbol_desc
103
- assert_equal -1, sort_value(:desc)
102
+ assert_equal(-1, sort_value(:desc))
104
103
  end
105
104
 
106
105
  def test_sort_value_when_value_is_uppercase_symbol_descending
107
- assert_equal -1, sort_value(:DESCENDING)
106
+ assert_equal(-1, sort_value(:DESCENDING))
108
107
  end
109
108
 
110
109
  def test_sort_value_when_value_is_uppercase_symbol_desc
111
- assert_equal -1, sort_value(:DESC)
110
+ assert_equal(-1, sort_value(:DESC))
112
111
  end
113
112
 
114
113
  def test_sort_value_when_value_is_invalid
@@ -1,7 +1,7 @@
1
1
  require './test/test_helper'
2
2
  require 'logger'
3
3
 
4
- class CursorTest < Test::Unit::TestCase
4
+ class CursorFailTest < Test::Unit::TestCase
5
5
 
6
6
  include Mongo
7
7
 
@@ -1,7 +1,7 @@
1
1
  require './test/test_helper'
2
2
  require 'logger'
3
3
 
4
- class CursorTest < Test::Unit::TestCase
4
+ class CursorMessageTest < Test::Unit::TestCase
5
5
 
6
6
  include Mongo
7
7
 
data/test/cursor_test.rb CHANGED
@@ -120,7 +120,7 @@ class CursorTest < Test::Unit::TestCase
120
120
  results = @@coll.find.sort([:n, :asc]).to_a
121
121
 
122
122
  assert_equal MinKey.new, results[0]['n']
123
- assert_equal -1000000, results[1]['n']
123
+ assert_equal(-1000000, results[1]['n'])
124
124
  assert_equal 1000000, results[2]['n']
125
125
  assert_equal MaxKey.new, results[3]['n']
126
126
  end
@@ -171,8 +171,8 @@ class CursorTest < Test::Unit::TestCase
171
171
  cursor = Cursor.new(@@coll, :timeout => false)
172
172
  assert_equal false, cursor.timeout
173
173
 
174
- @@coll.find({}, :timeout => false) do |cursor|
175
- assert_equal false, cursor.timeout
174
+ @@coll.find({}, :timeout => false) do |c|
175
+ assert_equal false, c.timeout
176
176
  end
177
177
  end
178
178
 
@@ -395,10 +395,13 @@ class CursorTest < Test::Unit::TestCase
395
395
  end
396
396
 
397
397
  cursor = @@coll.find
398
+ n = 0
398
399
  while cursor.has_next?
399
- assert cursor.next_document
400
+ assert cursor.next
401
+ n += 1
400
402
  end
401
403
 
404
+ assert_equal n, 200
402
405
  assert_equal false, cursor.has_next?
403
406
  end
404
407
 
@@ -451,4 +454,30 @@ class CursorTest < Test::Unit::TestCase
451
454
  assert_equal 100, cursor.map {|doc| doc }.length
452
455
  end
453
456
 
457
+ def test_transformer
458
+ transformer = Proc.new { |doc| doc }
459
+ cursor = Cursor.new(@@coll, :transformer => transformer)
460
+ assert_equal(transformer, cursor.transformer)
454
461
  end
462
+
463
+ def test_instance_transformation_with_next
464
+ klass = Struct.new(:id, :a)
465
+ transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
466
+ cursor = Cursor.new(@@coll, :transformer => transformer)
467
+ instance = cursor.next
468
+
469
+ assert_instance_of(klass, instance)
470
+ assert_instance_of(BSON::ObjectId, instance.id)
471
+ assert_equal(1, instance.a)
472
+ end
473
+
474
+ def test_instance_transformation_with_each
475
+ klass = Struct.new(:id, :a)
476
+ transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
477
+ cursor = Cursor.new(@@coll, :transformer => transformer)
478
+
479
+ cursor.each do |instance|
480
+ assert_instance_of(klass, instance)
481
+ end
482
+ end
483
+ end
data/test/db_api_test.rb CHANGED
@@ -239,7 +239,7 @@ class DBAPITest < Test::Unit::TestCase
239
239
  names = @@db.collection_names
240
240
  assert names.length >= 2
241
241
  assert names.include?(@@coll.name)
242
- assert names.include?('mongo-ruby-test.test2')
242
+ assert names.include?('test2')
243
243
  ensure
244
244
  @@db.drop_collection('test2')
245
245
  end
@@ -270,6 +270,17 @@ class DBAPITest < Test::Unit::TestCase
270
270
  end
271
271
  end
272
272
 
273
+ def test_collection_options_are_passed_to_the_existing_ones
274
+ @@db.drop_collection('foobar')
275
+
276
+ @@db.create_collection('foobar')
277
+
278
+ opts = {:safe => true}
279
+ coll = @@db.create_collection('foobar', opts)
280
+ assert_equal true, coll.safe
281
+ end
282
+
283
+
273
284
  def test_index_information
274
285
  assert_equal @@coll.index_information.length, 1
275
286
 
@@ -509,33 +520,37 @@ class DBAPITest < Test::Unit::TestCase
509
520
  @@db.drop_collection("test")
510
521
  test = @@db.collection("test")
511
522
 
512
- assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
513
- assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
523
+ assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
524
+ assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
514
525
 
515
526
  test.insert("a" => 2)
516
527
  test.insert("b" => 5)
517
528
  test.insert("a" => 1)
518
529
 
519
- assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
520
- assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
521
- assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
522
- assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
530
+ assert_equal 3, test.group(:initial => {"count" => 0},
531
+ :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
532
+ assert_equal 3, test.group(:initial => {"count" => 0},
533
+ :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
534
+ assert_equal 1, test.group(:cond => {"a" => {"$gt" => 1}},
535
+ :initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
536
+ assert_equal 1, test.group(:cond => {"a" => {"$gt" => 1}},
537
+ :initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")[0]["count"]
523
538
 
524
539
  finalize = "function (obj) { obj.f = obj.count - 1; }"
525
- assert_equal 2, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", finalize)[0]["f"]
540
+ assert_equal 2, test.group(:initial => {"count" => 0},
541
+ :reduce => "function (obj, prev) { prev.count++; }", :finalize => finalize)[0]["f"]
526
542
 
527
543
  test.insert("a" => 2, "b" => 3)
528
544
  expected = [{"a" => 2, "count" => 2},
529
545
  {"a" => nil, "count" => 1},
530
546
  {"a" => 1, "count" => 1}]
531
- assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
532
- assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)
547
+ assert_equal expected, test.group(:key => ["a"], :initial => {"count" => 0},
548
+ :reduce => "function (obj, prev) { prev.count++; }")
549
+ assert_equal expected, test.group(:key => :a, :initial => {"count" => 0},
550
+ :reduce => "function (obj, prev) { prev.count++; }")
533
551
 
534
552
  assert_raise OperationFailure do
535
- test.group([], {}, {}, "5 ++ 5")
536
- end
537
- assert_raise OperationFailure do
538
- test.group([], {}, {}, "5 ++ 5", true)
553
+ test.group(:initial => {}, :reduce => "5 ++ 5")
539
554
  end
540
555
  end
541
556
 
@@ -617,44 +632,7 @@ class DBAPITest < Test::Unit::TestCase
617
632
  assert_equal("mike", @@coll.find_one()["hello"])
618
633
  end
619
634
 
620
- def test_invalid_key_names
621
- @@coll.remove
622
-
623
- @@coll.insert({"hello" => "world"})
624
- @@coll.insert({"hello" => {"hello" => "world"}})
625
-
626
- assert_raise BSON::InvalidKeyName do
627
- @@coll.insert({"$hello" => "world"})
628
- end
629
-
630
- assert_raise BSON::InvalidKeyName do
631
- @@coll.insert({"hello" => {"$hello" => "world"}})
632
- end
633
-
634
- @@coll.insert({"he$llo" => "world"})
635
- @@coll.insert({"hello" => {"hell$o" => "world"}})
636
-
637
- assert_raise BSON::InvalidKeyName do
638
- @@coll.insert({".hello" => "world"})
639
- end
640
- assert_raise BSON::InvalidKeyName do
641
- @@coll.insert({"hello" => {".hello" => "world"}})
642
- end
643
- assert_raise BSON::InvalidKeyName do
644
- @@coll.insert({"hello." => "world"})
645
- end
646
- assert_raise BSON::InvalidKeyName do
647
- @@coll.insert({"hello" => {"hello." => "world"}})
648
- end
649
- assert_raise BSON::InvalidKeyName do
650
- @@coll.insert({"hel.lo" => "world"})
651
- end
652
- assert_raise BSON::InvalidKeyName do
653
- @@coll.insert({"hello" => {"hel.lo" => "world"}})
654
- end
655
- end
656
-
657
- def test_collection_names
635
+ def test_collection_names_errors
658
636
  assert_raise TypeError do
659
637
  @@db.collection(5)
660
638
  end
data/test/db_test.rb CHANGED
@@ -26,7 +26,7 @@ class DBTest < Test::Unit::TestCase
26
26
  @@db.collection('test').insert('a' => 1)
27
27
  fail "expected 'NilClass' exception"
28
28
  rescue => ex
29
- assert_match /NilClass/, ex.to_s
29
+ assert_match(/NilClass/, ex.to_s)
30
30
  ensure
31
31
  @@db = standard_connection.db(MONGO_TEST_DB)
32
32
  @@users = @@db.collection('system.users')
@@ -104,7 +104,7 @@ class DBTest < Test::Unit::TestCase
104
104
  db.pk_factory = Object.new
105
105
  fail "error: expected exception"
106
106
  rescue => ex
107
- assert_match /Cannot change/, ex.to_s
107
+ assert_match(/Cannot change/, ex.to_s)
108
108
  ensure
109
109
  conn.close
110
110
  end
@@ -280,7 +280,7 @@ class DBTest < Test::Unit::TestCase
280
280
  assert_not_nil doc
281
281
  result = doc['result']
282
282
  assert_not_nil result
283
- assert_match /firstExtent/, result
283
+ assert_match(/firstExtent/, result)
284
284
  end
285
285
 
286
286
  end
@@ -1,5 +1,4 @@
1
1
  require './test/test_helper'
2
- include Mongo
3
2
 
4
3
  class GridFileSystemTest < Test::Unit::TestCase
5
4
  context "GridFileSystem:" do
data/test/grid_io_test.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require './test/test_helper'
2
- include Mongo
3
2
 
4
3
  class GridIOTest < Test::Unit::TestCase
5
4
 
@@ -33,6 +32,78 @@ class GridIOTest < Test::Unit::TestCase
33
32
  end
34
33
  end
35
34
 
35
+ context "StringIO methods" do
36
+ setup do
37
+ @filename = 'test'
38
+ @mode = 'w'
39
+ @data = "012345678\n" * 100000
40
+ @file = GridIO.new(@files, @chunks, @filename, @mode)
41
+ @file.write(@data)
42
+ @file.close
43
+ end
44
+
45
+ should "read data character by character using" do
46
+ bytes = 0
47
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
48
+ while char = file.getc
49
+ bytes += 1
50
+ end
51
+ assert_equal bytes, 1_000_000
52
+ end
53
+
54
+ should "read length is a length is given" do
55
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
56
+ string = file.gets(1000)
57
+ assert_equal string.length, 1000
58
+ bytes = 0
59
+ bytes += string.length
60
+ while string = file.gets(1000)
61
+ bytes += string.length
62
+ end
63
+ assert_equal bytes, 1_000_000
64
+ end
65
+
66
+ should "read to the end of the line by default and assign to $_" do
67
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
68
+ string = file.gets
69
+ assert_equal 10, string.length
70
+ end
71
+
72
+ should "read to a given separator" do
73
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
74
+ string = file.gets("5")
75
+ assert_equal 6, string.length
76
+ end
77
+
78
+ should "read a multi-character separator" do
79
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
80
+ string = file.gets("45")
81
+ assert_equal 6, string.length
82
+ string = file.gets("45")
83
+ assert_equal "678\n012345", string
84
+ string = file.gets("\n01")
85
+ assert_equal "678\n01", string
86
+ end
87
+
88
+ should "read a mult-character separator with a length" do
89
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
90
+ string = file.gets("45", 3)
91
+ assert_equal 3, string.length
92
+ end
93
+
94
+ should "tell position, eof, and rewind" do
95
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
96
+ string = file.read(1000)
97
+ assert_equal 1000, file.pos
98
+ assert !file.eof?
99
+ file.read
100
+ assert file.eof?
101
+ file.rewind
102
+ assert_equal 0, file.pos
103
+ assert_equal 1_000_000, file.read.length
104
+ end
105
+ end
106
+
36
107
  context "Seeking" do
37
108
  setup do
38
109
  @filename = 'test'
data/test/grid_test.rb CHANGED
@@ -1,6 +1,22 @@
1
1
  require './test/test_helper'
2
2
  include Mongo
3
3
 
4
+ def read_and_write_stream(filename, read_length, opts={})
5
+ io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
6
+ id = @grid.put(io, opts.merge!(:filename => filename + read_length.to_s))
7
+ file = @grid.get(id)
8
+ io.rewind
9
+ data = io.read
10
+ if data.respond_to?(:force_encoding)
11
+ data.force_encoding("binary")
12
+ end
13
+ read_data = ""
14
+ while(chunk = file.read(read_length))
15
+ read_data << chunk
16
+ end
17
+ assert_equal data.length, read_data.length
18
+ end
19
+
4
20
  class GridTest < Test::Unit::TestCase
5
21
  context "Tests:" do
6
22
  setup do
@@ -161,22 +177,6 @@ class GridTest < Test::Unit::TestCase
161
177
 
162
178
  context "Streaming: " do || {}
163
179
  setup do
164
- def read_and_write_stream(filename, read_length, opts={})
165
- io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
166
- id = @grid.put(io, opts.merge!(:filename => filename + read_length.to_s))
167
- file = @grid.get(id)
168
- io.rewind
169
- data = io.read
170
- if data.respond_to?(:force_encoding)
171
- data.force_encoding("binary")
172
- end
173
- read_data = ""
174
- while(chunk = file.read(read_length))
175
- read_data << chunk
176
- end
177
- assert_equal data.length, read_data.length
178
- end
179
-
180
180
  @grid = Grid.new(@db, 'test-fs')
181
181
  end
182
182
 
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
2
+ require 'logger'
3
+ require 'rubygems'
4
+ require 'resque'
5
+ require 'sinatra'
6
+ require File.join(File.dirname(__FILE__), 'processor')
7
+
8
+ $con = Mongo::Connection.new
9
+ $db = $con['foo']
10
+
11
+
12
+ configure do
13
+ LOGGER = Logger.new("sinatra.log")
14
+ enable :logging, :dump_errors
15
+ set :raise_errors, true
16
+ end
17
+
18
+ get '/' do
19
+ Processor.perform(1)
20
+ true
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'logger'
2
+
3
+ class Processor
4
+ @queue = :processor
5
+
6
+ def self.connection
7
+ @log ||= Logger.new(STDOUT)
8
+ @con ||= Mongo::Connection.new("localhost", 27017)
9
+ end
10
+
11
+ def self.perform(n)
12
+ begin
13
+ 100.times do |n|
14
+ self.connection['resque']['docs'].insert({:n => n, :data => "0" * 1000}, :safe => true)
15
+ end
16
+
17
+ 5.times do |n|
18
+ num = rand(100)
19
+ self.connection['resque']['docs'].find({:n => {"$gt" => num}}).limit(1).to_a
20
+ end
21
+ rescue => e
22
+ @log.warn(e.inspect)
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
2
+ require 'logger'
3
+
4
+ $con = Mongo::Connection.new
5
+ $db = $con['foo']
6
+
7
+ class Load < Sinatra::Base
8
+
9
+ configure do
10
+ LOGGER = Logger.new("sinatra.log")
11
+ enable :logging, :dump_errors
12
+ set :raise_errors, true
13
+ end
14
+
15
+ get '/' do
16
+ 3.times do |n|
17
+ if (v=$db.eval("1 + #{n}")) != 1 + n
18
+ STDERR << "#{1 + n} expected but got #{v}"
19
+ raise StandardError, "#{1 + n} expected but got #{v}"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')
2
+
3
+ $con = Mongo::Connection.new
4
+ $db = $con['foo']
5
+
6
+ class Load < Sinatra::Base
7
+
8
+ configure do
9
+ LOGGER = Logger.new("sinatra.log")
10
+ enable :logging, :dump_errors
11
+ set :raise_errors, true
12
+ end
13
+
14
+ get '/' do
15
+ 3.times do |n|
16
+ if (v=$db.eval("1 + #{n}")) != 1 + n
17
+ STDERR << "#{1 + n} expected but got #{v}"
18
+ raise StandardError, "#{1 + n} expected but got #{v}"
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,29 @@
1
+ # set path to app that will be used to configure unicorn,
2
+ # # note the trailing slash in this example
3
+ @dir = "/home/kyle/work/10gen/ruby-driver/test/load/"
4
+
5
+ worker_processes 10
6
+ working_directory @dir
7
+
8
+ preload_app true
9
+
10
+ timeout 30
11
+
12
+ # Specify path to socket unicorn listens to,
13
+ # we will use this in our nginx.conf later
14
+ listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
15
+
16
+ # Set process id path
17
+ pid "#{@dir}tmp/pids/unicorn.pid"
18
+
19
+ # # Set log file paths
20
+ stderr_path "#{@dir}log/unicorn.stderr.log"
21
+ stdout_path "#{@dir}log/unicorn.stdout.log"
22
+
23
+ # NOTE: You need this when using forking web servers!
24
+ after_fork do |server, worker|
25
+ $con.close if $con
26
+ $con = Mongo::Connection.new
27
+ $db = $con['foo']
28
+ STDERR << "FORKED #{server} #{worker}"
29
+ end