File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change 17
17
module Async
18
18
# Handles scheduling of fibers. Implements the fiber scheduler interface.
19
19
class Scheduler < Node
20
+ class ClosedError < RuntimeError
21
+ def initialize ( message = "Scheduler is closed!" )
22
+ super
23
+ end
24
+ end
25
+
20
26
# Whether the fiber scheduler is supported.
21
27
# @public Since `stable-v1`.
22
28
def self . supported?
@@ -233,7 +239,7 @@ def run_once(timeout = nil)
233
239
234
240
# Run the reactor until all tasks are finished. Proxies arguments to {#async} immediately before entering the loop, if a block is provided.
235
241
def run ( ...)
236
- Kernel ::raise RuntimeError , 'Reactor has been closed' if @selector . nil?
242
+ Kernel ::raise ClosedError if @selector . nil?
237
243
238
244
initial_task = self . async ( ...) if block_given?
239
245
@@ -260,6 +266,8 @@ def run(...)
260
266
# @returns [Task] The task that was scheduled into the reactor.
261
267
# @deprecated With no replacement.
262
268
def async ( *arguments , **options , &block )
269
+ Kernel ::raise ClosedError if @selector . nil?
270
+
263
271
task = Task . new ( Task . current? || self , **options , &block )
264
272
265
273
# I want to take a moment to explain the logic of this.
Original file line number Diff line number Diff line change @@ -59,6 +59,12 @@ def initialize(message = "execution expired")
59
59
#
60
60
# @public Since `stable-v1`.
61
61
class Task < Node
62
+ class FinishedError < RuntimeError
63
+ def initialize ( message = "Cannot create child task within a task that has finished execution!" )
64
+ super
65
+ end
66
+ end
67
+
62
68
# @deprecated With no replacement.
63
69
def self . yield
64
70
Fiber . scheduler . transfer
@@ -164,7 +170,7 @@ def run(*arguments)
164
170
165
171
# Run an asynchronous task as a child of the current task.
166
172
def async ( *arguments , **options , &block )
167
- raise "Cannot create child task within a task that has finished execution!" if self . finished?
173
+ raise FinishedError if self . finished?
168
174
169
175
task = Task . new ( self , **options , &block )
170
176
Original file line number Diff line number Diff line change 267
267
expect ( reactor . to_s ) . to be =~ /stopped/
268
268
end
269
269
end
270
+
271
+ it "validates scheduler assignment" do
272
+ # Assign the scheduler:
273
+ reactor = self . reactor
274
+
275
+ # Close the previous scheduler:
276
+ Async { }
277
+
278
+ expect do
279
+ # The reactor is closed:
280
+ reactor . async { }
281
+ end . to raise_exception ( Async ::Scheduler ::ClosedError )
282
+ end
270
283
end
You can’t perform that action at this time.
0 commit comments