Skip to content

Commit 065517a

Browse files
committed
Fixed test bug; better replication ack tests; deprecated DB#error and DB#last_status for DB#get_last_error
1 parent 02e5b77 commit 065517a

File tree

7 files changed

+104
-43
lines changed

7 files changed

+104
-43
lines changed

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ namespace :test do
9898
t.verbose = true
9999
end
100100

101+
Rake::TestTask.new(:replica_set_ack) do |t|
102+
t.test_files = FileList['test/replica_sets/replication_ack_test.rb']
103+
t.verbose = true
104+
end
105+
101106
Rake::TestTask.new(:auto_reconnect) do |t|
102107
t.test_files = FileList['test/auxillary/autoreconnect_test.rb']
103108
t.verbose = true

lib/mongo/db.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ def drop_collection(name)
276276
ok?(command(:drop => name))
277277
end
278278

279+
# @deprecated
280+
#
279281
# Get the error message from the most recently executed database
280282
# operation for this connection.
281283
#
@@ -286,19 +288,36 @@ def drop_collection(name)
286288
# @return [String, Nil] either the text describing an error or nil if no
287289
# error has occurred.
288290
def error(opts={})
291+
warn "DB#error is deprecated. Please use DB#get_last_error instead"
289292
opts.assert_valid_keys(:w, :wtimeout, :fsync)
293+
get_last_error(opts)['err']
294+
end
295+
296+
# Run the getlasterror command with the specified replication options.
297+
#
298+
# @option opts [Boolean] :fsync (false)
299+
# @option opts [Integer] :w (nil)
300+
# @option opts [Integer] :wtimeout (nil)
301+
#
302+
# @return [Hash] the entire response to getlasterror.
303+
#
304+
# @raise [MongoDBError] if the operation fails.
305+
def get_last_error(opts={})
290306
cmd = BSON::OrderedHash.new
291307
cmd[:getlasterror] = 1
292-
cmd.merge!(opts) unless opts.empty?
308+
cmd.merge!(opts)
293309
doc = command(cmd, :check_response => false)
294310
raise MongoDBError, "error retrieving last error: #{doc.inspect}" unless ok?(doc)
295-
doc['err']
311+
doc
296312
end
297313

314+
# @deprecated
315+
#
298316
# Get status information from the last operation on this connection.
299317
#
300318
# @return [Hash] a hash representing the status of the last db op.
301319
def last_status
320+
warn "DB#last_status is deprecated. Please use the equivalent DB#get_last_error instead"
302321
command(:getlasterror => 1)
303322
end
304323

test/collection_test.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,6 @@ def test_safe_mode_with_advanced_safe_with_invalid_options
139139
@@test.remove({:foo => 2}, :safe => {:w => 2, :wtime => 1, :fsync => true})
140140
end
141141
end
142-
143-
def test_safe_mode_with_w_failure
144-
assert_raise_error OperationFailure, "timed out waiting for slaves" do
145-
@@test.insert({:foo => 1}, :safe => {:w => 2, :wtimeout => 1, :fsync => true})
146-
end
147-
assert_raise_error OperationFailure, "timed out waiting for slaves" do
148-
@@test.update({:foo => 1}, {:foo => 2}, :safe => {:w => 2, :wtimeout => 1, :fsync => true})
149-
end
150-
assert_raise_error OperationFailure, "timed out waiting for slaves" do
151-
@@test.remove({:foo => 2}, :safe => {:w => 2, :wtimeout => 1, :fsync => true})
152-
end
153-
end
154-
155-
def test_safe_mode_with_write_and_fsync
156-
assert @@test.insert({:foo => 1}, :safe => {:w => 1, :wtimeout => 1, :fsync => true})
157-
assert @@test.update({:foo => 1}, {:foo => 2}, :safe => {:w => 1, :wtimeout => 1, :fsync => true})
158-
assert @@test.remove({:foo => 2}, :safe => {:w => 1, :wtimeout => 1, :fsync => true})
159-
end
160142
end
161143

162144
def test_update

test/cursor_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,18 @@ def test_has_next
399399

400400
def test_cursor_invalid
401401
@@coll.remove
402-
1000.times do |n|
402+
10000.times do |n|
403403
@@coll.insert({:a => n})
404404
end
405405

406406
cursor = @@coll.find({})
407-
cursor.next_document
408-
cursor.close
409407

410-
assert_raise_error(Mongo::OperationFailure, "CURSOR_NOT_FOUND") do
411-
999.times do
408+
assert_raise_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
409+
9999.times do
412410
cursor.next_document
411+
cursor.instance_variable_set(:@cursor_id, 1234567890)
413412
end
414413
end
415414
end
415+
416416
end

