class DriverRuntime

Defined at line 43 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.h

|DriverRuntime| is a RAII client to the driver runtime for a unit test. It can be used to

start background dispatchers, and run the foreground dispatcher. On destruction it will shutdown

all created dispatchers, and reset the driver runtime.

There can only be one active instance of this class during a test case. The instance is commonly

the first member of a test fixture class. After constructing an instance of |DriverRuntime|,

other code may get the instance via the static |GetInstance| accessor.

# Thread safety

This class is thread-unsafe. Instances must be created and used from the unit test's main thread.

The exception to this is the |Quit| and |ResetQuit| functions which can be called from any

thread.

Public Methods

void DriverRuntime ()

Starts the driver runtime environment. If the env fails to start, this will throw an assert.

This will also create and attach a foreground driver dispatcher to the current thread.

Foreground dispatchers will not be ran in the background on the driver runtime's managed

thread pool, but instead must be run using the Run* methods.

Defined at line 39 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void ~DriverRuntime ()

Resets the driver runtime environment.

Defined at line 47 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

DriverRuntime * GetInstance ()

Gets the active instance of the DriverRuntime. The instance is stored in thread_local storage.

Defined at line 53 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

fdf::UnownedSynchronizedDispatcher GetForegroundDispatcher ()

Returns the current foreground driver dispatcher.

Defined at line 55 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

fdf::UnownedSynchronizedDispatcher StartBackgroundDispatcher ()

Starts a background driver dispatcher and returns it. Background dispatchers are automatically

ran on the managed thread pool inside the driver runtime. Objects that live on background

dispatchers are not safe to access directly from a test, but instead must be wrapped inside of

an |async_patterns::TestDispatcherBound|.

Defined at line 66 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void ShutdownAllDispatchers (fdf_dispatcher_t * dut_initial_dispatcher)

Shuts down all of the driver dispatchers that exist. This will return when all shutdowns

have completed. If this has already been done returns immediately.

|dut_initial_dispatcher| will be set as the current dispatcher context before this returns

to help with synchronization_checkers inside of the driver during teardown afterwards.

It should be the same dispatcher that the dut (driver under test) was created with.

If a nullptr is given then there will not be a current dispatcher context after this returns.

Defined at line 75 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void ResetForegroundDispatcher (fit::closure post_shutdown)

Shuts down the foreground dispatcher and runs the given closure afterwards in the context of

the dispatcher. Creates a new foreground dispatcher to replace it.

Returns when the shutdown has completed fully and the new dispatcher is created.

Defined at line 114 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void ShutdownBackgroundDispatcher (fdf_dispatcher_t * dispatcher, fit::closure post_shutdown)

Shuts down the given dispatcher and runs the given closure afterwards in the context of

the dispatcher. Returns when the shutdown has completed fully.

Defined at line 140 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

template <typename Result>
Result RunToCompletion (AsyncTask<Result> async_task)

Runs the foreground dispatcher until the |task| has completed.

Returns the result of the |task| when complete.

Defined at line 143 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.h

template <typename Callable>
auto PerformBlockingWork (Callable && work)

Runs the foreground dispatcher while spawning a new thread to perform |work|, and

returns the result of calling |work|. Runs the foreground dispatcher until |work| returns.

|Callable| should take zero arguments.

Defined at line 160 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.h

void Run ()

Runs the foreground dispatcher until it is quit.

Defined at line 175 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

bool RunWithTimeout (zx::duration timeout)

Runs the foreground dispatcher for at most |timeout|. Returns |true| if the timeout has been

reached before it is quit.

Defined at line 181 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void RunUntil (fit::function<bool ()> condition, zx::duration step)

Runs the foreground dispatcher until the condition returns true.

Providing this for parity with async::Loop. Avoid this when possible as it is a bad pattern,

the condition callback may have data race and use after free problems.

Prefer to use the |Run| and |Quit| combination, or the |RunUntilIdle| call.

|step| specifies the interval at which this method should wake up to poll

|condition|. If |step| is |zx::duration::infinite()|, no polling timer is

set. Instead, the condition is checked initially and after anything happens

on the dispatcher (e.g. a task executes). This is useful when the caller knows

that |condition| will be made true by a task running on the dispatcher. This will

generally be the case unless |condition| is made true on a different

thread.

Defined at line 212 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

bool RunWithTimeoutOrUntil (fit::function<bool ()> condition, zx::duration timeout, zx::duration step)

Runs the foreground dispatcher until the condition returns true or the timeout is reached.

Returns |true| if the condition was met, and |false| if the timeout was

reached.

Providing this for parity with async::Loop. Avoid this when possible as it is a bad pattern,

the condition callback may have data race and use after free problems.

Prefer to use the |RunWithTimeout| and |Quit| combination, or the |RunUntilIdle| call.

|step| specifies the interval at which this method should wake up to poll

|condition|. If |step| is |zx::duration::infinite()|, no polling timer is

set. Instead, the condition is checked initially and after anything happens

on the dispatcher (e.g. a task executes). This is useful when the caller knows

that |condition| will be made true by a task running on the dispatcher. This will

generally be the case unless |condition| is made true on a different

thread.

Defined at line 217 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

void RunUntilIdle ()

Runs the foreground dispatcher until idle.

Defined at line 242 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

template <typename PromiseType>
typename PromiseType::result_type RunPromise (PromiseType promise)

Internal template implementation details

This is mirrored from:

`//sdk/lib/async-loop-testing/cpp/include/lib/async-loop/testing/cpp/real_loop.h`

========================================

Defined at line 245 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.h

void Quit ()

Quits the foreground dispatcher.

Active invocations of |Run| will eventually terminate upon completion of their

current unit of work.

Subsequent calls to |Run| will return immediately until |ResetQuit| is called.

Defined at line 247 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

zx_status_t ResetQuit ()

Resets the quit state of the foreground dispatcher so that it can be restarted

using |Run|.

This function must only be called when the foreground dispatcher is not running.

The caller must ensure all active invocations of |Run| have terminated before

resetting the quit state.

Returns |ZX_OK| if the foreground dispatcher's quit state was correctly reset.

Returns |ZX_ERR_BAD_STATE| if the foreground dispatcher was shutting down or if

it was currently actively running

Defined at line 249 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

fit::closure QuitClosure ()

Creates a closure that calls |Quit|.

Defined at line 251 of file ../../sdk/lib/driver/testing/cpp/driver_runtime.cc

Records