Sha256: 1d889c814245c107afc9b0d18c9e3d0e2b639f0fdc055ee3e58821fe28eafe5e

Contents?: true

Size: 1.87 KB

Versions: 19

Compression:

Stored size: 1.87 KB

Contents

require 'test/unit'
require 'thread'
if RUBY_PLATFORM != "java"
  $:.unshift File.expand_path( File.join( File.dirname( __FILE__ ), "../ext/fastthread" ) )
  require 'fastthread'
end

class TestMutex < Test::Unit::TestCase
  def self.mutex_test( name, &body )
    define_method( "test_#{ name }" ) do
      body.call( self, Mutex.new, "" )
    end
    # at one time we also tested Mutex_m here, but it's no longer
    # part of fastthread
  end

  mutex_test( :locked_p ) do |test, m, prefix|
    test.instance_eval do
      assert_equal false, m.send( "#{ prefix }locked?" )
      m.send "#{ prefix }lock"
      assert_equal true, m.send( "#{ prefix }locked?" )
      m.send "#{ prefix }unlock"
      assert_equal false, m.send( "#{ prefix }locked?" )
    end
  end

  mutex_test( :synchronize ) do |test, m, prefix|
    test.instance_eval do
      assert !m.send( "#{ prefix }locked?" )
      m.send "#{ prefix }synchronize" do
        assert m.send( "#{ prefix }locked?" )
      end
      assert !m.send( "#{ prefix }locked?" )
    end
  end

  mutex_test( :synchronize_exception ) do |test, m, prefix|
    test.instance_eval do
      assert !m.send( "#{ prefix }locked?" )
      assert_raise ArgumentError do
        m.send "#{ prefix }synchronize" do
          assert m.send( "#{ prefix }locked?" )
          raise ArgumentError
        end
      end
      assert !m.send( "#{ prefix }locked?" )
    end
  end

  mutex_test( :mutual_exclusion ) do |test, m, prefix|
    test.instance_eval do
      s = ""

      ("a".."c").map do |c|
        Thread.new do
          Thread.pass
          5.times do
            m.send "#{ prefix }synchronize" do
              s << c
              Thread.pass
              s << c
            end
          end
        end
      end.each do |thread|
        thread.join
      end

      assert_equal 30, s.length
      assert s.match( /^(aa|bb|cc)+$/ )
    end
  end
end 

Version data entries

19 entries across 19 versions & 4 rubygems

Version Path
blackwinter-fastthread-1.0.5 test/test_mutex.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/fastthread-1.0.7/test/test_mutex.rb
merb-core-1.1.3 spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
merb-core-1.1.2 spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
merb-core-1.1.1 spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
blackwinter-fastthread-1.0.7 test/test_mutex.rb
merb-core-1.1.0 spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
merb-core-1.1.0.rc1 spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
merb-core-1.1.0.pre spec10/public/webrat/test_app/gems/gems/fastthread-1.0.1/test/test_mutex.rb
fastthread-1.0.1-i386-mswin32 test/test_mutex.rb
fastthread-1.0.5 test/test_mutex.rb
fastthread-1.0.6 test/test_mutex.rb
fastthread-1.0.4 test/test_mutex.rb
fastthread-1.0.3 test/test_mutex.rb
fastthread-0.6.3 test/test_mutex.rb
fastthread-1.0.1 test/test_mutex.rb
fastthread-0.6.4.1 test/test_mutex.rb
fastthread-1.0.7 test/test_mutex.rb
fastthread-1.0 test/test_mutex.rb