Sha256: 3610d4b5c93fe3dd2d473727067cd1516e3679e8be2975a449fe59226fe240f2

Contents?: true

Size: 829 Bytes

Versions: 1

Compression:

Stored size: 829 Bytes

Contents

require 'minitest/autorun'
require "sdb_lock"

class LockTest < MiniTest::Test
  def setup
    @lock = SdbLock.new(
        'lock_test'
    )
    @lock.unlock("test")
  end

  # This test will take long time (16 secs in my environment).
  def test_try_lock
    shared_var = 0
    locked_count = 0

    threads = 10.times.map do
      Thread.new do
        Thread.pass
        locked = @lock.try_lock("test") { shared_var += 1 }
        locked_count += 1 if locked
      end
    end
    threads.each { |thread| thread.join }
    assert_equal(locked_count, shared_var)
  end

  def test_lock
    shared_var = 0
    threads = 10.times.map do
      Thread.new do
        Thread.pass
        @lock.lock("test") { shared_var += 1 }
      end
    end
    threads.each { |thread| thread.join }
    assert_equal(10, shared_var)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sdb_lock-0.1.0 test/test_lock.rb