Core\Test::assertCompletes
Core\Test::assertCompletes(
callable(): mixed $body,
{ within: Core\Time\Duration } $settings,
{message?: string},
): voidCore\Test::assertCompletes() checks that a piece of code finishes within a time limit. You pass the code as a function with no parameters, and the limit as the option within, for example 5s.
The check works only in a test with a fixed clock, which #[Test(at: ...)] sets. It moves that clock forward by within first, and then calls the function. So a loop that waits for a deadline ends at once, and the test does not wait for real time. Without a fixed clock, the check throws a LogicError and does not call the function.
The check fails with a Core\Test\Failure when the function returns but a task it started is still running. An exception from the function is not changed. The test reports it as it is.
The examples below show a job with a limit, the error without a fixed clock, and a test of a retry loop that ends at a deadline.
Parameters
$bodycallable(): mixedThe work to run. It is called once, on the test's own task. Its return value is ignored.$settings{ within: Core\Time\Duration }The time budget. You must always give it, because no default duration fits every caller.withinCore\Time\DurationHow far the test's clock moves before$bodyruns. A retry, a backoff or a timeout inside$bodythen does not wait.Core\Test::advancemoves the same clock. The move stays: after this call, the test reads the moved clock.
Options
messagestringoptionalYour own text, added at the start of the failure message, as on every other check.
Return value
Returns void. Nothing. If $body returns while a task it started is still running, the call throws an error that names the budget. The check runs when $body returns.
Errors
Core\Test\Failure$bodyreturned while a task it started was still running. The message gives the number of tasks and thewithinvalue they had.LogicErrorThe test has noat:, so there is no fixed clock to move.Core\Test::advancethrows the same error for the same reason.RuntimeErrorThe moved clock is outside the range anInstantcan have.Core\Test::advancethrows the same error.
Examples
Check that a job finishes
<?nvs// Shows how to check that a job finishes within a time limit.
use Core\Test;
class Report{ public static function build(array<int> $sales): int { int $total = 0; foreach ($sales as int $amount) { $total = $total + $amount; } return $total; }}
class ReportTest{ // The check needs a fixed clock, so the test sets one with `at:`. #[Test(at: '2026-03-01T09:00:00Z')] public function theReportFinishesInFiveSeconds(): void { // `within` is the time limit. The check moves the clock forward by 5 seconds, // then runs the function. It fails if the function leaves a task still running. Test::assertCompletes(fn(): void => { Report::build([120, 80, 45]); }, { within: 5s }); }}
// `nvs test` runs the test above. This line runs under `nvs run` and prints `245`.echo Report::build([120, 80, 45]), "\n";245A check without a fixed clock
<?nvs// Shows what happens when the program has no fixed clock. Only a test with// `#[Test(at: ...)]` has a fixed clock, and this program runs under `nvs run`.
// The check throws a `LogicError` before it calls the function.// So "the function ran" is not printed.try { Core\Test::assertCompletes(fn(): void => { echo "the function ran\n"; }, { within: 50ms });} catch (LogicError $error) { echo $error->message, "\n";}Core\Test::assertCompletes(): this test declared no `at:`, so it has no fixed clock to spend `within` againstTest a retry without waiting
<?nvs// Shows a test of a retry loop that gives up after 30 seconds. The test does not wait 30 seconds.
use Core\Test;use Core\Time;use Core\Time\Instant;
class Payment{ // Tries to send the payment until it works or the deadline has passed. // Returns how many times it tried. public static function sendUntil(Instant $deadline, bool $gatewayIsUp): int { int $tries = 0; while (Time::now()->compareTo($deadline) < 0) { $tries = $tries + 1; if ($gatewayIsUp) { return $tries; } } return $tries; }}
class PaymentTest{ #[Test(at: '2026-03-01T09:00:00Z')] public function itGivesUpWhenTheGatewayIsDown(): void { Instant $deadline = Time::now()->plus(30s); // The check moves the clock forward by 30 seconds before the loop starts. // The deadline has already passed, so the loop ends at once. Test::assertCompletes( fn(): void => { Payment::sendUntil($deadline, false); }, { within: 30s }, ); }}
// `nvs test` runs the test above. This line runs under `nvs run`. The gateway// is up, so the first try works and this prints `1`.echo Payment::sendUntil(Time::now()->plus(30s), true), "\n";1