mongo 1.0 → 1.1.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 (95) hide show
  1. data/LICENSE.txt +1 -13
  2. data/{README.rdoc → README.md} +129 -149
  3. data/Rakefile +94 -58
  4. data/bin/mongo_console +21 -0
  5. data/docs/1.0_UPGRADE.md +21 -0
  6. data/docs/CREDITS.md +123 -0
  7. data/docs/FAQ.md +112 -0
  8. data/docs/GridFS.md +158 -0
  9. data/docs/HISTORY.md +185 -0
  10. data/docs/REPLICA_SETS.md +75 -0
  11. data/docs/TUTORIAL.md +247 -0
  12. data/docs/WRITE_CONCERN.md +28 -0
  13. data/lib/mongo/collection.rb +225 -105
  14. data/lib/mongo/connection.rb +374 -315
  15. data/lib/mongo/cursor.rb +122 -77
  16. data/lib/mongo/db.rb +109 -85
  17. data/lib/mongo/exceptions.rb +6 -0
  18. data/lib/mongo/gridfs/grid.rb +19 -11
  19. data/lib/mongo/gridfs/grid_ext.rb +36 -9
  20. data/lib/mongo/gridfs/grid_file_system.rb +15 -9
  21. data/lib/mongo/gridfs/grid_io.rb +49 -16
  22. data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
  23. data/lib/mongo/repl_set_connection.rb +290 -0
  24. data/lib/mongo/util/conversions.rb +3 -1
  25. data/lib/mongo/util/core_ext.rb +17 -4
  26. data/lib/mongo/util/pool.rb +125 -0
  27. data/lib/mongo/util/server_version.rb +2 -0
  28. data/lib/mongo/util/support.rb +12 -0
  29. data/lib/mongo/util/uri_parser.rb +71 -0
  30. data/lib/mongo.rb +23 -7
  31. data/{mongo-ruby-driver.gemspec → mongo.gemspec} +9 -7
  32. data/test/auxillary/1.4_features.rb +2 -2
  33. data/test/auxillary/authentication_test.rb +1 -1
  34. data/test/auxillary/autoreconnect_test.rb +1 -1
  35. data/test/{slave_connection_test.rb → auxillary/slave_connection_test.rb} +6 -6
  36. data/test/bson/binary_test.rb +15 -0
  37. data/test/bson/bson_test.rb +537 -0
  38. data/test/bson/byte_buffer_test.rb +190 -0
  39. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  40. data/test/bson/json_test.rb +17 -0
  41. data/test/bson/object_id_test.rb +141 -0
  42. data/test/bson/ordered_hash_test.rb +197 -0
  43. data/test/collection_test.rb +195 -15
  44. data/test/connection_test.rb +93 -56
  45. data/test/conversions_test.rb +1 -1
  46. data/test/cursor_fail_test.rb +75 -0
  47. data/test/cursor_message_test.rb +43 -0
  48. data/test/cursor_test.rb +93 -32
  49. data/test/db_api_test.rb +28 -55
  50. data/test/db_connection_test.rb +2 -3
  51. data/test/db_test.rb +45 -40
  52. data/test/grid_file_system_test.rb +14 -6
  53. data/test/grid_io_test.rb +36 -7
  54. data/test/grid_test.rb +54 -10
  55. data/test/replica_sets/connect_test.rb +84 -0
  56. data/test/replica_sets/count_test.rb +35 -0
  57. data/test/{replica → replica_sets}/insert_test.rb +17 -14
  58. data/test/replica_sets/pooled_insert_test.rb +55 -0
  59. data/test/replica_sets/query_secondaries.rb +80 -0
  60. data/test/replica_sets/query_test.rb +41 -0
  61. data/test/replica_sets/replication_ack_test.rb +64 -0
  62. data/test/replica_sets/rs_test_helper.rb +29 -0
  63. data/test/safe_test.rb +68 -0
  64. data/test/support/hash_with_indifferent_access.rb +199 -0
  65. data/test/support/keys.rb +45 -0
  66. data/test/support_test.rb +19 -0
  67. data/test/test_helper.rb +53 -15
  68. data/test/threading/{test_threading_large_pool.rb → threading_with_large_pool_test.rb} +2 -2
  69. data/test/threading_test.rb +2 -2
  70. data/test/tools/repl_set_manager.rb +241 -0
  71. data/test/tools/test.rb +13 -0
  72. data/test/unit/collection_test.rb +70 -7
  73. data/test/unit/connection_test.rb +18 -39
  74. data/test/unit/cursor_test.rb +7 -8
  75. data/test/unit/db_test.rb +14 -17
  76. data/test/unit/grid_test.rb +49 -0
  77. data/test/unit/pool_test.rb +9 -0
  78. data/test/unit/repl_set_connection_test.rb +82 -0
  79. data/test/unit/safe_test.rb +125 -0
  80. metadata +132 -51
  81. data/bin/bson_benchmark.rb +0 -59
  82. data/bin/fail_if_no_c.rb +0 -11
  83. data/examples/admin.rb +0 -43
  84. data/examples/capped.rb +0 -22
  85. data/examples/cursor.rb +0 -48
  86. data/examples/gridfs.rb +0 -44
  87. data/examples/index_test.rb +0 -126
  88. data/examples/info.rb +0 -31
  89. data/examples/queries.rb +0 -70
  90. data/examples/simple.rb +0 -24
  91. data/examples/strict.rb +0 -35
  92. data/examples/types.rb +0 -36
  93. data/test/replica/count_test.rb +0 -34
  94. data/test/replica/pooled_insert_test.rb +0 -54
  95. data/test/replica/query_test.rb +0 -39
