2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../test_helper", __FILE__)
|
2009-08-10 20:10:52 +00:00
|
|
|
|
|
|
|
class TestThreading < Test::Unit::TestCase
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-08-10 20:10:52 +00:00
|
|
|
|
2012-03-16 20:17:33 +00:00
|
|
|
@@con = standard_connection(:pool_size => 10, :pool_timeout => 30)
|
2011-11-16 18:06:56 +00:00
|
|
|
@@db = @@con[MONGO_TEST_DB]
|
2009-08-10 20:10:52 +00:00
|
|
|
@@coll = @@db.collection('thread-test-collection')
|
|
|
|
|
2009-11-05 20:14:48 +00:00
|
|
|
def set_up_safe_data
|
|
|
|
@@db.drop_collection('duplicate')
|
|
|
|
@@db.drop_collection('unique')
|
|
|
|
@duplicate = @@db.collection('duplicate')
|
2009-11-23 20:20:05 +00:00
|
|
|
@unique = @@db.collection('unique')
|
2009-11-05 20:14:48 +00:00
|
|
|
|
|
|
|
@duplicate.insert("test" => "insert")
|
|
|
|
@duplicate.insert("test" => "update")
|
|
|
|
@unique.insert("test" => "insert")
|
|
|
|
@unique.insert("test" => "update")
|
2010-04-28 19:16:33 +00:00
|
|
|
@unique.create_index("test", :unique => true)
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_update
|
2011-11-03 22:37:23 +00:00
|
|
|
times = []
|
2009-11-05 20:14:48 +00:00
|
|
|
set_up_safe_data
|
|
|
|
threads = []
|
|
|
|
100.times do |i|
|
|
|
|
threads[i] = Thread.new do
|
2011-11-04 18:26:12 +00:00
|
|
|
100.times do
|
2011-11-03 22:37:23 +00:00
|
|
|
if i % 2 == 0
|
|
|
|
assert_raise Mongo::OperationFailure do
|
|
|
|
t1 = Time.now
|
|
|
|
@unique.update({"test" => "insert"}, {"$set" => {"test" => "update"}}, :safe => true)
|
|
|
|
times << Time.now - t1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
t1 = Time.now
|
|
|
|
@duplicate.update({"test" => "insert"}, {"$set" => {"test" => "update"}}, :safe => true)
|
|
|
|
times << Time.now - t1
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-12-15 21:40:40 +00:00
|
|
|
|
2009-11-05 20:14:48 +00:00
|
|
|
100.times do |i|
|
|
|
|
threads[i].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_insert
|
|
|
|
set_up_safe_data
|
|
|
|
threads = []
|
|
|
|
100.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
|
2009-12-15 21:40:40 +00:00
|
|
|
|
2009-11-05 20:14:48 +00:00
|
|
|
100.times do |i|
|
|
|
|
threads[i].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-10 20:10:52 +00:00
|
|
|
def test_threading
|
2009-11-05 20:14:48 +00:00
|
|
|
@@coll.drop
|
|
|
|
@@coll = @@db.collection('thread-test-collection')
|
2009-08-10 20:10:52 +00:00
|
|
|
|
|
|
|
1000.times do |i|
|
2012-04-03 19:57:02 +00:00
|
|
|
@@coll.insert("x" => i, :safe => true)
|
2009-08-10 20:10:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
10.times do |i|
|
2009-11-05 20:14:48 +00:00
|
|
|
threads[i] = Thread.new do
|
2009-08-10 20:10:52 +00:00
|
|
|
sum = 0
|
2009-11-05 20:14:48 +00:00
|
|
|
@@coll.find().each do |document|
|
2009-08-10 20:10:52 +00:00
|
|
|
sum += document["x"]
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
2009-08-10 20:10:52 +00:00
|
|
|
assert_equal 499500, sum
|
2009-11-05 20:14:48 +00:00
|
|
|
end
|
2009-08-10 20:10:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
threads[i].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|