mongo-ruby-driver/test/threading/test_threading_large_pool.rb

91 lines
2.1 KiB
Ruby
Raw Normal View History

require './test/test_helper'
2009-11-24 18:47:37 +00:00
# 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, :timeout => 60).db(MONGO_TEST_DB)
2009-11-24 18:47:37 +00:00
@@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)
2009-11-24 18:47:37 +00:00
end
def test_safe_update
set_up_safe_data
threads = []
300.times do |i|
2009-11-24 18:47:37 +00:00
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
2009-12-15 21:40:40 +00:00
300.times do |i|
2009-11-24 18:47:37 +00:00
threads[i].join
end
end
def test_safe_insert
2009-11-24 21:13:14 +00:00
set_up_safe_data
threads = []
300.times do |i|
2009-11-24 21:13:14 +00:00
threads[i] = Thread.new do
if i % 2 == 0
assert_raise Mongo::OperationFailure do
@unique.insert({"test" => "insert"}, :safe => true)
2009-11-24 18:47:37 +00:00
end
2009-11-24 21:13:14 +00:00
else
@duplicate.insert({"test" => "insert"}, :safe => true)
2009-11-24 18:47:37 +00:00
end
end
2009-11-24 21:13:14 +00:00
end
2009-12-15 21:40:40 +00:00
300.times do |i|
2009-11-24 18:47:37 +00:00
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