data/test/db_test.rb CHANGED
@@ -1,23 +1,20 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  require 'digest/md5'
3
3
  require 'stringio'
4
4
  require 'logger'
5
5
 
6
6
  class TestPKFactory
7
7
  def create_pk(row)
8
- row['_id'] ||= BSON::ObjectID.new
8
+ row['_id'] ||= BSON::ObjectId.new
9
9
  row
10
10
  end
11
11
  end
12
12
 
13
- # NOTE: assumes Mongo is running
14
13
  class DBTest < Test::Unit::TestCase
15
14
 
16
15
  include Mongo
17
16
 
18
- @@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
19
- @@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
20
- @@conn = Connection.new(@@host, @@port)
17
+ @@conn = standard_connection
21
18
  @@db = @@conn.db(MONGO_TEST_DB)
22
19
  @@users = @@db.collection('system.users')
23
20
  @@version = @@conn.server_version
@@ -31,7 +28,7 @@ class DBTest < Test::Unit::TestCase
31
28
  rescue => ex
32
29
  assert_match /NilClass/, ex.to_s
33
30
  ensure
34
- @@db = Connection.new(@@host, @@port).db(MONGO_TEST_DB)
31
+ @@db = standard_connection.db(MONGO_TEST_DB)
35
32
  @@users = @@db.collection('system.users')
36
33
  end
37
34
  end
@@ -40,7 +37,7 @@ class DBTest < Test::Unit::TestCase
40
37
  output = StringIO.new
41
38
  logger = Logger.new(output)
42
39
  logger.level = Logger::DEBUG
43
- conn = Connection.new(@host, @port, :logger => logger)
40
+ conn = standard_connection(:logger => logger)
44
41
  assert_equal logger, conn.logger
45
42
 
46
43
  conn.logger.debug 'testing'
@@ -76,22 +73,8 @@ class DBTest < Test::Unit::TestCase
76
73
  assert_kind_of Collection, colls[0]
77
74
  end
78
75
 
79
- def test_pair
80
- @@conn.close
81
- @@users = nil
82
- @@conn = Connection.paired([["this-should-fail", 27017], [@@host, @@port]])
83
- @@db = @@conn[MONGO_TEST_DB]
84
- assert @@conn.connected?
85
- ensure
86
- unless @@conn.connected?
87
- @@conn = Connection.new(@@host, @@port)
88
- @@db = @@conn.db(MONGO_TEST_DB)
89
- end
90
- @@users = @@db.collection('system.users')
91
- end
92
-
93
76
  def test_pk_factory
94
- db = Connection.new(@@host, @@port).db(MONGO_TEST_DB, :pk => TestPKFactory.new)
77
+ db = standard_connection.db(MONGO_TEST_DB, :pk => TestPKFactory.new)
95
78
  coll = db.collection('test')
