1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00

Make WeakenTest less timing dependent

This commit is contained in:
Niklas Keller 2021-12-02 19:01:05 +01:00
parent bdce2f0896
commit 557913da92

View File

@ -9,91 +9,103 @@ class WeakenTest extends AsyncTestCase
{ {
public function provideObjectFactories(): iterable public function provideObjectFactories(): iterable
{ {
return [ yield 'binding' => [
'binding' => [fn (&$count) => new class($count) { fn (&$count) => new class($count) {
private string $watcher; private string $callbackId;
public function __construct(int &$count) public function __construct(int &$count)
{ {
$this->watcher = EventLoop::repeat(0.01, weaken(function (string $watcher) use (&$count): void { $this->callbackId = EventLoop::repeat(
AsyncTestCase::assertNotNull($this); 0.001,
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this)); weaken(function (string $callbackId) use (&$count): void {
AsyncTestCase::assertSame($watcher, $this->watcher); AsyncTestCase::assertNotNull($this);
++$count; AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
})); AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$count;
})
);
} }
public function __destruct() public function __destruct()
{ {
EventLoop::cancel($this->watcher); EventLoop::cancel($this->callbackId);
} }
}], },
'static' => [fn (&$count) => new class($count) { ];
private string $watcher = '';
yield 'static' => [
fn (&$count) => new class($count) {
private string $callbackId = '';
public function __construct(int &$count) public function __construct(int &$count)
{ {
$watcherRef = &$this->watcher; $callbackIdRef = &$this->callbackId;
$this->watcher = EventLoop::repeat(0.01, weaken(static function (string $watcher) use ( $this->callbackId = EventLoop::repeat(0.001, weaken(static function (string $callbackId) use (
&$count, &$count,
&$watcherRef &$callbackIdRef
): void { ): void {
AsyncTestCase::assertSame($watcher, $watcherRef); AsyncTestCase::assertSame($callbackId, $callbackIdRef);
++$count; ++$count;
})); }));
} }
public function __destruct() public function __destruct()
{ {
EventLoop::cancel($this->watcher); EventLoop::cancel($this->callbackId);
} }
}], },
'fromCallable' => [fn (&$count) => new class($count) { ];
private string $watcher = '';
yield 'fromCallable' => [
fn (&$count) => new class($count) {
private string $callbackId = '';
private int $count; private int $count;
public function __construct(int &$count) public function __construct(int &$count)
{ {
$this->count = &$count; $this->count = &$count;
$this->watcher = EventLoop::repeat(0.01, weaken(\Closure::fromCallable([$this, 'callback']))); $this->callbackId = EventLoop::repeat(0.001, weaken(\Closure::fromCallable([$this, 'callback'])));
} }
private function callback(string $watcher): void private function callback(string $callbackId): void
{ {
AsyncTestCase::assertNotNull($this); AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this)); AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($watcher, $this->watcher); AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$this->count; ++$this->count;
} }
public function __destruct() public function __destruct()
{ {
EventLoop::cancel($this->watcher); EventLoop::cancel($this->callbackId);
} }
}], },
'__invoke' => [fn (&$count) => new class($count) { ];
private string $watcher = '';
yield '__invoke' => [
fn (&$count) => new class($count) {
private string $callbackId = '';
private int $count; private int $count;
public function __construct(int &$count) public function __construct(int &$count)
{ {
$this->count = &$count; $this->count = &$count;
$this->watcher = EventLoop::repeat(0.01, weaken($this)); $this->callbackId = EventLoop::repeat(0.001, weaken($this));
} }
public function __invoke(string $watcher): void public function __invoke(string $callbackId): void
{ {
AsyncTestCase::assertNotNull($this); AsyncTestCase::assertNotNull($this);
AsyncTestCase::assertStringContainsString('anonymous', \get_class($this)); AsyncTestCase::assertStringContainsString('anonymous', \get_class($this));
AsyncTestCase::assertSame($watcher, $this->watcher); AsyncTestCase::assertSame($callbackId, $this->callbackId);
++$this->count; ++$this->count;
} }
public function __destruct() public function __destruct()
{ {
EventLoop::cancel($this->watcher); EventLoop::cancel($this->callbackId);
} }
}], },
]; ];
} }
@ -102,16 +114,18 @@ class WeakenTest extends AsyncTestCase
*/ */
public function test(callable $factory): void public function test(callable $factory): void
{ {
$this->setTimeout(0.1); $this->setTimeout(0.2);
$count = 0; $count = 0;
$object = $factory($count); $object = $factory($count);
delay(0.035); delay(0.05);
unset($object); // Should destroy object and cancel loop watcher. unset($object); // Should destroy object and cancel loop watcher.
self::assertSame(3, $count); self::assertGreaterThan(1, $count);
delay(0.025); $countBackup = $count;
self::assertSame(3, $count);
delay(0.05);
self::assertSame($countBackup, $count);
} }
} }