aboutsummaryrefslogtreecommitdiffstats
path: root/test/fiber/test_sleep.rb
blob: bfb892e2c66093767260c2e79577bbe8f24a6a28 (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 TestFiberSleep < 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