/
threading_with_large_pool_test.rb
90 lines (75 loc) · 2.1 KB
/
threading_with_large_pool_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require File.expand_path("../../test_helper", __FILE__)
# Essentialy the same as test_threading.rb but with an expanded pool for
# testing multiple connections.
class TestThreadingLargePool < Test::Unit::TestCase
include Mongo
@@db = standard_connection(:pool_size => 50, :pool_timeout => 60).db(MONGO_TEST_DB)
@@coll = @@db.collection('thread-test-collection')
def set_up_safe_data
@@db.drop_collection('duplicate')
@@db.drop_collection('unique')
@duplicate = @@db.collection('duplicate')
@unique = @@db.collection('unique')
@duplicate.insert("test" => "insert")
@duplicate.insert("test" => "update")
@unique.insert("test" => "insert")
@unique.insert("test" => "update")
@unique.create_index("test", :unique => true)
end
def test_safe_update
set_up_safe_data
threads = []
300.times do |i|
threads[i] = Thread.new do
if i % 2 == 0
assert_raise Mongo::OperationFailure do
@unique.update({"test" => "insert"}, {"$set" => {"test" => "update"}}, :safe => true)
end
else
@duplicate.update({"test" => "insert"}, {"$set" => {"test" => "update"}}, :safe => true)
end
end
end
300.times do |i|
threads[i].join
end
end
def test_safe_insert
set_up_safe_data
threads = []
300.times do |i|
threads[i] = Thread.new do
if i % 2 == 0
assert_raise Mongo::OperationFailure do
@unique.insert({"test" => "insert"}, :safe => true)
end
else
@duplicate.insert({"test" => "insert"}, :safe => true)
end
end
end
300.times do |i|
threads[i].join
end
end
def test_threading
@@coll.drop
@@coll = @@db.collection('thread-test-collection')
1000.times do |i|
@@coll.insert("x" => i)
end
threads = []
10.times do |i|
threads[i] = Thread.new do
sum = 0
@@coll.find().each do |document|
sum += document["x"]
end
assert_equal 499500, sum
end
end
10.times do |i|
threads[i].join
end
end
end