Skip to content

Commit e026435

Browse files
Stress tests for the bulk API
1 parent f58428a commit e026435

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright (C) 2009-2013 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'test_helper'
16+
17+
class BulkApiStressTest < Test::Unit::TestCase
18+
19+
# Generate a large string of 'size' MB (estimated
20+
# by a string of 'size' * 1024 * 1024 characters).
21+
def generate_large_string(size)
22+
s = "a" * (size * 1024 * 1024)
23+
end
24+
25+
def setup
26+
@client = standard_connection
27+
@db = @client[TEST_DB]
28+
@coll = @db["bulk-api-stress-tests"]
29+
@coll.remove
30+
end
31+
32+
def test_ordered_batch_large_inserts
33+
bulk = @coll.initialize_ordered_bulk_op
34+
s = generate_large_string(4)
35+
36+
for i in 0..5
37+
bulk.insert({:_id => i, :msg => s})
38+
end
39+
bulk.insert({:_id => 3}) # error
40+
bulk.insert({:_id => 100})
41+
42+
ex = assert_raise BulkWriteError do
43+
bulk.execute
44+
end
45+
46+
error_details = ex.result
47+
assert_equal 6, error_details["nInserted"]
48+
assert_equal 1, error_details["writeErrors"].length
49+
error = error_details["writeErrors"][0]
50+
assert_equal 11000, error["code"] # duplicate key error
51+
assert error["errmsg"].kind_of? String
52+
assert_equal 6, error["index"]
53+
assert_equal 6, @coll.count()
54+
end
55+
56+
def test_unordered_batch_large_inserts
57+
bulk = @coll.initialize_unordered_bulk_op
58+
s = generate_large_string(4)
59+
60+
for i in 0..5
61+
bulk.insert({:_id => i, :msg => s})
62+
end
63+
bulk.insert({:_id => 3}) # error
64+
bulk.insert({:_id => 100})
65+
66+
ex = assert_raise BulkWriteError do
67+
bulk.execute
68+
end
69+
70+
error_details = ex.result
71+
assert_equal 7, error_details["nInserted"]
72+
assert_equal 1, error_details["writeErrors"].length
73+
error = error_details["writeErrors"][0]
74+
assert_equal 11000, error["code"] # duplicate key error
75+
assert error["errmsg"].kind_of? String
76+
assert_equal 6, error["index"]
77+
assert_equal 7, @coll.count()
78+
end
79+
80+
def test_large_single_insert
81+
bulk = @coll.initialize_unordered_bulk_op
82+
s = generate_large_string(17)
83+
bulk.insert({:a => s})
84+
# RUBY-730:
85+
# ex = assert_raise BulkWriteError do
86+
# bulk.execute
87+
# end
88+
end
89+
90+
def test_ordered_batch_large_batch
91+
bulk = @coll.initialize_ordered_bulk_op
92+
93+
bulk.insert({:_id => 1600})
94+
for i in 0..2000
95+
bulk.insert({:_id => i})
96+
end
97+
98+
ex = assert_raise BulkWriteError do
99+
bulk.execute
100+
end
101+
102+
error_details = ex.result
103+
assert_equal 1601, error_details["nInserted"]
104+
assert_equal 1, error_details["writeErrors"].length
105+
error = error_details["writeErrors"][0]
106+
assert_equal 11000, error["code"] # duplicate key error
107+
assert error["errmsg"].kind_of? String
108+
assert_equal 1601, error["index"]
109+
assert_equal 1601, @coll.count()
110+
end
111+
112+
def test_unordered_batch_large_batch
113+
bulk = @coll.initialize_unordered_bulk_op
114+
115+
bulk.insert({:_id => 1600})
116+
for i in 0..2000
117+
bulk.insert({:_id => i})
118+
end
119+
120+
ex = assert_raise BulkWriteError do
121+
bulk.execute
122+
end
123+
124+
error_details = ex.result
125+
assert_equal 2001, error_details["nInserted"]
126+
assert_equal 1, error_details["writeErrors"].length
127+
error = error_details["writeErrors"][0]
128+
assert_equal 11000, error["code"] # duplicate key error
129+
assert error["errmsg"].kind_of? String
130+
assert_equal 1601, error["index"]
131+
assert_equal 2001, @coll.count()
132+
end
133+
end

0 commit comments

Comments
 (0)