1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00

Use AsyncTestCase for stream tests

This commit is contained in:
Niklas Keller 2020-05-17 21:41:42 +02:00 committed by Aaron Piotrowski
parent 704f87ccc8
commit 85b4707832
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
6 changed files with 226 additions and 289 deletions

View File

@ -3,14 +3,13 @@
namespace Amp\Test\Stream;
use Amp\AsyncGenerator;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Stream;
use Amp\Test\BaseTest;
class ConcatTest extends BaseTest
class ConcatTest extends AsyncTestCase
{
public function getArrays()
public function getArrays(): array
{
return [
[[\range(1, 3), \range(4, 6)], \range(1, 6)],
@ -27,17 +26,15 @@ class ConcatTest extends BaseTest
*/
public function testConcat(array $iterators, array $expected)
{
Loop::run(function () use ($iterators, $expected) {
$iterators = \array_map(function (array $iterator): Stream {
return Stream\fromIterable($iterator);
}, $iterators);
$iterators = \array_map(static function (array $iterator): Stream {
return Stream\fromIterable($iterator);
}, $iterators);
$iterator = Stream\concat($iterators);
$stream = Stream\concat($iterators);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
});
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
}
/**
@ -45,33 +42,37 @@ class ConcatTest extends BaseTest
*/
public function testConcatWithFailedStream()
{
Loop::run(function () {
$exception = new TestException;
$expected = \range(1, 6);
$generator = new AsyncGenerator(function (callable $yield) use ($exception) {
yield $yield(6); // Emit once before failing.
throw $exception;
});
$exception = new TestException;
$expected = \range(1, 6);
$generator = new AsyncGenerator(static function (callable $yield) use ($exception) {
yield $yield(6); // Emit once before failing.
throw $exception;
});
$iterator = Stream\concat([Stream\fromIterable(\range(1, 5)), $generator, Stream\fromIterable(\range(7, 10))]);
$stream = Stream\concat([
Stream\fromIterable(\range(1, 5)),
$generator,
Stream\fromIterable(\range(7, 10)),
]);
try {
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
try {
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->assertEmpty($expected);
});
$this->fail("The exception used to fail the stream should be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
$this->assertEmpty($expected);
}
public function testNonStream()
{
$this->expectException(\TypeError::class);
/** @noinspection PhpParamsInspection */
Stream\concat([1]);
}
}

View File

@ -3,7 +3,6 @@
namespace Amp\Test\Stream;
use Amp\AsyncGenerator;
use Amp\Loop;
use Amp\PHPUnit\TestException;
use Amp\Stream;
use Amp\StreamSource;
@ -13,44 +12,37 @@ class FilterTest extends BaseTest
{
public function testNoValuesEmitted()
{
$invoked = false;
Loop::run(function () use (&$invoked) {
$source = new StreamSource;
$source = new StreamSource;
$iterator = Stream\filter($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$stream = Stream\filter($source->stream(), $this->createCallback(0));
$this->assertInstanceOf(Stream::class, $iterator);
$source->complete();
$source->complete();
});
$this->assertFalse($invoked);
yield Stream\discard($stream);
}
public function testValuesEmitted()
{
Loop::run(function () {
$count = 0;
$values = [1, 2, 3];
$expected = [1, 3];
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$iterator = Stream\filter($generator, function ($value) use (&$count) {
++$count;
return $value & 1;
});
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
$count = 0;
$values = [1, 2, 3];
$expected = [1, 3];
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
$this->assertSame(3, $count);
});
$iterator = Stream\filter($generator, static function ($value) use (&$count) {
++$count;
return $value & 1;
});
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->assertSame(3, $count);
}
/**
@ -58,49 +50,34 @@ class FilterTest extends BaseTest
*/
public function testCallbackThrows()
{
Loop::run(function () {
$values = [1, 2, 3];
$exception = new TestException;
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$iterator = Stream\filter($generator, function () use ($exception) {
throw $exception;
});
try {
yield $iterator->continue();
$this->fail("The exception thrown from the filter callback should be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
$values = [1, 2, 3];
$exception = new TestException;
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$stream = Stream\filter($generator, static function () use ($exception) {
throw $exception;
});
$this->expectExceptionObject($exception);
yield $stream->continue();
}
public function testStreamFails()
{
Loop::run(function () {
$invoked = false;
$exception = new TestException;
$source = new StreamSource;
$exception = new TestException;
$source = new StreamSource;
$stream = Stream\filter($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$stream = Stream\filter($source->stream(), $this->createCallback(0));
$source->fail($exception);
$source->fail($exception);
try {
yield $stream->continue();
$this->fail("The exception used to fail the iterator should be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
}
$this->expectExceptionObject($exception);
$this->assertFalse($invoked);
});
yield $stream->continue();
}
}

View File

@ -4,92 +4,84 @@ namespace Amp\Test\Stream;
use Amp\Delayed;
use Amp\Failure;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Stream;
use Amp\Success;
use Amp\Test\BaseTest;
class FromIterableTest extends BaseTest
class FromIterableTest extends AsyncTestCase
{
const TIMEOUT = 10;
public function testSuccessfulPromises()
{
Loop::run(function () {
$expected = \range(1, 3);
$iterator = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]);
$expected = \range(1, 3);
$stream = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
});
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
}
public function testFailedPromises()
{
Loop::run(function () {
$exception = new \Exception;
$iterator = Stream\fromIterable([new Failure($exception), new Failure($exception)]);
$exception = new \Exception;
$iterator = Stream\fromIterable([new Failure($exception), new Failure($exception)]);
try {
yield $iterator->continue();
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
});
$this->expectExceptionObject($exception);
yield $iterator->continue();
}
public function testMixedPromises()
{
Loop::run(function () {
$exception = new TestException;
$expected = \range(1, 2);
$iterator = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
$exception = new TestException;
$expected = \range(1, 2);
$stream = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
try {
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->fail("A failed promise in the iterable should fail the iterator and be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
try {
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->fail("A failed promise in the iterable should fail the stream and be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
$this->assertEmpty($expected);
});
$this->assertEmpty($expected);
}
public function testPendingPromises()
{
Loop::run(function () {
$expected = \range(1, 4);
$iterator = Stream\fromIterable([new Delayed(30, 1), new Delayed(10, 2), new Delayed(20, 3), new Success(4)]);
$expected = \range(1, 4);
$stream = Stream\fromIterable([
new Delayed(30, 1),
new Delayed(10, 2),
new Delayed(20, 3),
new Success(4),
]);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
});
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
}
public function testTraversable()
{
Loop::run(function () {
$expected = \range(1, 4);
$generator = (function () {
foreach (\range(1, 4) as $value) {
yield $value;
}
})();
$iterator = Stream\fromIterable($generator);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
$expected = \range(1, 4);
$generator = (static function () {
foreach (\range(1, 4) as $value) {
yield $value;
}
})();
$this->assertEmpty($expected);
});
$stream = Stream\fromIterable($generator);
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
$this->assertEmpty($expected);
}
/**
@ -102,7 +94,7 @@ class FromIterableTest extends BaseTest
Stream\fromIterable($arg);
}
public function provideInvalidIteratorArguments()
public function provideInvalidIteratorArguments(): array
{
return [
[null],
@ -116,17 +108,15 @@ class FromIterableTest extends BaseTest
public function testInterval()
{
Loop::run(function () {
$count = 3;
$iterator = Stream\fromIterable(\range(1, $count), self::TIMEOUT);
$count = 3;
$stream = Stream\fromIterable(\range(1, $count), self::TIMEOUT);
$i = 0;
while (list($value) = yield $iterator->continue()) {
$this->assertSame(++$i, $value);
}
$i = 0;
while (list($value) = yield $stream->continue()) {
$this->assertSame(++$i, $value);
}
$this->assertSame($count, $i);
});
$this->assertSame($count, $i);
}
/**
@ -135,14 +125,12 @@ class FromIterableTest extends BaseTest
public function testSlowConsumer()
{
$count = 5;
Loop::run(function () use ($count) {
$iterator = Stream\fromIterable(\range(1, $count), self::TIMEOUT);
$stream = Stream\fromIterable(\range(1, $count), self::TIMEOUT);
for ($i = 0; list($value) = yield $iterator->continue(); ++$i) {
yield new Delayed(self::TIMEOUT * 2);
}
for ($i = 0; list($value) = yield $stream->continue(); ++$i) {
yield new Delayed(self::TIMEOUT * 2);
}
$this->assertSame($count, $i);
});
$this->assertSame($count, $i);
}
}

View File

@ -3,54 +3,44 @@
namespace Amp\Test\Stream;
use Amp\AsyncGenerator;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Stream;
use Amp\StreamSource;
use Amp\Test\BaseTest;
class MapTest extends BaseTest
class MapTest extends AsyncTestCase
{
public function testNoValuesEmitted()
{
$invoked = false;
Loop::run(function () use (&$invoked) {
$source = new StreamSource;
$source = new StreamSource;
$iterator = Stream\map($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
/** @noinspection PhpUnusedLocalVariableInspection */
$stream = Stream\map($source->stream(), $this->createCallback(0));
$this->assertInstanceOf(Stream::class, $iterator);
$source->complete();
});
$this->assertFalse($invoked);
$source->complete();
}
public function testValuesEmitted()
{
Loop::run(function () {
$count = 0;
$values = [1, 2, 3];
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$iterator = Stream\map($generator, function ($value) use (&$count) {
++$count;
return $value + 1;
});
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($values) + 1, $value);
$count = 0;
$values = [1, 2, 3];
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
$this->assertSame(3, $count);
});
$stream = Stream\map($generator, static function ($value) use (&$count) {
++$count;
return $value + 1;
});
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($values) + 1, $value);
}
$this->assertSame(3, $count);
}
/**
@ -58,50 +48,35 @@ class MapTest extends BaseTest
*/
public function testOnNextCallbackThrows()
{
Loop::run(function () {
$values = [1, 2, 3];
$exception = new TestException;
$values = [1, 2, 3];
$exception = new TestException;
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$iterator = Stream\map($generator, function () use ($exception) {
throw $exception;
});
try {
yield $iterator->continue();
$this->fail("The exception thrown from the map callback should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
foreach ($values as $value) {
yield $yield($value);
}
});
$stream = Stream\map($generator, static function () use ($exception) {
throw $exception;
});
$this->expectExceptionObject($exception);
yield $stream->continue();
}
public function testStreamFails()
{
Loop::run(function () {
$invoked = false;
$exception = new TestException;
$source = new StreamSource;
$exception = new TestException;
$source = new StreamSource;
$iterator = Stream\map($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$iterator = Stream\map($source->stream(), $this->createCallback(0));
$source->fail($exception);
$source->fail($exception);
try {
yield $iterator->continue();
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
}
$this->expectExceptionObject($exception);
$this->assertFalse($invoked);
});
yield $iterator->continue();
}
}

View File

@ -4,14 +4,13 @@ namespace Amp\Test\Stream;
use Amp\AsyncGenerator;
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Stream;
use Amp\Test\BaseTest;
class MergeTest extends BaseTest
class MergeTest extends AsyncTestCase
{
public function getArrays()
public function getArrays(): array
{
return [
[[\range(1, 3), \range(4, 6)], [1, 4, 2, 5, 3, 6]],
@ -23,22 +22,20 @@ class MergeTest extends BaseTest
/**
* @dataProvider getArrays
*
* @param array $iterators
* @param array $streams
* @param array $expected
*/
public function testMerge(array $iterators, array $expected)
public function testMerge(array $streams, array $expected)
{
Loop::run(function () use ($iterators, $expected) {
$iterators = \array_map(function (array $iterator): Stream {
return Stream\fromIterable($iterator);
}, $iterators);
$streams = \array_map(static function (array $iterator): Stream {
return Stream\fromIterable($iterator);
}, $streams);
$iterator = Stream\merge($iterators);
$stream = Stream\merge($streams);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
});
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
}
/**
@ -46,30 +43,28 @@ class MergeTest extends BaseTest
*/
public function testMergeWithDelayedYields()
{
Loop::run(function () {
$iterators = [];
$values1 = [new Delayed(10, 1), new Delayed(50, 2), new Delayed(70, 3)];
$values2 = [new Delayed(20, 4), new Delayed(40, 5), new Delayed(60, 6)];
$expected = [1, 4, 5, 2, 6, 3];
$streams = [];
$values1 = [new Delayed(10, 1), new Delayed(50, 2), new Delayed(70, 3)];
$values2 = [new Delayed(20, 4), new Delayed(40, 5), new Delayed(60, 6)];
$expected = [1, 4, 5, 2, 6, 3];
$iterators[] = new AsyncGenerator(function (callable $yield) use ($values1) {
foreach ($values1 as $value) {
yield $yield(yield $value);
}
});
$iterators[] = new AsyncGenerator(function (callable $yield) use ($values2) {
foreach ($values2 as $value) {
yield $yield(yield $value);
}
});
$iterator = Stream\merge($iterators);
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
$streams[] = new AsyncGenerator(function (callable $yield) use ($values1) {
foreach ($values1 as $value) {
yield $yield(yield $value);
}
});
$streams[] = new AsyncGenerator(function (callable $yield) use ($values2) {
foreach ($values2 as $value) {
yield $yield(yield $value);
}
});
$stream = Stream\merge($streams);
while (list($value) = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
}
}
/**
@ -77,28 +72,30 @@ class MergeTest extends BaseTest
*/
public function testMergeWithFailedStream()
{
Loop::run(function () {
$exception = new TestException;
$generator = new AsyncGenerator(function (callable $yield) use ($exception) {
yield $yield(1); // Emit once before failing.
throw $exception;
});
$iterator = Stream\merge([$generator, Stream\fromIterable(\range(1, 5))]);
try {
while (yield $iterator->continue()) ;
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
$exception = new TestException;
$generator = new AsyncGenerator(static function (callable $yield) use ($exception) {
yield $yield(1); // Emit once before failing.
throw $exception;
});
$stream = Stream\merge([$generator, Stream\fromIterable(\range(1, 5))]);
try {
/** @noinspection PhpStatementHasEmptyBodyInspection */
while (yield $stream->continue()) {
;
}
$this->fail("The exception used to fail the stream should be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
}
public function testNonStream()
{
$this->expectException(\TypeError::class);
/** @noinspection PhpParamsInspection */
Stream\merge([1]);
}
}

View File

@ -2,21 +2,20 @@
namespace Amp\Test\Stream;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Stream;
use Amp\Test\BaseTest;
use function Amp\Promise\wait;
class ToArrayTest extends BaseTest
class ToArrayTest extends AsyncTestCase
{
public function testNonEmpty()
{
$iterator = Stream\fromIterable(["abc", "foo", "bar"], 5);
$this->assertSame(["abc", "foo", "bar"], wait(Stream\toArray($iterator)));
$stream = Stream\fromIterable(["abc", "foo", "bar"], 5);
$this->assertSame(["abc", "foo", "bar"], yield Stream\toArray($stream));
}
public function testEmpty()
{
$iterator = Stream\fromIterable([], 5);
$this->assertSame([], wait(Stream\toArray($iterator)));
$stream = Stream\fromIterable([], 5);
$this->assertSame([], yield Stream\toArray($stream));
}
}