Sha256: 7de8cc1c8a6f4b7b7e47bb899c57f092f13ac8aa1c4bcea879424d9684da39a1

Contents?: true

Size: 1007 Bytes

Versions: 4

Compression:

Stored size: 1007 Bytes

Contents

# frozen_string_literal: true

require_relative 'helper'

class MutexTest < MiniTest::Test
  def test_mutex
    buf = []
    lock = Polyphony::Mutex.new
    (1..3).each do |i|
      spin do
        lock.synchronize do
          buf << ">> #{i}"
          sleep(rand * 0.05)
          buf << "<< #{i}"
        end
      end
    end

    Fiber.current.await_all_children
    assert_equal ['>> 1', '<< 1', '>> 2', '<< 2', '>> 3', '<< 3'], buf
  end

  def test_condition_variable
    buf = []
    lock1 = Polyphony::Mutex.new
    lock2 = Polyphony::Mutex.new
    cond = Polyphony::ConditionVariable.new

    spin do
      lock1.synchronize do
        sleep 0.01
        cond.wait(lock1)
        lock2.synchronize do
          buf << :foo
        end
      end
    end

    spin do
      lock2.synchronize do
        sleep 0.01
        lock1.synchronize do
          buf << :bar
        end
        cond.signal
      end
    end

    Fiber.current.await_all_children
    assert_equal [:bar, :foo], buf
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polyphony-0.45.4 test/test_sync.rb
polyphony-0.45.2 test/test_sync.rb
polyphony-0.45.1 test/test_sync.rb
polyphony-0.45.0 test/test_sync.rb