<commit_before><?php

declare(strict_types=1);

namespace GrumPHPTest\Unit\Runner\Promise;

use Amp\CancelledException;
use Amp\Delayed;
use Amp\Failure;
use Amp\LazyPromise;
use Amp\Promise;
use Amp\Success;
use GrumPHP\Runner\Promise\MultiPromise;
use PHPUnit\Framework\TestCase;
use function Amp\Promise\wait;

class MultiPromiseTest extends TestCase
{
    use LoopResettingTrait;

    /** @test */
    public function it_does_not_cancel_if_all_good(): void
    {
        [$errors, $results] = wait(MultiPromise::cancelable(
            $this->wrapLazyPromises(
                new Delayed(10, new Success(1)),
                new Delayed(20, new Success(2))
            ),
            function () {
                return false;
            }
        ));

        self::assertSame([], $errors);
        self::assertSame([1, 2], $results);
    }

    /** @test */
    public function it_cancels_on_first_bad_result(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(10, new Success(1)),
                    new Delayed(20, new Success(2)),
                    new Delayed(30, new Success(3))
                ),
                function () {
                    return true;
                }
            ));

            self::assertCount(2, $errors);
            self::assertInstanceOf(CancelledException::class, $errors[1]);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([1], $results);
        });
    }

    /** @test */
    public function it_cancels_on_conditional_bad_result(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(10, new Success(1)),
                    new Delayed(20, new Success(2)),
                    new Delayed(30, new Success(3))
                ),
                function ($index): bool {
                    return $index == 2;
                }
            ));

            self::assertCount(1, $errors);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([1, 2], $results);
        });
    }

    /** @test */
    public function it_cancels_on_exception_thrown(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(10, new Failure(new \Exception('1'))),
                    new Delayed(20, new Failure(new \Exception('2'))),
                    new Delayed(30, new Failure(new \Exception('3')))
                ),
                function ($index): bool {
                    return false;
                }
            ));

            self::assertCount(3, $errors);
            self::assertInstanceOf(\Exception::class, $errors[0]);
            self::assertInstanceOf(CancelledException::class, $errors[1]);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([], $results);
        });
    }

    private function wrapLazyPromises(Promise ... $promises)
    {
        return array_map(
            static function (Promise $promise) {
                return new LazyPromise(function () use ($promise) {
                    return $promise;
                });
            },
            $promises
        );
    }
}
<commit_msg>delay times for CIs<commit_after><?php

declare(strict_types=1);

namespace GrumPHPTest\Unit\Runner\Promise;

use Amp\CancelledException;
use Amp\Delayed;
use Amp\Failure;
use Amp\LazyPromise;
use Amp\Promise;
use Amp\Success;
use GrumPHP\Runner\Promise\MultiPromise;
use PHPUnit\Framework\TestCase;
use function Amp\Promise\wait;

class MultiPromiseTest extends TestCase
{
    use LoopResettingTrait;

    /** @test */
    public function it_does_not_cancel_if_all_good(): void
    {
        [$errors, $results] = wait(MultiPromise::cancelable(
            $this->wrapLazyPromises(
                new Delayed(100, new Success(1)),
                new Delayed(200, new Success(2))
            ),
            function () {
                return false;
            }
        ));

        self::assertSame([], $errors);
        self::assertSame([1, 2], $results);
    }

    /** @test */
    public function it_cancels_on_first_bad_result(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(100, new Success(1)),
                    new Delayed(200, new Success(2)),
                    new Delayed(300, new Success(3))
                ),
                function () {
                    return true;
                }
            ));

            self::assertCount(2, $errors);
            self::assertInstanceOf(CancelledException::class, $errors[1]);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([1], $results);
        });
    }

    /** @test */
    public function it_cancels_on_conditional_bad_result(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(100, new Success(1)),
                    new Delayed(200, new Success(2)),
                    new Delayed(300, new Success(3))
                ),
                function ($index): bool {
                    return $index == 2;
                }
            ));

            self::assertCount(1, $errors);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([1, 2], $results);
        });
    }

    /** @test */
    public function it_cancels_on_exception_thrown(): void
    {
        $this->safelyRunAsync(function () {
            [$errors, $results] = wait(MultiPromise::cancelable(
                $this->wrapLazyPromises(
                    new Delayed(100, new Failure(new \Exception('1'))),
                    new Delayed(200, new Failure(new \Exception('2'))),
                    new Delayed(300, new Failure(new \Exception('3')))
                ),
                function ($index): bool {
                    return false;
                }
            ));

            self::assertCount(3, $errors);
            self::assertInstanceOf(\Exception::class, $errors[0]);
            self::assertInstanceOf(CancelledException::class, $errors[1]);
            self::assertInstanceOf(CancelledException::class, $errors[2]);

            self::assertSame([], $results);
        });
    }

    private function wrapLazyPromises(Promise ... $promises)
    {
        return array_map(
            static function (Promise $promise) {
                return new LazyPromise(function () use ($promise) {
                    return $promise;
                });
            },
            $promises
        );
    }
}

{'fraction_non_alphanumeric': 0.1278500489578962, 'fraction_numerical': 0.015246887676598125, 'mean_word_length': 1.7436684574059862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5517703', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1922, 'n_words': 363}