2009-08-10 20:10:52 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestThreading < Test::Unit::TestCase
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-08-10 20:10:52 +00:00
|
|
|
|
|
|
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
|
|
|
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
2009-08-10 20:10:52 +00:00
|
|
|
@@coll = @@db.collection('thread-test-collection')
|
|
|
|
|
|
|
|
def test_threading
|
|
|
|
@@coll.clear
|
|
|
|
|
|
|
|
1000.times do |i|
|
|
|
|
@@coll.insert("x" => i)
|
|
|
|
end
|
|
|
|
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
threads[i] = Thread.new{
|
|
|
|
sum = 0
|
|
|
|
@@coll.find().each { |document|
|
|
|
|
sum += document["x"]
|
|
|
|
}
|
|
|
|
assert_equal 499500, sum
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
threads[i].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|