Sha256: b4019f518dd91a6146d2e933f8bc5ede1958c2b14b8fd0983ea9f3f8ad9a1065

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

require 'test_helper.rb'

class SynchronizedTest < Test::Unit::TestCase
  
  def test_supports_synchronized_access_of_methods_by_including_synchronized

    klass = Class.new do
      include Synchronized

      attr_accessor :values

      def initialize
        @values = []
      end

      synchronized
      def test_method( value )
        @value = value
        @values << @value
        sleep 1
        @values << @value
      end
    end

    clazz = klass.new

    thread_1 = Thread.new do
      clazz.test_method "thread 1"
    end

    sleep 0.3

    thread_2 = Thread.new do
      clazz.test_method "thread 2"
    end

    thread_1.join 1
    thread_2.join 1

    assert_equal "thread 1", clazz.values[0], "1st wrong"
    assert_equal "thread 1", clazz.values[1], "2nd wrong"
    assert_equal "thread 2", clazz.values[2], "3rd wrong"
    assert_equal "thread 2", clazz.values[3], "4th wrong"
    
  end
  
  def test_supports_synchronized_access_of_methods_by_including_mailbox

    klass = Class.new do
      include Mailbox

      attr_accessor :values

      def initialize
        @values = []
      end

      synchronized
      def test_method( value )
        @value = value
        @values << @value
        sleep 1
        @values << @value
      end
    end

    clazz = klass.new

    thread_1 = Thread.new do
      clazz.test_method "thread 1"
    end

    sleep 0.3

    thread_2 = Thread.new do
      clazz.test_method "thread 2"
    end

    thread_1.join 1
    thread_2.join 1

    assert_equal "thread 1", clazz.values[0], "1st wrong"
    assert_equal "thread 1", clazz.values[1], "2nd wrong"
    assert_equal "thread 2", clazz.values[2], "3rd wrong"
    assert_equal "thread 2", clazz.values[3], "4th wrong"
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mailbox-0.1.7 test/synchronized_test.rb
mailbox-0.1.6 test/synchronized_test.rb
mailbox-0.1.4 test/synchronized_test.rb
mailbox-0.1.3 test/synchronized_test.rb