EmbeddedEventLoop
public final class EmbeddedEventLoop : EventLoop
An EventLoop
that is embedded in the current running context with no external
control.
Unlike more complex EventLoop
s, such as SelectableEventLoop
, the EmbeddedEventLoop
has no proper eventing mechanism. Instead, reads and writes are fully controlled by the
entity that instantiates the EmbeddedEventLoop
. This property makes EmbeddedEventLoop
of limited use for many application purposes, but highly valuable for testing and other
kinds of mocking.
Time is controllable on an EmbeddedEventLoop
. It begins at NIODeadline.uptimeNanoseconds(0)
and may be advanced by a fixed amount by using advanceTime(by:)
, or advanced to a point in
time with advanceTime(to:)
.
Warning
UnlikeSelectableEventLoop
, EmbeddedEventLoop
is not thread-safe. This
is because it is intended to be run in the thread that instantiated it. Users are
responsible for ensuring they never call into the EmbeddedEventLoop
in an
unsynchronized fashion.
-
Declaration
Swift
public var inEventLoop: Bool { get }
-
Initialize a new
EmbeddedEventLoop
.Declaration
Swift
public init()
-
Declaration
Swift
@discardableResult public func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T>
-
Declaration
Swift
@discardableResult public func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T>
-
On an
EmbeddedEventLoop
,execute
will simply usescheduleTask
with a deadline of now. This means thattask
will be run the next time you callEmbeddedEventLoop.run
.Declaration
Swift
public func execute(_ task: @escaping () -> Void)
-
Run all tasks that have previously been submitted to this
EmbeddedEventLoop
, either by callingexecute
or events that have been enqueued usingscheduleTask
/scheduleRepeatedTask
/scheduleRepeatedAsyncTask
and whose deadlines have expired.Seealso
EmbeddedEventLoop.advanceTime
.Declaration
Swift
public func run()
-
Runs the event loop and moves “time” forward by the given amount, running any scheduled tasks that need to be run.
Declaration
Swift
public func advanceTime(by increment: TimeAmount)
-
Runs the event loop and moves “time” forward to the given point in time, running any scheduled tasks that need to be run.
Note
Ifdeadline
is before the current time, the current time will not be advanced.Declaration
Swift
public func advanceTime(to deadline: NIODeadline)
-
See
EventLoop.shutdownGracefully
Declaration
Swift
public func shutdownGracefully(queue: DispatchQueue, _ callback: @escaping (Error?) -> Void)