Skip to content

Commit 01ca04c

Browse files
committed
Merge pull request #161 from estolfo/master
Refactoring replica set cursor tests
2 parents a96bc6e + 1d8411d commit 01ca04c

File tree

1 file changed

+83
-56
lines changed

1 file changed

+83
-56
lines changed

test/replica_set/cursor_test.rb

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ def test_get_more_secondary
1818

1919
def test_close_primary
2020
setup_client(:primary)
21-
cursor_close_test(:primary)
21+
kill_cursor_test(:primary)
2222
end
2323

2424
def test_close_secondary
2525
setup_client(:secondary)
26-
cursor_close_test(:secondary)
26+
kill_cursor_test(:secondary)
2727
end
2828

2929
def test_cursors_get_closed
3030
setup_client
31-
assert_cursor_count
31+
assert_cursors_on_members
3232
end
3333

3434
def test_cursors_get_closed_secondary
3535
setup_client(:secondary)
36-
assert_cursor_count(:secondary)
36+
assert_cursors_on_members(:secondary)
3737
end
3838

3939
def test_cursors_get_closed_secondary_query
4040
setup_client(:primary)
41-
assert_cursor_count(:secondary)
41+
assert_cursors_on_members(:secondary)
4242
end
4343

4444
private
@@ -51,24 +51,17 @@ def setup_client(read=:primary)
5151
@db = @client.db(MONGO_TEST_DB)
5252
@db.drop_collection("cursor_tests")
5353
@coll = @db.collection("cursor_tests")
54-
55-
@coll.insert({:a => 1}, :w => 3)
56-
@coll.insert({:b => 2}, :w => 3)
57-
@coll.insert({:c => 3}, :w => 3)
58-
59-
# Pin reader
60-
@coll.find_one
54+
insert_docs
6155

6256
# Setup Direct Connections
6357
@primary = Mongo::MongoClient.new(*@client.manager.primary)
6458
end
6559

66-
def cursor_count(client)
67-
client['cursor_tests'].command({:cursorInfo => 1})['totalOpen']
68-
end
69-
70-
def query_count(client)
71-
client['admin'].command({:serverStatus => 1})['opcounters']['query']
60+
def insert_docs
61+
@n_docs = 102 # batch size is 101
62+
@n_docs.times do |i|
63+
@coll.insert({ "x" => i }, :w => 3)
64+
end
7265
end
7366

7467
def set_read_client_and_tag(read)
@@ -80,66 +73,100 @@ def set_read_client_and_tag(read)
8073
cursor.next
8174
pool = cursor.instance_variable_get(:@pool)
8275
cursor.close
83-
@read = Mongo::MongoClient.new(pool.host, pool.port)
76+
@read = Mongo::MongoClient.new(pool.host, pool.port, :slave_ok => true)
8477
tag
8578
rescue Mongo::ConnectionFailure
8679
false
8780
end
8881
end
8982
end
9083

91-
def assert_cursor_count(read=:primary)
92-
set_read_client_and_tag(read)
93-
94-
before_primary_cursor = cursor_count(@primary)
95-
before_read_cursor = cursor_count(@read)
96-
before_read_query = query_count(@read)
97-
84+
def route_query(read)
9885
read_opts = {:read => read}
9986
read_opts[:tag_sets] = [{:node => @tag}] unless read == :primary
100-
@coll.find({}, read_opts).limit(2).to_a
101-
102-
after_primary_cursor = cursor_count(@primary)
103-
after_read_cursor = cursor_count(@read)
104-
after_read_query = query_count(@read)
87+
object_id = BSON::ObjectId.new
88+
read_opts[:comment] = object_id
89+
90+
# set profiling level to 2 on client and member to which the query will be routed
91+
@client.db(MONGO_TEST_DB).profiling_level = :all
92+
@client.secondaries.each do |node|
93+
node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
94+
node.db(MONGO_TEST_DB).profiling_level = :all
95+
end
10596