96
79
  coll.remove
97
80
 
@@ -102,7 +85,7 @@ class DBTest < Test::Unit::TestCase
102
85
  assert_not_nil oid
103
86
  assert_equal insert_id, oid
104
87
 
105
- oid = BSON::ObjectID.new
88
+ oid = BSON::ObjectId.new
106
89
  data = {'_id' => oid, 'name' => 'Barney', 'age' => 41}
107
90
  coll.insert(data)
108
91
  row = coll.find_one({'name' => data['name']})
@@ -114,7 +97,7 @@ class DBTest < Test::Unit::TestCase
114
97
  end
115
98
 
116
99
  def test_pk_factory_reset
117
- conn = Connection.new(@@host, @@port)
100
+ conn = standard_connection
118
101
  db = conn.db(MONGO_TEST_DB)
119
102
  db.pk_factory = Object.new # first time
120
103
  begin
@@ -142,10 +125,10 @@ class DBTest < Test::Unit::TestCase
142
125
 
143
126
  def test_authenticate_with_connection_uri
144
127
  @@db.add_user('spongebob', 'squarepants')
145
- assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@localhost/#{@@db.name}")
128
+ assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}")
146
129
 
147
130
  assert_raise Mongo::AuthenticationError do
148
- Mongo::Connection.from_uri("mongodb://wrong:info@localhost/#{@@db.name}")
131
+ Mongo::Connection.from_uri("mongodb://wrong:info@#{host_port}/#{@@db.name}")
149
132
  end
150
133
  end
151
134
 
@@ -153,32 +136,41 @@ class DBTest < Test::Unit::TestCase
153
136
  assert @@db.logout
154
137
  end
155
138
 
139
+ def test_command
140
+ assert_raise OperationFailure do
141
+ @@db.command({:non_command => 1}, :check_response => true)
142
+ end
143
+
144
+ result = @@db.command({:non_command => 1}, :check_response => false)
145
+ assert !Mongo::Support.ok?(result)
146
+ end
147
+
156
148
  def test_error
157
149
  @@db.reset_error_history
158
- assert_nil @@db.error
150
+ assert_nil @@db.get_last_error['err']
159
151
  assert !@@db.error?
160
152
  assert_nil @@db.previous_error
161
153
 
162
- @@db.send(:command, :forceerror => 1)
154
+ @@db.command({:forceerror => 1}, :check_response => false)
163
155
  assert @@db.error?
164
- assert_not_nil @@db.error
156
+ assert_not_nil @@db.get_last_error['err']
165
157
  assert_not_nil @@db.previous_error
166
158
 
167
- @@db.send(:command, :forceerror => 1)
159
+ @@db.command({:forceerror => 1}, :check_response => false)
168
160
  assert @@db.error?
169
- assert @@db.error
161
+ assert @@db.get_last_error['err']
170
162
  prev_error = @@db.previous_error
171
163
  assert_equal 1, prev_error['nPrev']
172
- assert_equal prev_error["err"], @@db.error
164
+ assert_equal prev_error["err"], @@db.get_last_error['err']
173
165
 
174
166
  @@db.collection('test').find_one
175
- assert_nil @@db.error
167
+ assert_nil @@db.get_last_error['err']
176
168
  assert !@@db.error?
177
169
  assert @@db.previous_error
178
170
  assert_equal 2, @@db.previous_error['nPrev']
179
171
 
180
172
  @@db.reset_error_history
181
- assert_nil @@db.error
173
+ assert_nil @@db.get_last_error['err']
182
174
  assert !@@db.error?
183
175
  assert_nil @@db.previous_error
184
176
  end
@@ -186,7 +178,7 @@ class DBTest < Test::Unit::TestCase
186
178
  def test_check_command_response
187
179
  command = {:forceerror => 1}
188
180
  assert_raise OperationFailure do
189
- @@db.command(command, false, true)
181
+ @@db.command(command)
190
182
  end
191
183
  end
192
184
 
@@ -195,16 +187,16 @@ class DBTest < Test::Unit::TestCase
195
187
  @@db['test'].save("i" => 1)
196
188
 
197
189
  @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
