getCancellation()->subscribe(function () { $this->fail("Callback has been called"); }); $deferredCancellation->getCancellation()->subscribe(function () { $this->assertTrue(true); }); $deferredCancellation->getCancellation()->unsubscribe($id); $deferredCancellation->cancel(); } public function testThrowingCallbacksEndUpInLoop(): void { EventLoop::setErrorHandler(function (\Throwable $exception) use (&$reason): void { $reason = $exception; }); $cancellationSource = new DeferredCancellation; $cancellationSource->getCancellation()->subscribe(function () { throw new TestException; }); $cancellationSource->cancel(); delay(0.01); // Tick event loop to invoke callbacks. self::assertInstanceOf(TestException::class, $reason); } public function testDoubleCancelOnlyInvokesOnce(): void { $cancellationSource = new DeferredCancellation; $cancellationSource->getCancellation()->subscribe($this->createCallback(1)); $cancellationSource->cancel(); $cancellationSource->cancel(); } public function testCalledIfSubscribingAfterCancel(): void { $cancellationSource = new DeferredCancellation; $cancellationSource->cancel(); $cancellationSource->getCancellation()->subscribe($this->createCallback(1)); } }