106-
assert_equal before_primary_cursor, after_primary_cursor
107-
assert_equal before_read_cursor, after_read_cursor
108-
assert_equal 1, after_read_query - before_read_query
109-
end
97+
@cursor = @coll.find({}, read_opts)
98+
@cursor.next
11099

111-
def insert_docs
112-
102.times do |i|
113-
@coll.insert({:i =>i}, :w => 3)
100+
# on client and other members set profiling level to 0
101+
@client.db(MONGO_TEST_DB).profiling_level = :off
102+
@client.secondaries.each do |node|
103+
node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
104+
node.db(MONGO_TEST_DB).profiling_level = :off
114105
end
106+
# do a query on system.profile of the reader to see if it was used for the query
107+
profiled_queries = @read.db(MONGO_TEST_DB).collection('system.profile').find({
108+
'ns' => "#{MONGO_TEST_DB}.cursor_tests", "query.$comment" => object_id })
109+
110+
assert_equal 1, profiled_queries.count
115111
end
116112

117113
# batch from send_initial_query is 101 documents
114+
# check that you get n_docs back from the query, with the same port
118115
def cursor_get_more_test(read=:primary)
119-
insert_docs
116+
set_read_client_and_tag(read)
120117
10.times do
121-
cursor = @coll.find({}, :read => read)
122-
cursor.next
123-
port = cursor.instance_variable_get(:@pool).port
124-
assert cursor.alive?
125-
while cursor.has_next?
126-
cursor.next
127-
assert_equal port, cursor.instance_variable_get(:@pool).port
118+
# assert that the query went to the correct member
119+
route_query(read)
120+
docs_count = 1
121+
port = @cursor.instance_variable_get(:@pool).port
122+
assert @cursor.alive?
123+
while @cursor.has_next?
124+
docs_count += 1
125+
@cursor.next
126+
assert_equal port, @cursor.instance_variable_get(:@pool).port
128127
end
129-
assert !cursor.alive?
130-
cursor.close #cursor is already closed
128+
assert !@cursor.alive?
129+
assert_equal @n_docs, docs_count
130+
@cursor.close #cursor is already closed
131131
end
132132
end
133133

134134
# batch from get_more can be huge, so close after send_initial_query
135-
def cursor_close_test(read=:primary)
136-
insert_docs
135+
def kill_cursor_test(read=:primary)
136+
set_read_client_and_tag(read)
137137
10.times do
138-
cursor = @coll.find({}, :read => read)
139-
cursor.next
140-
assert cursor.instance_variable_get(:@pool)
141-
assert cursor.alive?
142-
cursor.close
138+
# assert that the query went to the correct member
139+
route_query(read)
140+
cursor_id = @cursor.cursor_id
141+
cursor_clone = @cursor.clone
142+
assert_equal cursor_id, cursor_clone.cursor_id
143+
assert @cursor.instance_variable_get(:@pool)
144+
# .next was called once already and leave one for get more
145+
(@n_docs-2).times { @cursor.next }
146+
@cursor.close
147+
# an exception confirms the cursor has indeed been closed
148+
assert_raise Mongo::OperationFailure do
149+
cursor_clone.next
150+
end
151+
end
152+
end
153+
154+
def assert_cursors_on_members(read=:primary)
155+
set_read_client_and_tag(read)
156+
# assert that the query went to the correct member
157+
route_query(read)
158+
cursor_id = @cursor.cursor_id
159+
cursor_clone = @cursor.clone
160+
assert_equal cursor_id, cursor_clone.cursor_id
161+
assert @cursor.instance_variable_get(:@pool)
162+
port = @cursor.instance_variable_get(:@pool).port
163+
while @cursor.has_next?
164+
@cursor.next
165+
assert_equal port, @cursor.instance_variable_get(:@pool).port
166+
end
167+
# an exception confirms the cursor has indeed been closed after query
168+
assert_raise Mongo::OperationFailure do
169+
cursor_clone.next
143170
end
144171
end
145-
end
172+
end

0 commit comments

Comments
 (0)