198
- assert @@db.last_status()["updatedExisting"]
190
+ assert @@db.get_last_error()["updatedExisting"]
199
191
 
200
192
  @@db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
201
- assert !@@db.last_status()["updatedExisting"]
193
+ assert !@@db.get_last_error()["updatedExisting"]
202
194
  end
203
195
 
204
196
  def test_text_port_number_raises_no_errors
205
- conn = Connection.new(@@host, @@port.to_s)
197
+ conn = standard_connection
206
198
  db = conn[MONGO_TEST_DB]
207
- assert db.collection('users').remove
199
+ db.collection('users').remove
208
200
  end
209
201
 
210
202
  def test_user_management
@@ -221,6 +213,19 @@ class DBTest < Test::Unit::TestCase
221
213
  assert !@@db.remove_user("joe")
222
214
  end
223
215
 
216
+ def test_stored_function_management
217
+ @@db.add_stored_function("sum", "function (x, y) { return x + y; }")
218
+ assert_equal @@db.eval("return sum(2,3);"), 5
219
+ assert @@db.remove_stored_function("sum")
220
+ assert_raise OperationFailure do
221
+ @@db.eval("return sum(2,3);")
222
+ end
223
+ end
224
+
225
+ def test_eval
226
+ @@db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
227
+ assert_equal 'hello', @@db['system'].find_one['_id']
228
+ end
224
229
 
225
230
  if @@version >= "1.3.5"
226
231
  def test_db_stats
@@ -1,17 +1,16 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  include Mongo
3
3
 
4
4
  class GridFileSystemTest < Test::Unit::TestCase
5
5
  context "GridFileSystem:" do
6
6
  setup do
7
- @con = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
8
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
9
- @db = @con.db(MONGO_TEST_DB)
7
+ @con = standard_connection
8
+ @db = @con.db(MONGO_TEST_DB)
10
9
  end
11
10
 
12
11
  teardown do
13
- @db['fs.files'].remove
14
- @db['fs.chunks'].remove
12
+ @db.drop_collection('fs.files')
13
+ @db.drop_collection('fs.chunks')
15
14
  end
16
15
 
17
16
  context "When reading:" do
@@ -25,6 +24,15 @@ class GridFileSystemTest < Test::Unit::TestCase
25
24
  @grid = GridFileSystem.new(@db)
26
25
  end
27
26
 
27
+ should "return existence of the file" do
28
+ file = @grid.exist?(:filename => 'sample.file')
29
+ assert_equal 'sample.file', file['filename']
30
+ end
31
+
32
+ should "return nil if the file doesn't exist" do
33
+ assert_nil @grid.exist?(:filename => 'foo.file')
34
+ end
35
+
28
36
  should "read sample data" do
29
37
  data = @grid.open('sample.file', 'r') { |f| f.read }
30
38
  assert_equal data.length, @chunks_data.length
data/test/grid_io_test.rb CHANGED
@@ -1,12 +1,11 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  include Mongo
3
3
 
4
4
  class GridIOTest < Test::Unit::TestCase
5
5
 
6
6
  context "GridIO" do
7
7
  setup do
8
- @db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
9
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db(MONGO_TEST_DB)
8
+ @db = standard_connection.db(MONGO_TEST_DB)
10
9
  @files = @db.collection('fs.files')
11
10
  @chunks = @db.collection('fs.chunks')
12
11
  @chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]])
@@ -34,6 +33,39 @@ class GridIOTest < Test::Unit::TestCase
34
33
  end
35
34
  end
36
35
 
36
+ context "Seeking" do
37
+ setup do
38
+ @filename = 'test'
39
+ @mode = 'w'
40
+ @data = "1" * 1024 * 1024
41
+ @file = GridIO.new(@files, @chunks, @filename, @mode)
42
+ @file.write(@data)
43
+ @file.close
44
+ end
45
+
46
+ should "read all data using read_length and then be able to seek" do
47
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
48
+ assert_equal @data, file.read(1024 * 1024)
49
+ file.seek(0)
50
+ assert_equal @data, file.read
51
+ end
52
+
53
+ should "read all data using read_all and then be able to seek" do
54
+ file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
55
+ assert_equal @data, file.read
56
+ file.seek(0)
57
+ assert_equal @data, file.read
58
+ file.seek(1024 * 512)
59
+ assert_equal 524288, file.file_position
60
+ assert_equal @data.length / 2, file.read.length
61
+ assert_equal 1048576, file.file_position
62
+ assert_nil file.read
63
+ file.seek(1024 * 512)
64
+ assert_equal 524288, file.file_position
65
+ end
66
+
67
+ end
68
+
37
69
  context "Grid MD5 check" do
