<commit_before><?php

/*
 * This file is part of Icicle, a library for writing asynchronous code in PHP using coroutines built with awaitables.
 *
 * @copyright 2014-2015 <NAME>. All rights reserved.
 * @license MIT See the LICENSE file that was distributed with this source code for more information.
 */

namespace Icicle\Tests\Promise;

use Exception;
use Icicle\Awaitable;
use Icicle\Loop;
use Icicle\Loop\SelectLoop;
use Icicle\Tests\TestCase;

class RetryTest extends TestCase
{
    const TIMEOUT = 0.1;
    
    public function setUp()
    {
        Loop\loop(new SelectLoop());
    }
    
    public function testPromisorReturningScalar()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorReturnsFulfilledPromise()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorReturnsPendingPromise()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value)->delay(self::TIMEOUT);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorThrowsException()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            throw $exception;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    public function testPromiseRejectingCallsOnRejected()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            return Awaitable\reject($exception);
        };
        
        $onRejected = function ($value) use ($exception) {
            $this->assertSame($exception, $value);
            return false;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $onRejected)
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    /**
     * @depends testPromiseRejectingCallsOnRejected
     */
    public function testPendingPromiseRejectingCallsOnRejected()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            $promise = new Awaitable\Promise(function () {});
            return $promise->timeout(self::TIMEOUT, $exception);
        };
        
        $onRejected = function ($value) use ($exception) {
            $this->assertSame($exception, $value);
            return false;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $onRejected)
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    /**
     * @depends testPromiseRejectingCallsOnRejected
     */
    public function testOnRejectedThrowingRejectsPromise()
    {
        $exception = new Exception();
        
        $promisor = function () {
            return Awaitable\reject(new Exception());
        };
        <commit_msg>Fix retry() test using timeout()<commit_after><?php

/*
 * This file is part of Icicle, a library for writing asynchronous code in PHP using coroutines built with awaitables.
 *
 * @copyright 2014-2015 <NAME>. All rights reserved.
 * @license MIT See the LICENSE file that was distributed with this source code for more information.
 */

namespace Icicle\Tests\Promise;

use Exception;
use Icicle\Awaitable;
use Icicle\Loop;
use Icicle\Loop\SelectLoop;
use Icicle\Tests\TestCase;

class RetryTest extends TestCase
{
    const TIMEOUT = 0.1;
    
    public function setUp()
    {
        Loop\loop(new SelectLoop());
    }
    
    public function testPromisorReturningScalar()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorReturnsFulfilledPromise()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorReturnsPendingPromise()
    {
        $value = 'testing';
        
        $promisor = function () use ($value) {
            return Awaitable\resolve($value)->delay(self::TIMEOUT);
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($value));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($callback);
        
        Loop\run();
    }
    
    public function testPromisorThrowsException()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            throw $exception;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $this->createCallback(0))
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    public function testPromiseRejectingCallsOnRejected()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            return Awaitable\reject($exception);
        };
        
        $onRejected = function ($value) use ($exception) {
            $this->assertSame($exception, $value);
            return false;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $onRejected)
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    /**
     * @depends testPromiseRejectingCallsOnRejected
     */
    public function testPendingPromiseRejectingCallsOnRejected()
    {
        $exception = new Exception();
        
        $promisor = function () use ($exception) {
            $promise = new Awaitable\Promise(function () {});
            return $promise->timeout(self::TIMEOUT, function () use ($exception) {
                throw $exception;
            });
        };
        
        $onRejected = function ($value) use ($exception) {
            $this->assertSame($exception, $value);
            return false;
        };
        
        $callback = $this->createCallback(1);
        $callback->method('__invoke')
                 ->with($this->identicalTo($exception));
        
        Awaitable\retry($promisor, $onRejected)
            ->done($this->createCallback(0), $callback);
        
        Loop\run();
    }
    
    /**
     * @depends testPromiseRejectingCallsOnRejected
     */
    public function testOnRejectedThrowingRejectsPromise()
    {
        $exception = new Exception();
        
        $promisor = function () {
            return Awaitable\reject(new Exception());
        };
        
{'fraction_non_alphanumeric': 0.13059575900370246, 'fraction_numerical': 0.005161000785369685, 'mean_word_length': 1.886658031088083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, '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': '4985901', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2419, 'n_words': 454}