Example #1
0
 /** @dataProvider valueDataProvider */
 public function testDelayGeneratorCreator($value)
 {
     static $index = 0;
     $this->given($timeout = 500, $current = 0, $this->function->microtime = function () use($timeout, &$current) {
         $value = $current;
         $current += $timeout / 1000;
         return $value;
     }, $creator = function ($limit, $value) {
         while ($limit-- > 0) {
             yield;
         }
         return (function () use($value) {
             yield;
             return $value;
         })();
     })->then->object($generator = testedClass::delay($timeout, $creator(3, $value)))->isInstanceOf(\generator::class)->variable(async\runtime::await($generator))->isIdenticalTo($value)->function('microtime')->wasCalledWithArguments(true)->exactly(++$index * 2);
 }
Example #2
0
function read($stream, callable $data = null, $length = null) : \generator
{
    if (is_resource($stream) === false) {
        throw new \invalidArgumentException();
    }
    $data = $data ?? function () {
    };
    do {
        $read = [$stream];
        $write = $except = null;
        $select = stream_select($read, $write, $except, 0);
        if ($select > 0) {
            $buffer = stream_get_contents($read[0], $length ?: -1);
            if ($buffer !== '') {
                runtime::await($data($buffer));
            }
        }
        $cancel = yield;
        $metadata = stream_get_meta_data($stream);
    } while (($metadata['eof'] === false || $metadata['unread_bytes'] > 0) && $cancel == false);
}
Example #3
0
 /** @dataProvider valueDataProvider */
 public function testSomeGeneratorCreators($value, $otherValue = null)
 {
     $this->given($otherValue = $otherValue ?? $value, $creator = function ($limit, $value) {
         while ($limit-- > 0) {
             yield;
         }
         return (function () use($value) {
             yield;
             return $value;
         })();
     })->then->object($generator = testedClass::some(1, $creator(5, $value), $creator(3, $otherValue)))->isInstanceOf(\generator::class)->variable(testedClass::await($generator))->isIdenticalTo([1 => $otherValue])->object($generator = testedClass::some(2, $creator(5, $value), $creator(3, $otherValue)))->isInstanceOf(\generator::class)->variable(testedClass::await($generator))->isIdenticalTo([$value, $otherValue]);
 }
Example #4
0
function fork(&$generators) : \generator
{
    return runtime::fork($generators);
}