2010-03-19 14:19:01 +00:00
|
|
|
require 'test_helper'
|
2010-03-04 00:45:33 +00:00
|
|
|
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
def current_user
|
2010-03-19 14:19:01 +00:00
|
|
|
Thread.current.object_id
|
2010-03-04 00:45:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ThreadSafeTest < Test::Unit::TestCase
|
2010-03-19 14:19:01 +00:00
|
|
|
should "be thread safe" do
|
2010-03-04 00:45:33 +00:00
|
|
|
blocked = true
|
2010-03-19 14:19:01 +00:00
|
|
|
|
|
|
|
slow_thread = Thread.new do
|
2010-03-04 00:45:33 +00:00
|
|
|
controller = TestController.new
|
2010-03-19 14:19:01 +00:00
|
|
|
controller.send :set_whodunnit
|
2010-03-04 00:45:33 +00:00
|
|
|
begin
|
2010-03-19 14:19:01 +00:00
|
|
|
sleep 0.001
|
2010-03-04 00:45:33 +00:00
|
|
|
end while blocked
|
|
|
|
PaperTrail.whodunnit
|
|
|
|
end
|
|
|
|
|
2010-03-19 14:19:01 +00:00
|
|
|
fast_thread = Thread.new do
|
2010-03-04 00:45:33 +00:00
|
|
|
controller = TestController.new
|
2010-03-19 14:19:01 +00:00
|
|
|
controller.send :set_whodunnit
|
|
|
|
who = PaperTrail.whodunnit
|
2010-03-04 00:45:33 +00:00
|
|
|
blocked = false
|
2010-03-19 14:19:01 +00:00
|
|
|
who
|
2010-03-04 00:45:33 +00:00
|
|
|
end
|
2010-03-19 14:19:01 +00:00
|
|
|
|
|
|
|
assert_not_equal slow_thread.value, fast_thread.value
|
2010-03-04 00:45:33 +00:00
|
|
|
end
|
2010-03-19 14:19:01 +00:00
|
|
|
end
|