test/db_test.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,6 @@ def test_error
191191
assert_nil @@db.previous_error
192192
end
193193

194-
if @@version >= "1.5.1"
195-
def test_failing_error_params
196-
assert_raise_error Mongo::MongoDBError, "timed out waiting for slaves" do
197-
@@db.error(:w => 2, :wtimeout => 10, :fsync => true)
198-
end
199-
end
200-
201-
def test_passing_error_params
202-
assert_nil @@db.error(:w => 1, :wtimeout => 10, :fsync => true)
203-
end
204-
205-
def test_invalid_error_params
206-
assert_raise_error ArgumentError, "Unknown key(s): z" do
207-
@@db.error(:z => 1, :wtimeout => 10, :fsync => true)
208-
end
209-
end
210-
end
211-
212194
def test_check_command_response
213195
command = {:forceerror => 1}
214196
assert_raise OperationFailure do
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'mongo'
3+
require 'test/unit'
4+
require 'test/test_helper'
5+
6+
# NOTE: This test expects a replica set of three nodes to be running on local host.
7+
class ReplicaSetAckTest < Test::Unit::TestCase
8+
include Mongo
9+
10+
def setup
11+
@conn = Mongo::Connection.multi([['localhost', 27017], ['localhost', 27018], ['localhost', 27019]])
12+
13+
master = [@conn.host, @conn.port]
14+
@slaves = @conn.nodes - master
15+
16+
@slave1 = Mongo::Connection.new(@slaves[0][0], @slaves[0][1], :slave_ok => true)
17+
@slave2 = Mongo::Connection.new(@slaves[1][0], @slaves[1][1], :slave_ok => true)
18+
19+
@db = @conn.db(MONGO_TEST_DB)
20+
@db.drop_collection("test-sets")
21+
@col = @db.collection("test-sets")
22+
end
23+
24+
def test_safe_mode_with_w_failure
25+
assert_raise_error OperationFailure, "timed out waiting for slaves" do
26+
@col.insert({:foo => 1}, :safe => {:w => 4, :wtimeout => 1, :fsync => true})
27+
end
28+
assert_raise_error OperationFailure, "timed out waiting for slaves" do
29+
@col.update({:foo => 1}, {:foo => 2}, :safe => {:w => 4, :wtimeout => 1, :fsync => true})
30+
end
31+
assert_raise_error OperationFailure, "timed out waiting for slaves" do
32+
@col.remove({:foo => 2}, :safe => {:w => 4, :wtimeout => 1, :fsync => true})
33+
end
34+
end
35+
36+
def test_safe_mode_replication_ack
37+
@col.insert({:baz => "bar"}, :safe => {:w => 3, :wtimeout => 1000})
38+
39+
assert @col.insert({:foo => "0" * 10000}, :safe => {:w => 3, :wtimeout => 1000})
40+
assert_equal 2, @slave1[MONGO_TEST_DB]["test-sets"].count
41+
assert_equal 2, @slave2[MONGO_TEST_DB]["test-sets"].count
42+
43+
44+
assert @col.update({:baz => "bar"}, {:baz => "foo"}, :safe => {:w => 3, :wtimeout => 1000})
45+
assert @slave1[MONGO_TEST_DB]["test-sets"].find_one({:baz => "foo"})
46+
assert @slave2[MONGO_TEST_DB]["test-sets"].find_one({:baz => "foo"})
47+
48+
assert @col.remove({}, :safe => {:w => 3, :wtimeout => 1000})
49+
assert_equal 0, @slave1[MONGO_TEST_DB]["test-sets"].count
50+
assert_equal 0, @slave2[MONGO_TEST_DB]["test-sets"].count
51+
end
52+
53+
def test_last_error_responses
54+
20.times { @col.insert({:baz => "bar"}) }
55+
response = @db.get_last_error(:w => 3, :wtimeout => 10000)
56+
assert response['ok'] == 1
57+
assert response['lastOp']
58+
59+
@col.update({}, {:baz => "foo"}, :multi => true)
60+
response = @db.get_last_error(:w => 3, :wtimeout => 1000)
61+
assert response['ok'] == 1
62+
assert response['lastOp']
63+
64+
@col.remove({})
65+
response = @db.get_last_error(:w => 3, :wtimeout => 1000)
66+
assert response['ok'] == 1
67+
assert response['n'] == 20
68+
assert response['lastOp']
69+
end
70+
71+
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def assert_raise_error(klass, message)
4848
rescue => e
4949
assert_equal klass, e.class
5050
assert e.message.include?(message), "#{e.message} does not include #{message}."
51+
else
52+
flunk "Expected assertion #{klass} but none was raised."
5153
end
5254
end
5355
end

0 commit comments

Comments
 (0)