Sha256: aae9ad810212bd48f563cc762b6d85a9d8c18d773e9c5d0bb0abba772b43a48c

Contents?: true

Size: 883 Bytes

Versions: 6

Compression:

Stored size: 883 Bytes

Contents

require 'minitest_helper'

describe "simplest" do
  it "should prevent threads from accessing a resource concurrently" do
    times = ActiveSupport::OrderedHash.new
    ActiveRecord::Base.connection_pool.disconnect!
    t1 = Thread.new do
      ActiveRecord::Base.connection.reconnect!
      ActiveRecord::Base.with_advisory_lock("simplest test") do
        times[:t1_acquire] = Time.now
        sleep 0.5
      end
      times[:t1_release] = Time.now
    end
    sleep 0.1
    t2 = Thread.new do
      ActiveRecord::Base.connection.reconnect!
      ActiveRecord::Base.with_advisory_lock("simplest test") do
        times[:t2_acquire] = Time.now
        sleep 1
      end
      times[:t2_release] = Time.now
    end
    t1.join
    t2.join
    times.keys.must_equal [:t1_acquire, :t1_release, :t2_acquire, :t2_release]
    times[:t2_acquire].must_be :>, times[:t1_release]
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
with_advisory_lock-0.0.8 test/simplest_test.rb
with_advisory_lock-0.0.7 test/simplest_test.rb
with_advisory_lock-0.0.6 test/simplest_test.rb
with_advisory_lock-0.0.5 test/simplest_test.rb
with_advisory_lock-0.0.4 test/simplest_test.rb
with_advisory_lock-0.0.3 test/simplest_test.rb