From aac0f945c97858ccf84c82051daeabf98c9eb072 Mon Sep 17 00:00:00 2001 From: Phan Le Date: Thu, 4 Mar 2010 11:45:33 +1100 Subject: [PATCH] Added a test for Thread safety --- test/thread_safe_test.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/thread_safe_test.rb diff --git a/test/thread_safe_test.rb b/test/thread_safe_test.rb new file mode 100644 index 00000000..41689850 --- /dev/null +++ b/test/thread_safe_test.rb @@ -0,0 +1,34 @@ +require File.dirname(__FILE__) + '/test_helper.rb' + +class TestController < ActionController::Base + + def current_user + @current_user ||= ActiveSupport::SecureRandom.hex(32) + end + +end + +class ThreadSafeTest < Test::Unit::TestCase + + should "is thread safe when dealing with audit logging" do + blocked = true + + blocked_thread = Thread.new do + controller = TestController.new + controller.send(:set_whodunnit) + begin + puts "sleep for .001 sec" + sleep(0.001) + end while blocked + PaperTrail.whodunnit + end + + fast_running_thread = Thread.new do + controller = TestController.new + controller.send(:set_whodunnit) + blocked = false + PaperTrail.whodunnit + end + assert_not_equal blocked_thread.value, fast_running_thread.value + end +end \ No newline at end of file