Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run all tests in a fiber #194

Merged
merged 1 commit into from
Apr 23, 2023
Merged

Run all tests in a fiber #194

merged 1 commit into from
Apr 23, 2023

Conversation

WyriHaximus
Copy link
Owner

@WyriHaximus WyriHaximus commented Apr 23, 2023

Since all tests are executed inside a fiber, there is a default timeout of 30 seconds. To lower or raise that timeout
this package comes with a TimeOut attribute. It can be set on the class and method level. When set on both the method level it takes priority over the class level.

<?php

declare(strict_types=1);

namespace WyriHaximus\Tests\AsyncTestUtilities;

use React\EventLoop\Loop;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\AsyncTestUtilities\TimeOut;

use function React\Async\async;
use function React\Async\await;
use function React\Promise\resolve;
use function React\Promise\Timer\sleep;
use function time;

#[TimeOut(0.3)]
final class AsyncTestCaseTest extends AsyncTestCase
{
    #[TimeOut(1)]
    public function testAllTestsAreRanInAFiber(): void
    {
        self::expectOutputString('ab');

        Loop::futureTick(async(static function (): void {
            echo 'a';
        }));

        await(sleep(1));

        echo 'b';
    }

    public function testExpectCallableExactly(): void
    {
        $callable = $this->expectCallableExactly(3);

        Loop::futureTick($callable);
        Loop::futureTick($callable);
        Loop::futureTick($callable);
    }

    public function testExpectCallableOnce(): void
    {
        Loop::futureTick($this->expectCallableOnce());
    }
}

Since all tests are executed inside a fiber, there is a default timeout of `30` seconds. To lower or raise that timeout
this package comes with a `TimeOut` attribute. It can be set on the class and method level. When set on both the method level it takes priority over the class level.

```php
<?php

declare(strict_types=1);

namespace WyriHaximus\Tests\AsyncTestUtilities;

use React\EventLoop\Loop;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\AsyncTestUtilities\TimeOut;

use function React\Async\async;
use function React\Async\await;
use function React\Promise\resolve;
use function React\Promise\Timer\sleep;
use function time;

#[TimeOut(0.3)]
final class AsyncTestCaseTest extends AsyncTestCase
{
    #[TimeOut(1)]
    public function testAllTestsAreRanInAFiber(): void
    {
        self::expectOutputString('ab');

        Loop::futureTick(async(static function (): void {
            echo 'a';
        }));

        await(sleep(1));

        echo 'b';
    }

    public function testExpectCallableExactly(): void
    {
        $callable = $this->expectCallableExactly(3);

        Loop::futureTick($callable);
        Loop::futureTick($callable);
        Loop::futureTick($callable);
    }

    public function testExpectCallableOnce(): void
    {
        Loop::futureTick($this->expectCallableOnce());
    }
}
```
@WyriHaximus WyriHaximus force-pushed the fun-all-tests-in-a-fiber branch from 6a884f5 to 5830b95 Compare April 23, 2023 12:06
@WyriHaximus WyriHaximus merged commit f19172b into master Apr 23, 2023
@WyriHaximus WyriHaximus deleted the fun-all-tests-in-a-fiber branch April 23, 2023 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant