File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ module FiberSpecs
2+
3+ class LoggingScheduler
4+ attr_reader :events
5+ def initialize
6+ @events = [ ]
7+ end
8+
9+ def block ( *args )
10+ @events << { event : :block , fiber : Fiber . current , args : args }
11+ Fiber . yield
12+ end
13+
14+ def io_wait ( *args )
15+ @events << { event : :io_wait , fiber : Fiber . current , args : args }
16+ Fiber . yield
17+ end
18+
19+ def kernel_sleep ( *args )
20+ @events << { event : :kernel_sleep , fiber : Fiber . current , args : args }
21+ Fiber . yield
22+ end
23+
24+ def unblock ( *args )
25+ @events << { event : :unblock , fiber : Fiber . current , args : args }
26+ Fiber . yield
27+ end
28+ end
29+
30+ end
Original file line number Diff line number Diff line change 11require_relative '../../spec_helper'
2+ require_relative '../fiber/fixtures/scheduler'
23
34describe "Kernel#sleep" do
45 it "is a private method" do
@@ -73,6 +74,40 @@ def o.divmod(*); [0, 0.001]; end
7374 t . value . should == 5
7475 end
7576 end
77+
78+ context "Kernel.sleep with Fiber scheduler" do
79+ before :each do
80+ Fiber . set_scheduler ( FiberSpecs ::LoggingScheduler . new )
81+ end
82+
83+ after :each do
84+ Fiber . set_scheduler ( nil )
85+ end
86+
87+ it "calls the scheduler without arguments when no duration is given" do
88+ sleeper = Fiber . new ( blocking : false ) do
89+ sleep
90+ end
91+ sleeper . resume
92+ Fiber . scheduler . events . should == [ { event : :kernel_sleep , fiber : sleeper , args : [ ] } ]
93+ end
94+
95+ it "calls the scheduler with the given duration" do
96+ sleeper = Fiber . new ( blocking : false ) do
97+ sleep ( 0.01 )
98+ end
99+ sleeper . resume
100+ Fiber . scheduler . events . should == [ { event : :kernel_sleep , fiber : sleeper , args : [ 0.01 ] } ]
101+ end
102+
103+ it "does not call the scheduler if the fiber is blocking" do
104+ sleeper = Fiber . new ( blocking : true ) do
105+ sleep ( 0.01 )
106+ end
107+ sleeper . resume
108+ Fiber . scheduler . events . should == [ ]
109+ end
110+ end
76111end
77112
78113describe "Kernel.sleep" do
You can’t perform that action at this time.
0 commit comments