1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/test/WeakenTest.php
2021-12-02 19:12:06 +01:00

141 lines
4.5 KiB
PHP

<?php
namespace Amp;
use Amp\PHPUnit\AsyncTestCase;
use Revolt\EventLoop;
class WeakenTest extends AsyncTestCase
{
public function provideObjectFactories(): iterable
{
yield 'binding' => [
fn (&$count, &$id) => new class($count, $id) {
private string $callbackId;
public function __construct(int &$count, &$id)
{
$this->callbackId = $id = EventLoop::repeat(
0.001,
weaken(function (string $callbackId) use (&$count): void {
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$count;
})
);
}
public function __destruct()
{
EventLoop::cancel($this->callbackId);
}
},
];
yield 'static' => [
fn (&$count, &$id) => new class($count, $id) {
private string $callbackId = '';
public function __construct(int &$count, &$id)
{
$callbackIdRef = &$this->callbackId;
$this->callbackId = $id = EventLoop::repeat(0.001, weaken(static function (string $callbackId) use (
&$count,
&$callbackIdRef
): void {
AsyncTestCase::assertSame($callbackId, $callbackIdRef);
++$count;
}));
}
public function __destruct()
{
EventLoop::cancel($this->callbackId);
}
},
];
yield 'fromCallable' => [
fn (&$count, &$id) => new class($count, $id) {
private string $callbackId = '';
private int $count;
public function __construct(int &$count, &$id)
{
$this->count = &$count;
$this->callbackId = $id = EventLoop::repeat(
0.001,
weaken(\Closure::fromCallable([$this, 'callback']))
);
}
private function callback(string $callbackId): void
{
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$this->count;
}
public function __destruct()
{
EventLoop::cancel($this->callbackId);
}
},
];
yield '__invoke' => [
fn (&$count, &$id) => new class($count, $id) {
private string $callbackId = '';
private int $count;
public function __construct(int &$count, &$id)
{
$this->count = &$count;
$this->callbackId = $id = EventLoop::repeat(0.001, weaken($this));
}
public function __invoke(string $callbackId): void
{
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$this->count;
}
public function __destruct()
{
EventLoop::cancel($this->callbackId);
}
},
];
}
/**
* @dataProvider provideObjectFactories
*/
public function test(callable $factory): void
{
$this->setTimeout(0.2);
$count = 0;
$id = null;
$object = $factory($count, $id);
delay(0.05);
self::assertGreaterThan(1, $count);
// Ensure the callback isn't cancelled, yet
EventLoop::enable($id);
unset($object); // Should destroy object and cancel loop watcher.
// Ensure the callback is already cancelled
$this->expectException(EventLoop\InvalidCallbackError::class);
$this->expectExceptionCode(EventLoop\InvalidCallbackError::E_INVALID_IDENTIFIER);
EventLoop::enable($id);
}
}