38
70
  should "run in safe mode" do
39
71
  file = GridIO.new(@files, @chunks, 'smallfile', 'w', :safe => true)
@@ -52,9 +84,7 @@ class GridIOTest < Test::Unit::TestCase
52
84
 
53
85
  should "raise an exception when check fails" do
54
86
  io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
55
- db = mock()
56
- db.stubs(:command).returns({'md5' => '12345'})
57
- @files.expects(:db).returns(db)
87
+ @db.stubs(:command).returns({'md5' => '12345'})
58
88
  file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
59
89
  file.write(io)
60
90
  assert_raise GridMD5Failure do
@@ -65,7 +95,6 @@ class GridIOTest < Test::Unit::TestCase
65
95
  end
66
96
 
67
97
  context "Content types" do
68
-
69
98
  if defined?(MIME)
70
99
  should "determine common content types from the extension" do
71
100
  file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
data/test/grid_test.rb CHANGED
@@ -1,11 +1,10 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  include Mongo
3
3
 
4
4
  class GridTest < Test::Unit::TestCase
5
5
  context "Tests:" do
6
6
  setup do
7
- @db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
8
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db(MONGO_TEST_DB)
7
+ @db = standard_connection.db(MONGO_TEST_DB)
9
8
  @files = @db.collection('test-fs.files')
10
9
  @chunks = @db.collection('test-fs.chunks')
11
10
  end
@@ -19,7 +18,23 @@ class GridTest < Test::Unit::TestCase
19
18
  setup do
20
19
  @data = "GRIDDATA" * 50000
21
20
  @grid = Grid.new(@db, 'test-fs')
22
- @id = @grid.put(@data, :filename => 'sample', :metadata => {'app' => 'photos'})
21
+ @id = @grid.put(@data, :filename => 'sample',
22
+ :metadata => {'app' => 'photos'})
23
+ end
24
+
25
+ should "check existence" do
26
+ file = @grid.exist?(:filename => 'sample')
27
+ assert_equal 'sample', file['filename']
28
+ end
29
+
30
+ should "not be able to overwrite an exising file" do
31
+ assert_raise GridError do
32
+ @grid.put(@data, :filename => 'sample', :_id => @id, :safe => true)
33
+ end
34
+ end
35
+
36
+ should "return nil if it doesn't exist" do
37
+ assert_nil @grid.exist?(:metadata => 'foo')
23
38
  end
24
39
 
25
40
  should "retrieve the stored data" do
@@ -106,7 +121,8 @@ class GridTest < Test::Unit::TestCase
106
121
  context "Storing data with a length of zero" do
107
122
  setup do
108
123
  @grid = Grid.new(@db, 'test-fs')
109
- @id = @grid.put('', :filename => 'sample', :metadata => {'app' => 'photos'})
124
+ @id = @grid.put('', :filename => 'sample',
125
+ :metadata => {'app' => 'photos'})
110
126
  end
111
127
 
112
128
  should "return the zero length" do
@@ -115,6 +131,34 @@ class GridTest < Test::Unit::TestCase
115
131
  end
116
132
  end
117
133
 
134
+ context "Grid streaming: " do
135
+ setup do
136
+ @grid = Grid.new(@db, 'test-fs')
137
+ filename = 'sample_data'
138
+ @io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
139
+ id = @grid.put(@io, :filename => filename)
140
+ @file = @grid.get(id)
141
+ @io.rewind
142
+ @data = @io.read
143
+ if @data.respond_to?(:force_encoding)
144
+ @data.force_encoding("binary")
145
+ end
146
+ end
147
+
148
+ should "read the file" do
149
+ read_data = ""
150
+ @file.each do |chunk|
151
+ read_data << chunk
152
+ end
153
+ assert_equal @data.length, read_data.length
154
+ end
155
+
156
+ should "read the file if no block is given" do
157
+ read_data = @file.each
158
+ assert_equal @data.length, read_data.length
159
+ end
160
+ end
161
+
118
162
  context "Streaming: " do || {}
