mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Remove outdated / unused code
This commit is contained in:
parent
bfb2cfb9c6
commit
2917687e72
@ -40,11 +40,8 @@
|
||||
"concurrent-php/async-api": "dev-master"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/phpunit-util": "dev-ext-async",
|
||||
"react/promise": "^2",
|
||||
"friendsofphp/php-cs-fixer": "^2.3",
|
||||
"phpunit/phpunit": "^6.0.9",
|
||||
"phpstan/phpstan": "^0.8.5"
|
||||
"friendsofphp/php-cs-fixer": "^2.13",
|
||||
"phpunit/phpunit": "^6.0.9"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
/**
|
||||
* Will be thrown in case an operation is cancelled.
|
||||
*
|
||||
* @see Token
|
||||
* @see CancellationTokenSource
|
||||
*/
|
||||
class CancelledException extends \Exception
|
||||
{
|
||||
public function __construct(string $message = "The operation was cancelled", \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 0, $previous);
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
/**
|
||||
* A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.
|
||||
*
|
||||
* Instead of writing
|
||||
*
|
||||
* ```php
|
||||
* if ($token) {
|
||||
* $token->throwIfRequested();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* potentially multiple times, it allows writing
|
||||
*
|
||||
* ```php
|
||||
* $token = $token ?? new NullCancellationToken;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* $token->throwIfRequested();
|
||||
* ```
|
||||
*
|
||||
* instead.
|
||||
*/
|
||||
final class NullToken implements Token
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
return "null-token";
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function unsubscribe(string $id): void
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function throwIfRequested(): void
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
use Amp\Loop;
|
||||
use Amp\TimeoutException;
|
||||
|
||||
/**
|
||||
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
||||
*/
|
||||
final class TimeoutToken implements Token
|
||||
{
|
||||
/** @var string */
|
||||
private $watcher;
|
||||
|
||||
/** @var Token */
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* @param int $timeout Milliseconds until cancellation is requested.
|
||||
*/
|
||||
public function __construct(int $timeout)
|
||||
{
|
||||
$source = new TokenSource;
|
||||
$this->token = $source->getToken();
|
||||
|
||||
$this->watcher = Loop::delay($timeout, static function () use ($source) {
|
||||
$source->cancel(new TimeoutException);
|
||||
});
|
||||
|
||||
Loop::unreference($this->watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the delay watcher.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
Loop::cancel($this->watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
return $this->token->subscribe($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function unsubscribe(string $id): void
|
||||
{
|
||||
$this->token->unsubscribe($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return $this->token->isRequested();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function throwIfRequested(): void
|
||||
{
|
||||
$this->token->throwIfRequested();
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
/**
|
||||
* Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests.
|
||||
*/
|
||||
interface Token
|
||||
{
|
||||
/**
|
||||
* Subscribes a new handler to be invoked on a cancellation request.
|
||||
*
|
||||
* This handler might be invoked immediately in case the token has already been cancelled. Returned generators will
|
||||
* automatically be run as coroutines. Any unhandled exceptions will be throw into the event loop.
|
||||
*
|
||||
* @param callable $callback Callback to be invoked on a cancellation request. Will receive a `CancelledException`
|
||||
* as first argument that may be used to fail the operation's promise.
|
||||
*
|
||||
* @return string Identifier that can be used to cancel the subscription.
|
||||
*/
|
||||
public function subscribe(callable $callback): string;
|
||||
|
||||
/**
|
||||
* Unsubscribes a previously registered handler.
|
||||
*
|
||||
* The handler will no longer be called as long as this method isn't invoked from a subscribed callback.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unsubscribe(string $id): void;
|
||||
|
||||
/**
|
||||
* Returns whether cancellation has been requested yet.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequested(): bool;
|
||||
|
||||
/**
|
||||
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws CancelledException
|
||||
*/
|
||||
public function throwIfRequested(): void;
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Cancellation;
|
||||
|
||||
/**
|
||||
* A cancellation token source provides a mechanism to cancel operations.
|
||||
*
|
||||
* Cancellation of operation works by creating a cancellation token source and passing the corresponding token when
|
||||
* starting the operation. To cancel the operation, invoke `CancellationTokenSource::cancel()`.
|
||||
*
|
||||
* Any operation can decide what to do on a cancellation request, it has "don't care" semantics. An operation SHOULD be
|
||||
* aborted, but MAY continue. Example: A DNS client might continue to receive and cache the response, as the query has
|
||||
* been sent anyway. An HTTP client would usually close a connection, but might not do so in case a response is close to
|
||||
* be fully received to reuse the connection.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* ```php
|
||||
* $tokenSource = new CancellationTokenSource;
|
||||
* $token = $tokenSource->getToken();
|
||||
*
|
||||
* $response = yield $httpClient->request("https://example.com/stream", $token);
|
||||
* $responseBody = $response->getBody();
|
||||
*
|
||||
* while (($chunk = yield $response->read()) !== null) {
|
||||
* // consume $chunk
|
||||
*
|
||||
* if ($noLongerInterested) {
|
||||
* $cancellationTokenSource->cancel();
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see Token
|
||||
* @see CancelledException
|
||||
*/
|
||||
final class TokenSource
|
||||
{
|
||||
private $token;
|
||||
private $onCancel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->token = new class($this->onCancel) implements Token {
|
||||
/** @var string */
|
||||
private $nextId = "a";
|
||||
|
||||
/** @var callable[] */
|
||||
private $callbacks = [];
|
||||
|
||||
/** @var \Throwable|null */
|
||||
private $exception;
|
||||
|
||||
public function __construct(&$onCancel)
|
||||
{
|
||||
$onCancel = function (\Throwable $exception) {
|
||||
$this->exception = $exception;
|
||||
|
||||
$callbacks = $this->callbacks;
|
||||
$this->callbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
$callback($this->exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
|
||||
if ($this->exception) {
|
||||
$callback($this->exception);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function unsubscribe(string $id): void
|
||||
{
|
||||
unset($this->callbacks[$id]);
|
||||
}
|
||||
|
||||
public function isRequested(): bool
|
||||
{
|
||||
return $this->exception !== null;
|
||||
}
|
||||
|
||||
public function throwIfRequested(): void
|
||||
{
|
||||
if ($this->exception !== null) {
|
||||
throw $this->exception;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function getToken(): Token
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Throwable|null $previous Exception to be used as the previous exception to CancelledException.
|
||||
*/
|
||||
public function cancel(\Throwable $previous = null): void
|
||||
{
|
||||
if ($this->onCancel === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$onCancel = $this->onCancel;
|
||||
$this->onCancel = null;
|
||||
$onCancel(new CancelledException("The operation was cancelled", $previous));
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace Amp;
|
||||
|
||||
use Amp\Cancellation\CancelledException;
|
||||
use Concurrent\Awaitable;
|
||||
use Concurrent\Deferred;
|
||||
use Concurrent\Task;
|
||||
@ -58,7 +57,7 @@ final class Emitter
|
||||
public $waiting;
|
||||
};
|
||||
|
||||
$this->iterator = (static function () use (&$state) {
|
||||
$this->iterator = (static function () use ($state) {
|
||||
while (true) {
|
||||
if (isset($state->backpressure[$state->position])) {
|
||||
/** @var Deferred $deferred */
|
||||
@ -92,7 +91,7 @@ final class Emitter
|
||||
public function __destruct()
|
||||
{
|
||||
if (!$this->state->complete) {
|
||||
$this->fail(new CancelledException("The operation was cancelled, because the emitter was garbage collected without completing, created in {$this->createFile}:{$this->createLine}"));
|
||||
$this->fail(new \Error("The emitter was garbage collected without completing, created in {$this->createFile}:{$this->createLine}"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,7 +105,7 @@ final class Emitter
|
||||
public function extractIterator(): \Iterator
|
||||
{
|
||||
if ($this->iterator === null) {
|
||||
throw new \Error("The emitter's iterator can only be extracted once!");
|
||||
throw new \Error("The emitter's iterator can only be extracted once");
|
||||
}
|
||||
|
||||
$iterator = $this->iterator;
|
||||
@ -131,7 +130,7 @@ final class Emitter
|
||||
}
|
||||
|
||||
$this->state->values[] = $value;
|
||||
$this->state->backpressure[] = $pressure = new Deferred;
|
||||
$this->state->backpressure[] = $backpressure = new Deferred;
|
||||
|
||||
if ($this->state->waiting !== null) {
|
||||
/** @var Deferred $waiting */
|
||||
@ -140,7 +139,7 @@ final class Emitter
|
||||
$waiting->resolve(true);
|
||||
}
|
||||
|
||||
Task::await($pressure->awaitable());
|
||||
Task::await($backpressure->awaitable());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,7 +148,7 @@ final class Emitter
|
||||
public function complete(): void
|
||||
{
|
||||
if ($this->state->complete) {
|
||||
throw new \Error("Emitters has already been " . ($this->state->exception === null ? "completed" : "failed"));
|
||||
throw new \Error("Emitter has already been " . ($this->state->exception === null ? "completed" : "failed"));
|
||||
}
|
||||
|
||||
$this->state->complete = true;
|
||||
@ -170,7 +169,7 @@ final class Emitter
|
||||
public function fail(\Throwable $reason): void
|
||||
{
|
||||
if ($this->state->complete) {
|
||||
throw new \Error("Emitters has already been " . ($this->state->exception === null ? "completed" : "failed"));
|
||||
throw new \Error("Emitter has already been " . ($this->state->exception === null ? "completed" : "failed"));
|
||||
}
|
||||
|
||||
$this->state->complete = true;
|
||||
|
@ -5,8 +5,8 @@ namespace Amp;
|
||||
/**
|
||||
* A "safe" struct trait for public property aggregators.
|
||||
*
|
||||
* This trait is intended to make using public properties a little safer by throwing when
|
||||
* nonexistent property names are read or written.
|
||||
* This trait is intended to make using public properties a little safer by throwing when nonexistent property names are
|
||||
* read or written.
|
||||
*/
|
||||
trait Struct
|
||||
{
|
||||
@ -16,6 +16,13 @@ trait Struct
|
||||
*/
|
||||
private $__propertySuggestThreshold = 70;
|
||||
|
||||
public function __isset(string $property)
|
||||
{
|
||||
throw new \Error(
|
||||
$this->generateStructPropertyError($property)
|
||||
);
|
||||
}
|
||||
|
||||
public function __get(string $property)
|
||||
{
|
||||
throw new \Error(
|
||||
@ -30,13 +37,20 @@ trait Struct
|
||||
);
|
||||
}
|
||||
|
||||
public function __unset(string $property)
|
||||
{
|
||||
throw new \Error(
|
||||
$this->generateStructPropertyError($property)
|
||||
);
|
||||
}
|
||||
|
||||
private function generateStructPropertyError(string $property): string
|
||||
{
|
||||
$suggestion = $this->suggestPropertyName($property);
|
||||
$suggestStr = ($suggestion == "") ? "" : " ... did you mean \"{$suggestion}?\"";
|
||||
$suggestStr = $suggestion === "" ? "" : " ... did you mean '{$suggestion}'?";
|
||||
|
||||
return \sprintf(
|
||||
"%s property \"%s\" does not exist%s",
|
||||
"%s property '%s' does not exist%s",
|
||||
\str_replace("\0", "@", \get_class($this)), // Handle anonymous class names.
|
||||
$property,
|
||||
$suggestStr
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace Amp;
|
||||
|
||||
/**
|
||||
* Thrown if a promise doesn't resolve within a specified timeout.
|
||||
* Thrown if an awaitable doesn't resolve within a specified timeout.
|
||||
*
|
||||
* @see \Amp\timeout()
|
||||
*/
|
||||
|
@ -43,11 +43,11 @@ namespace Amp
|
||||
* @param Awaitable $awaitable Awaitable to which the timeout is applied.
|
||||
* @param int $timeout Timeout in milliseconds.
|
||||
*
|
||||
* @return Awaitable
|
||||
* @return mixed
|
||||
*
|
||||
* @throws TimeoutException
|
||||
*/
|
||||
function timeout(Awaitable $awaitable, int $timeout): Awaitable
|
||||
function timeout(Awaitable $awaitable, int $timeout)
|
||||
{
|
||||
$timeoutAwaitable = Task::async(function () use ($timeout) {
|
||||
$timer = new Timer($timeout);
|
||||
@ -62,16 +62,11 @@ namespace Amp
|
||||
function some(array $awaitables, int $required = 1)
|
||||
{
|
||||
if ($required < 0) {
|
||||
throw new \Error("Number of promises required must be non-negative");
|
||||
throw new \Error("Number of awaitables required must be non-negative, was '{$required}'");
|
||||
}
|
||||
|
||||
$pending = \count($awaitables);
|
||||
|
||||
if ($required > $pending) {
|
||||
throw new \Error("Too few promises provided");
|
||||
}
|
||||
if (empty($awaitables)) {
|
||||
return [[], []];
|
||||
throw new \Error("Array of awaitables can't be empty");
|
||||
}
|
||||
|
||||
$values = [];
|
||||
@ -88,11 +83,7 @@ namespace Amp
|
||||
$key,
|
||||
?\Throwable $error,
|
||||
$value
|
||||
) use (
|
||||
&$values,
|
||||
&$errors,
|
||||
$required
|
||||
) {
|
||||
) use (&$values, &$errors, $required) {
|
||||
if ($error) {
|
||||
$errors[$key] = $error;
|
||||
unset($values[$key]);
|
||||
|
@ -1,17 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
>
|
||||
<phpunit bootstrap="vendor/autoload.php" colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd">
|
||||
<testsuites>
|
||||
<testsuite name="Main">
|
||||
<directory>test</directory>
|
||||
@ -25,7 +14,4 @@
|
||||
<directory suffix=".php">lib</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<listeners>
|
||||
<listener class="Amp\PHPUnit\LoopReset"/>
|
||||
</listeners>
|
||||
</phpunit>
|
||||
|
@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\Cancellation\Token;
|
||||
use Amp\Cancellation\TokenSource;
|
||||
use Amp\Emitter;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
use Amp\PHPUnit\TestException;
|
||||
use Concurrent\Task;
|
||||
use function Amp\rethrow;
|
||||
|
||||
class CancellationTest extends TestCase
|
||||
{
|
||||
private function createIterator(Token $token): \Iterator
|
||||
{
|
||||
$emitter = new Emitter;
|
||||
|
||||
rethrow(Task::async(function () use ($emitter, $token) {
|
||||
$running = true;
|
||||
|
||||
$token->subscribe(function () use (&$running) {
|
||||
$running = false;
|
||||
});
|
||||
|
||||
$i = 0;
|
||||
while ($running) {
|
||||
$emitter->emit($i++);
|
||||
}
|
||||
|
||||
$emitter->complete();
|
||||
}));
|
||||
|
||||
return $emitter->extractIterator();
|
||||
}
|
||||
|
||||
public function testCancellationCancelsIterator(): void
|
||||
{
|
||||
$source = new TokenSource;
|
||||
$current = null;
|
||||
|
||||
foreach ($this->createIterator($source->getToken()) as $current) {
|
||||
$this->assertInternalType("int", $current);
|
||||
|
||||
if ($current === 3) {
|
||||
$source->cancel();
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame(3, $current);
|
||||
}
|
||||
|
||||
public function testUnsubscribeWorks(): void
|
||||
{
|
||||
$cancellationSource = new TokenSource;
|
||||
|
||||
$first = $cancellationSource->getToken()->subscribe(function () {
|
||||
$this->fail("Callback has been called");
|
||||
});
|
||||
|
||||
$cancellationSource->getToken()->subscribe(function () {
|
||||
$this->assertTrue(true);
|
||||
});
|
||||
|
||||
$cancellationSource->getToken()->unsubscribe($first);
|
||||
|
||||
$cancellationSource->cancel();
|
||||
}
|
||||
|
||||
public function testThrowingCallback(): void
|
||||
{
|
||||
$this->expectException(TestException::class);
|
||||
|
||||
$cancellationSource = new TokenSource;
|
||||
$cancellationSource->getToken()->subscribe(function () {
|
||||
throw new TestException(__LINE__);
|
||||
});
|
||||
|
||||
$cancellationSource->cancel();
|
||||
}
|
||||
|
||||
public function testDoubleCancelOnlyInvokesOnce(): void
|
||||
{
|
||||
$cancellationSource = new TokenSource;
|
||||
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
||||
|
||||
$cancellationSource->cancel();
|
||||
$cancellationSource->cancel();
|
||||
}
|
||||
|
||||
public function testCalledIfSubscribingAfterCancel(): void
|
||||
{
|
||||
$cancellationSource = new TokenSource;
|
||||
$cancellationSource->cancel();
|
||||
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Cancellation;
|
||||
|
||||
use Amp\Cancellation\CancelledException;
|
||||
use Amp\Cancellation\TimeoutToken;
|
||||
use Amp\Loop;
|
||||
use Amp\TimeoutException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use function Amp\delay;
|
||||
|
||||
class TimeoutTokenTest extends TestCase
|
||||
{
|
||||
public function testTimeout(): void
|
||||
{
|
||||
$token = new TimeoutToken(10);
|
||||
|
||||
$this->assertFalse($token->isRequested());
|
||||
delay(20);
|
||||
$this->assertTrue($token->isRequested());
|
||||
|
||||
try {
|
||||
$token->throwIfRequested();
|
||||
$this->fail("Must throw exception");
|
||||
} catch (CancelledException $exception) {
|
||||
$this->assertInstanceOf(TimeoutException::class, $exception->getPrevious());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWatcherCancellation(): void
|
||||
{
|
||||
$token = new TimeoutToken(1);
|
||||
$this->assertSame(1, Loop::getInfo()["delay"]["enabled"]);
|
||||
unset($token);
|
||||
$this->assertSame(0, Loop::getInfo()["delay"]["enabled"]);
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Loop;
|
||||
|
||||
use Amp\Loop;
|
||||
use Amp\Loop\Driver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DriverStateTest extends TestCase
|
||||
{
|
||||
/** @var Driver */
|
||||
private $loop;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loop = $this->getMockForAbstractClass(Driver::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function defaultsToNull()
|
||||
{
|
||||
$this->assertNull($this->loop->getState("foobar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideValues
|
||||
*/
|
||||
public function getsPreviouslySetValue($value)
|
||||
{
|
||||
$this->loop->setState("foobar", $value);
|
||||
$this->assertSame($value, $this->loop->getState("foobar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideValues
|
||||
*/
|
||||
public function getsPreviouslySetValueViaAccessor($value)
|
||||
{
|
||||
Loop::setState("foobar", $value);
|
||||
$this->assertSame($value, Loop::getState("foobar"));
|
||||
}
|
||||
|
||||
public function provideValues()
|
||||
{
|
||||
return [
|
||||
["string"],
|
||||
[42],
|
||||
[1.001],
|
||||
[true],
|
||||
[false],
|
||||
[null],
|
||||
[new \StdClass],
|
||||
];
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Loop;
|
||||
|
||||
use Amp\Loop\EvDriver;
|
||||
|
||||
/**
|
||||
* @requires extension ev
|
||||
*/
|
||||
class EvDriverTest extends DriverTest
|
||||
{
|
||||
public function getFactory(): callable
|
||||
{
|
||||
return function () {
|
||||
return new EvDriver;
|
||||
};
|
||||
}
|
||||
|
||||
public function testHandle()
|
||||
{
|
||||
$this->assertInstanceOf(\EvLoop::class, $this->loop->getHandle());
|
||||
}
|
||||
|
||||
public function testSupported()
|
||||
{
|
||||
$this->assertTrue(EvDriver::isSupported());
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Loop;
|
||||
|
||||
use Amp\Loop\EventDriver;
|
||||
|
||||
/**
|
||||
* @requires extension event
|
||||
*/
|
||||
class EventDriverTest extends DriverTest
|
||||
{
|
||||
public function getFactory(): callable
|
||||
{
|
||||
return function () {
|
||||
return new EventDriver;
|
||||
};
|
||||
}
|
||||
|
||||
public function testHandle()
|
||||
{
|
||||
$this->assertInstanceOf(\EventBase::class, $this->loop->getHandle());
|
||||
}
|
||||
|
||||
public function testSupported()
|
||||
{
|
||||
$this->assertTrue(EventDriver::isSupported());
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Loop;
|
||||
|
||||
use Amp\Loop\NativeDriver;
|
||||
|
||||
class NativeDriverTest extends DriverTest
|
||||
{
|
||||
public function getFactory(): callable
|
||||
{
|
||||
return function () {
|
||||
return new NativeDriver;
|
||||
};
|
||||
}
|
||||
|
||||
public function testHandle(): void
|
||||
{
|
||||
$this->assertNull($this->loop->getHandle());
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test\Loop;
|
||||
|
||||
use Amp\Loop\UvDriver;
|
||||
|
||||
/**
|
||||
* @requires extension uv
|
||||
*/
|
||||
class UvDriverTest extends DriverTest
|
||||
{
|
||||
public function getFactory(): callable
|
||||
{
|
||||
return function () {
|
||||
return new UvDriver;
|
||||
};
|
||||
}
|
||||
|
||||
public function testHandle()
|
||||
{
|
||||
$handle = $this->loop->getHandle();
|
||||
$this->assertTrue(\is_resource($handle) || $handle instanceof \UVLoop);
|
||||
}
|
||||
|
||||
public function testSupported()
|
||||
{
|
||||
$this->assertTrue(UvDriver::isSupported());
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
--TEST--
|
||||
Test order of destruction not interfering with access to UV handles
|
||||
--SKIPIF--
|
||||
<?php
|
||||
\extension_loaded("uv") or die("SKIP: ext/uv required for this test");
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
include __DIR__.'/../../vendor/autoload.php';
|
||||
|
||||
use Amp\Loop;
|
||||
|
||||
Loop::setState('test', new class {
|
||||
private $handle;
|
||||
public function __construct()
|
||||
{
|
||||
$this->handle = Loop::repeat(10, function () {});
|
||||
}
|
||||
public function __destruct()
|
||||
{
|
||||
Loop::cancel($this->handle);
|
||||
print "ok";
|
||||
}
|
||||
});
|
||||
|
||||
Loop::delay(0, [Loop::class, "stop"]);
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
ok
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\Loop;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LoopTest extends TestCase
|
||||
{
|
||||
public function testDelayWithNegativeDelay(): void
|
||||
{
|
||||
$this->expectException(\Error::class);
|
||||
|
||||
Loop::delay(-1, function () {
|
||||
});
|
||||
}
|
||||
|
||||
public function testRepeatWithNegativeInterval(): void
|
||||
{
|
||||
$this->expectException(\Error::class);
|
||||
|
||||
Loop::repeat(-1, function () {
|
||||
});
|
||||
}
|
||||
|
||||
public function testOnReadable(): void
|
||||
{
|
||||
Loop::run(function () {
|
||||
$ends = \stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
|
||||
\fwrite($ends[0], "trigger readability watcher");
|
||||
|
||||
Loop::onReadable($ends[1], function ($watcherId) {
|
||||
$this->assertTrue(true);
|
||||
Loop::cancel($watcherId);
|
||||
Loop::stop();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function testOnWritable(): void
|
||||
{
|
||||
Loop::run(function () {
|
||||
Loop::onWritable(STDOUT, function ($watcherId) {
|
||||
$this->assertTrue(true);
|
||||
Loop::cancel($watcherId);
|
||||
Loop::stop();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function testGet(): void
|
||||
{
|
||||
$this->assertInstanceOf(Loop\Driver::class, Loop::get());
|
||||
}
|
||||
|
||||
public function testGetInto(): void
|
||||
{
|
||||
$this->assertSame(Loop::get()->getInfo(), Loop::getInfo());
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\Loop;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
use Amp\PHPUnit\TestException;
|
||||
use Concurrent\Deferred;
|
||||
use function Amp\delay;
|
||||
use function Amp\rethrow;
|
||||
|
||||
class RethrowTest extends TestCase
|
||||
{
|
||||
public function testRethrow(): void
|
||||
{
|
||||
$exception = new TestException;
|
||||
|
||||
Loop::setErrorHandler($this->createCallback(1));
|
||||
|
||||
rethrow(Deferred::error($exception));
|
||||
delay(1);
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StructTestFixture
|
||||
{
|
||||
use \Amp\Struct;
|
||||
@ -9,11 +11,11 @@ class StructTestFixture
|
||||
public $_foofoofoofoofoofoofoofoobar;
|
||||
}
|
||||
|
||||
class StructTest extends \PHPUnit\Framework\TestCase
|
||||
class StructTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property "callbac" does not exist ... did you mean
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property 'callbac' does not exist ... did you mean
|
||||
* "callback?"
|
||||
*/
|
||||
public function testSetErrorWithSuggestion(): void
|
||||
@ -25,7 +27,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property "callbac" does not exist ... did you mean
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property 'callbac' does not exist ... did you mean
|
||||
* "callback?"
|
||||
*/
|
||||
public function testGetErrorWithSuggestion(): void
|
||||
@ -36,7 +38,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property "callZZZZZZZZZZZ" does not exist
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property 'callZZZZZZZZZZZ' does not exist
|
||||
*/
|
||||
public function testSetErrorWithoutSuggestion(): void
|
||||
{
|
||||
@ -46,7 +48,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property "callZZZZZZZZZZZ" does not exist
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property 'callZZZZZZZZZZZ' does not exist
|
||||
*/
|
||||
public function testGetErrorWithoutSuggestion(): void
|
||||
{
|
||||
@ -56,7 +58,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property "__propertySuggestThreshold" does not exist
|
||||
* @expectedExceptionMessage Amp\Test\StructTestFixture property '__propertySuggestThreshold' does not exist
|
||||
*/
|
||||
public function testSuggestionIgnoresPropertyStartingWithUnderscore(): void
|
||||
{
|
||||
@ -68,7 +70,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
// Use regexp to ensure no property is suggested, because expected message is a prefix then and still passes
|
||||
$this->expectException(\Error::class);
|
||||
$this->expectExceptionMessageRegExp("(Amp\\\\Test\\\\StructTestFixture property \"foofoofoofoofoofoofoofoobar\" does not exist$)");
|
||||
$this->expectExceptionMessageRegExp("(Amp\\\\Test\\\\StructTestFixture property 'foofoofoofoofoofoofoofoobar' does not exist$)");
|
||||
|
||||
$struct = new StructTestFixture;
|
||||
$struct->foofoofoofoofoofoofoofoobar = "test";
|
||||
@ -78,7 +80,7 @@ class StructTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
// Use regexp to ensure no property is suggested, because expected message is a prefix then and still passes
|
||||
$this->expectException(\Error::class);
|
||||
$this->expectExceptionMessageRegExp("(Amp\\\\Test\\\\StructTestFixture property \"foofoofoofoofoofoofoofoobar\" does not exist$)");
|
||||
$this->expectExceptionMessageRegExp("(Amp\\\\Test\\\\StructTestFixture property 'foofoofoofoofoofoofoofoobar' does not exist$)");
|
||||
|
||||
$struct = new StructTestFixture;
|
||||
$struct->foofoofoofoofoofoofoofoobar;
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\PHPUnit\TestCase;
|
||||
use Concurrent\AsyncTestCase;
|
||||
use Concurrent\Task;
|
||||
use function Amp\delay;
|
||||
|
||||
class TaskTest extends TestCase
|
||||
class TaskTest extends AsyncTestCase
|
||||
{
|
||||
public function testSequentialAwait(): void
|
||||
{
|
||||
|
@ -3,37 +3,34 @@
|
||||
namespace Amp\Test;
|
||||
|
||||
use Amp\TimeoutException;
|
||||
use Concurrent\AsyncTestCase;
|
||||
use Concurrent\Deferred;
|
||||
use Concurrent\Task;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use function Amp\delay;
|
||||
use function Amp\timeout;
|
||||
|
||||
class TimeoutTest extends TestCase
|
||||
class TimeoutTest extends AsyncTestCase
|
||||
{
|
||||
public function testSuccessfulPromise(): void
|
||||
public function testSuccessful(): void
|
||||
{
|
||||
$value = 1;
|
||||
|
||||
$awaitable = Deferred::value($value);
|
||||
$awaitable = timeout($awaitable, 100);
|
||||
|
||||
$this->assertSame($value, Task::await($awaitable));
|
||||
$this->assertSame($value, timeout(Deferred::value($value), 100));
|
||||
}
|
||||
|
||||
public function testFailedPromise(): void
|
||||
public function testFailure(): void
|
||||
{
|
||||
$exception = new \Exception;
|
||||
|
||||
$awaitable = Deferred::error($exception);
|
||||
$awaitable = timeout($awaitable, 100);
|
||||
|
||||
$this->expectExceptionObject($exception);
|
||||
Task::await($awaitable);
|
||||
|
||||
timeout($awaitable, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSuccessfulPromise
|
||||
* @depends testSuccessful
|
||||
*/
|
||||
public function testFastPending(): void
|
||||
{
|
||||
@ -45,11 +42,11 @@ class TimeoutTest extends TestCase
|
||||
return $value;
|
||||
});
|
||||
|
||||
$this->assertSame($value, Task::await(timeout($awaitable, 100)));
|
||||
$this->assertSame($value, timeout($awaitable, 100));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSuccessfulPromise
|
||||
* @depends testSuccessful
|
||||
*/
|
||||
public function testSlowPending(): void
|
||||
{
|
||||
@ -64,7 +61,7 @@ class TimeoutTest extends TestCase
|
||||
$this->expectException(TimeoutException::class);
|
||||
|
||||
try {
|
||||
Task::await(timeout($awaitable, 100));
|
||||
timeout($awaitable, 100);
|
||||
} finally {
|
||||
Task::await($awaitable); // await to clear pending watchers
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user