Sha256: e1d4d06dcf826828a25e1317fd806c92b63b8295148829d4dcbea8c5c1575b9c

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require_relative 'helper'

class TimerTest < MiniTest::Test
  def test_that_one_shot_timer_works
    count = 0
    t = Gyro::Timer.new(0.01, 0)
    spin {
      t.await
      count += 1
    }
    suspend
    assert_equal 1, count
  end

  def test_that_repeating_timer_works
    count = 0
    t = Gyro::Timer.new(0.001, 0.001)
    spin {
      loop {
        t.await
        count += 1
        break if count >= 3
      }
    }
    suspend
    assert_equal 3, count
  ensure
    t.stop
  end

  def test_that_repeating_timer_compensates_for_drift
    count = 0
    t = Gyro::Timer.new(0.1, 0.1)
    deltas = []
    last = nil
    spin {
      last = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
      loop {
        t.await
        now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        elapsed = (now - last)
        deltas << elapsed
        last = now
        count += 1
        sleep 0.05
        break if count >= 3
      }
    }
    suspend
    assert_equal 0, deltas[1..-1].filter { |d| (d - 0.1).abs >= 0.05 }.size
  ensure
    t.stop
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
polyphony-0.40 test/test_timer.rb
polyphony-0.38 test/test_timer.rb
polyphony-0.36 test/test_timer.rb
polyphony-0.34 test/test_timer.rb
polyphony-0.33 test/test_timer.rb
polyphony-0.32 test/test_timer.rb
polyphony-0.31 test/test_timer.rb
polyphony-0.30 test/test_timer.rb
polyphony-0.29 test/test_timer.rb
polyphony-0.28 test/test_timer.rb
polyphony-0.27 test/test_timer.rb