119
163
  setup do
120
164
  def read_and_write_stream(filename, read_length, opts={})
@@ -124,7 +168,7 @@ class GridTest < Test::Unit::TestCase
124
168
  io.rewind
125
169
  data = io.read
126
170
  if data.respond_to?(:force_encoding)
127
- data.force_encoding(:binary)
171
+ data.force_encoding("binary")
128
172
  end
129
173
  read_data = ""
130
174
  while(chunk = file.read(read_length))
@@ -144,12 +188,12 @@ class GridTest < Test::Unit::TestCase
144
188
  read_and_write_stream('small_data.txt', 1)
145
189
  end
146
190
 
147
- should "put and get a large io object when reading smaller than the chunk size" do
148
- read_and_write_stream('sample_file.pdf', 256 * 1024)
191
+ 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)
149
193
  end
150
194
 
151
- should "put and get a large io object when reading larger than the chunk size" do
152
- read_and_write_stream('sample_file.pdf', 300 * 1024)
195
+ should "put and get a large io object if reading more than the chunk size" do
196
+ read_and_write_stream('sample_data', 300 * 1024)
153
197
  end
154
198
  end
155
199
  end
@@ -0,0 +1,84 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running on RS.host,
5
+ # on ports TEST_PORT, RS.ports[1], and TEST + 2.
6
+ class ConnectTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ RS.restart_killed_nodes
11
+ end
12
+
13
+ def teardown
14
+ RS.restart_killed_nodes
15
+ end
16
+
17
+ def test_connect_with_deprecated_multi
18
+ @conn = Connection.multi([[RS.host, RS.ports[0]], [RS.host, RS.ports[1]]], :name => RS.name)
19
+ assert @conn.connected?
20
+ assert @conn.is_a?(ReplSetConnection)
21
+ end
22
+
23
+ def test_connect_bad_name
24
+ assert_raise_error(ReplicaSetConnectionError, "-wrong") do
25
+ ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
26
+ [RS.host, RS.ports[2]], :rs_name => RS.name + "-wrong")
27
+ end
28
+ end
29
+
30
+ def test_connect
31
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
32
+ [RS.host, RS.ports[2]], :name => RS.name)
33
+ assert @conn.connected?
34
+
35
+ assert_equal RS.primary, @conn.primary
36
+ assert_equal RS.secondaries.sort, @conn.secondaries.sort
37
+ assert_equal RS.arbiters.sort, @conn.arbiters.sort
38
+ end
39
+
40
+ def test_connect_with_primary_node_killed
41
+ node = RS.kill_primary
42
+
43
+ # Becuase we're killing the primary and trying to connect right away,
44
+ # this is going to fail right away.
45
+ assert_raise_error(ConnectionFailure, "Failed to connect to primary node") do
46
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
47
+ [RS.host, RS.ports[2]])
48
+ end
49
+
50
+ # This allows the secondary to come up as a primary
51
+ rescue_connection_failure do
52
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
53
+ [RS.host, RS.ports[2]])
54
+ end
55
+ assert @conn.connected?
56
+ end
57
+
58
+ def test_connect_with_secondary_node_killed
59
+ node = RS.kill_secondary
60
+
61
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
62
+ [RS.host, RS.ports[2]])
63
+ assert @conn.connected?
64
+ end
65
+
66
+ def test_connect_with_third_node_killed
67
+ RS.kill(RS.get_node_from_port(RS.ports[2]))
68
+
69
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
70
+ [RS.host, RS.ports[2]])
71
+ assert @conn.connected?
72
+ end
73
+
74
+ def test_connect_with_primary_stepped_down
75
+ RS.step_down_primary
76
+
77
+ rescue_connection_failure do
78
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
79
+ [RS.host, RS.ports[2]])
80
+ end
81
+ assert @conn.connected?
82
+ end
83
+
84
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetCountTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], [RS.host, RS.ports[2]])
11
+ @db = @conn.db(MONGO_TEST_DB)
12
+ @db.drop_collection("test-sets")
13
+ @coll = @db.collection("test-sets")
14
+ end
15
+
16
+ def teardown
17
+ RS.restart_killed_nodes
18
+ end
19
+
20
+ def test_correct_count_after_insertion_reconnect
21
+ @coll.insert({:a => 20}, :safe => {:w => 2, :wtimeout => 10000})
22
+ assert_equal 1, @coll.count
23
+
24
+ # Kill the current master node
25
+ @node = RS.kill_primary
26
+
27
+ rescue_connection_failure do
28
+ @coll.insert({:a => 30}, :safe => true)
29
+ end
30
+
31
+ @coll.insert({:a => 40}, :safe => true)
32
+ assert_equal 3, @coll.count, "Second count failed"
33
+ end
34
+
35
+ end
@@ -1,23 +1,26 @@
1
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require 'mongo'
3
- require 'test/unit'
4
- require 'test/test_helper'
2
+ require './test/replica_sets/rs_test_helper'
5
3
 
