1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/test/WeakenTest.php

117 lines
3.8 KiB
PHP
Raw Normal View History

2021-09-29 16:00:33 +02:00
<?php
namespace Amp;
use Amp\PHPUnit\AsyncTestCase;
2021-10-15 00:50:40 +02:00
use Revolt\EventLoop;
2021-09-29 16:00:33 +02:00
class WeakenTest extends AsyncTestCase
{
public function provideObjectFactories(): iterable
{
return [
'binding' => [fn (&$count) => new class ($count) {
private string $watcher;
public function __construct(int &$count)
{
2021-10-15 00:50:40 +02:00
$this->watcher = EventLoop::repeat(0.01, weaken(function (string $watcher) use (&$count): void {
2021-09-29 16:00:33 +02:00
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($watcher, $this->watcher);
++$count;
}));
}
public function __destruct()
{
2021-10-15 00:50:40 +02:00
EventLoop::cancel($this->watcher);
2021-09-29 16:00:33 +02:00
}
}],
'static' => [fn (&$count) => new class ($count) {
private string $watcher = '';
public function __construct(int &$count)
{
$watcherRef = &$this->watcher;
2021-10-15 00:50:40 +02:00
$this->watcher = EventLoop::repeat(0.01, weaken(static function (string $watcher) use (
2021-09-29 16:00:33 +02:00
&$count, &$watcherRef
): void {
AsyncTestCase::assertSame($watcher, $watcherRef);
++$count;
}));
}
public function __destruct()
{
2021-10-15 00:50:40 +02:00
EventLoop::cancel($this->watcher);
2021-09-29 16:00:33 +02:00
}
}],
'fromCallable' => [fn (&$count) => new class ($count) {
private string $watcher = '';
private int $count;
public function __construct(int &$count)
{
$this->count = &$count;
2021-10-15 00:50:40 +02:00
$this->watcher = EventLoop::repeat(0.01, weaken(\Closure::fromCallable([$this, 'callback'])));
2021-09-29 16:00:33 +02:00
}
private function callback(string $watcher): void
{
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($watcher, $this->watcher);
++$this->count;
}
public function __destruct()
{
2021-10-15 00:50:40 +02:00
EventLoop::cancel($this->watcher);
2021-09-29 16:00:33 +02:00
}
}],
'__invoke' => [fn (&$count) => new class ($count) {
private string $watcher = '';
private int $count;
public function __construct(int &$count)
{
$this->count = &$count;
2021-10-15 00:50:40 +02:00
$this->watcher = EventLoop::repeat(0.01, weaken($this));
2021-09-29 16:00:33 +02:00
}
public function __invoke(string $watcher): void
{
AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($watcher, $this->watcher);
++$this->count;
}
public function __destruct()
{
2021-10-15 00:50:40 +02:00
EventLoop::cancel($this->watcher);
2021-09-29 16:00:33 +02:00
}
}],
];
}
/**
* @dataProvider provideObjectFactories
*/
public function test(callable $factory): void
{
$this->setTimeout(0.1);
$count = 0;
$object = $factory($count);
delay(0.035);
unset($object); // Should destroy object and cancel loop watcher.
self::assertSame(3, $count);
delay(0.025);
self::assertSame(3, $count);
}
}