mongo-ruby-driver/test/test_threading.rb

38 lines
780 B
Ruby
Raw Normal View History

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
include Mongo
2009-08-10 20:10:52 +00:00
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@@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