6
- # NOTE: this test should be run only if a replica pair is running.
7
- class ReplicaPairInsertTest < Test::Unit::TestCase
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetInsertTest < Test::Unit::TestCase
8
7
  include Mongo
9
8
 
10
9
  def setup
11
- @conn = Mongo::Connection.new({:left => ["localhost", 27017], :right => ["localhost", 27018]}, nil)
12
- @db = @conn.db('mongo-ruby-test')
13
- @db.drop_collection("test-pairs")
14
- @coll = @db.collection("test-pairs")
10
+ @conn = ReplSetConnection.new([TEST_HOST, RS.ports[0]], [TEST_HOST, RS.ports[1]], [TEST_HOST, RS.ports[2]])
11
+ @db = @conn.db(MONGO_TEST_DB)
12
+ @db.drop_collection("test-sets")
13
+ @coll = @db.collection("test-sets")
14
+ end
15
+
16
+ def teardown
17
+ RS.restart_killed_nodes
15
18
  end
16
19
 
17
20
  def test_insert
18
21
  @coll.save({:a => 20}, :safe => true)
19
- puts "Please disconnect the current master."
20
- gets
22
+
23
+ RS.kill_primary
21
24
 
22
25
  rescue_connection_failure do
23
26
  @coll.save({:a => 30}, :safe => true)
@@ -28,9 +31,9 @@ class ReplicaPairInsertTest < Test::Unit::TestCase
28
31
  @coll.save({:a => 60}, :safe => true)
29
32
  @coll.save({:a => 70}, :safe => true)
30
33
 
31
- puts "Please reconnect the old master to make sure that the new master " +
32
- "has synced with the previous master. Note: this may have happened already."
33
- gets
34
+ # Restart the old master and wait for sync
35
+ RS.restart_killed_nodes
36
+ sleep(1)
34
37
  results = []
35
38
 
36
39
  rescue_connection_failure do
@@ -0,0 +1,55 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
11
+ [RS.host, RS.ports[2]], :pool_size => 10, :timeout => 5)
12
+ @db = @conn.db(MONGO_TEST_DB)
13
+ @db.drop_collection("test-sets")
14
+ @coll = @db.collection("test-sets")
15
+ end
16
+
17
+ def teardown
18
+ RS.restart_killed_nodes
19
+ end
20
+
21
+ def test_insert
22
+ expected_results = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
23
+ @coll.save({:a => -1}, :safe => true)
24
+
25
+ RS.kill_primary
26
+
27
+ threads = []
28
+ 10.times do |i|
29
+ threads[i] = Thread.new do
30
+ rescue_connection_failure do
31
+ @coll.save({:a => i}, :safe => true)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Restart the old master and wait for sync
37
+ RS.restart_killed_nodes
38
+ sleep(1)
39
+ results = []
40
+
41
+ rescue_connection_failure do
42
+ @coll.find.each {|r| results << r}
43
+ expected_results.each do |a|
44
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
45
+ end
46
+ end
47
+
48
+ @coll.save({:a => 10}, :safe => true)
49
+ @coll.find.each {|r| results << r}
50
+ (expected_results + [10]).each do |a|
51
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
52
+ end
53
+ end
54
+
55
+ end