summaryrefslogtreecommitdiff
path: root/test/scheduler/test_sleep.rb
blob: 0be760341e6bd26a0ccff4932fcf97c8cc8cbf51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'

class TestSchedulerSleep < Test::Unit::TestCase
  ITEMS = [0, 1, 2, 3, 4]

  def test_sleep
    items = []

    thread = Thread.new do
      scheduler = Scheduler.new
      Thread.current.scheduler = scheduler

      5.times do |i|
        Fiber do
          sleep(i/100.0)
          items << i
        end
      end

      # Should be 5 fibers waiting:
      assert_equal scheduler.waiting.size, 5
    end

    thread.join

    assert_equal ITEMS, items
  end
end