Skip to content

Instantly share code, notes, and snippets.

@nahi
Created December 17, 2011 07:18
Show Gist options
  • Save nahi/1489554 to your computer and use it in GitHub Desktop.
Save nahi/1489554 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'tempfile'
class TestRequireOnce < Test::Unit::TestCase
def test_require_fires_once_across_threads
file = Tempfile.open(['target', '.rb'])
file.puts "$foo += 1"
file.close
$foo = 0
100.times do |idx|
[1,2,3,4,5].map { |x|
Thread.new {require file.path}
}.each(&:join)
raise "Concurrent requires caused double-loading: $foo = #{$foo} at #{idx}th try" if $foo != 1
$foo = 0
$".delete(file.path)
end
end
end
% rbx test_require_once.rb
Loaded suite test_require_once
Started
E
Finished in 0.041527 seconds.
1) Error:
test_require_fires_once_across_threads(TestRequireOnce):
RuntimeError: Concurrent requires caused double-loading: $foo = 2 at 12th try
test_require_once.rb:14:in `test_require_fires_once_across_threads'
kernel/common/integer.rb:92:in `times'
test_require_once.rb:10:in `test_require_fires_once_across_threads'
kernel/bootstrap/array.rb:66:in `each'
kernel/bootstrap/array.rb:66:in `each'
1 tests, 0 assertions, 0 failures, 1 errors
zsh: exit 1 /home/nahi/git/rubinius/bin/rbx test_require_once.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment