Sha256: 9ae36b480ea5cf6ce0084a50e4d1189c548566ced4568963f9ab2565d3f9b75e

Contents?: true

Size: 696 Bytes

Versions: 3

Compression:

Stored size: 696 Bytes

Contents

require_relative "helper"
require_relative "../lib/ohm/locking"

class Server < Ohm::Model
  include Ohm::Locking
  extend Ohm::Locking

  attribute :taken
  index :taken

  def self.acquire
    spinlock do
      s = Server.find(taken: 0).first

      if s
        s.update(taken: 1)
      end

      return s
    end
  end
end

test "mutex method is added at instance and class level" do
  assert Server.new.respond_to?(:spinlock)
end

test "acquire with concurrency" do
  s = Server.create(taken: 0)

  res = {}

  threads = 5.times.map do |i|
    Thread.new(i, res) do |idx, e|
      e[idx] = Server.acquire
    end
  end

  threads.each(&:join)

  assert_equal 1, res.values.compact.size
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ohm-contrib-3.0.0 test/locking.rb
ohm-contrib-2.2.0 test/locking.rb
ohm-contrib-2.0.1 test/locking.rb