def test_token_source_works(): source = CancellationTokenSource() assert not source.is_cancellation_requested with source as token: assert isinstance(token, CancellationToken)
def canceller() -> Tuple[AsyncDisposable, CancellationToken]: cts = CancellationTokenSource() async def cancel() -> None: log.debug("cancller, cancelling!") cts.cancel() return AsyncDisposable.create(cancel), cts.token
def test_token_cancelled_source_works(): source = CancellationTokenSource.cancelled_source() assert isinstance(source, CancellationTokenSource) assert source.is_cancellation_requested with pytest.raises(ObjectDisposedException): with source as disposable: assert not disposable
def test_token_cancellation_register_works(): called = [] source = CancellationTokenSource() with source as token: token.register(lambda: called.append(True)) assert not called assert called
def test_token_cancellation_register_works(): called: List[bool] = [] source = CancellationTokenSource() with source: token = source.token token.register(lambda: called.append(True)) assert not called assert called
def test_token_cancelled_register_throws(): called = [] source = CancellationTokenSource.cancelled_source() with pytest.raises(ObjectDisposedException): # type: ignore with source as token: token.register(lambda: called.append(True)) assert not called
def _delay(source: AsyncObservable[TSource]) -> AsyncObservable[TSource]: cts = CancellationTokenSource() token = cts.token async def subscribe_async( aobv: AsyncObserver[TSource]) -> AsyncDisposable: async def worker( inbox: MailboxProcessor[Tuple[Notification[TSource], datetime]] ) -> None: @tailrec_async async def loop() -> TailCallResult[None]: if token.is_cancellation_requested: return ns, due_time = await inbox.receive() diff = due_time - datetime.utcnow() seconds = diff.total_seconds() if seconds > 0: await asyncio.sleep(seconds) async def matcher() -> None: with match(ns) as case: for x in case(OnNext[TSource]): await aobv.asend(x) return for err in case(OnError[TSource]): await aobv.athrow(err) return for x in case(OnCompleted): await aobv.aclose() return await matcher() return TailCall() asyncio.ensure_future(loop()) agent = MailboxProcessor.start(worker, token) async def fn(ns: Notification[TSource]) -> None: due_time = datetime.utcnow() + timedelta(seconds=seconds) agent.post((ns, due_time)) obv: AsyncNotificationObserver[ TSource] = AsyncNotificationObserver(fn) subscription = await source.subscribe_async(obv) async def cancel() -> None: log.debug("delay:cancel()") cts.cancel() await subscription.dispose_async() return AsyncDisposable.create(cancel) return AsyncAnonymousObservable(subscribe_async)
def test_token_cancelled_register_throws(): called: List[bool] = [] source = CancellationTokenSource.cancelled_source() with pytest.raises(ObjectDisposedException): with source: token = source.token token.register(lambda: called.append(True)) assert not called
def test_token_cancellation_register_unregister_works(): called: List[bool] = [] source = CancellationTokenSource() with source as _: token = source.token registration = token.register(lambda: called.append(True)) assert not called registration.dispose() assert not called
def test_token_disposing_works(): source = CancellationTokenSource() with source as disposable: token = source.token disposable.dispose() assert token.is_cancellation_requested with pytest.raises(ObjectDisposedException): token.throw_if_cancellation_requested()
def test_token_cancellation_works(): source = CancellationTokenSource() with source as token: token.throw_if_cancellation_requested() assert token.can_be_canceled assert not token.is_cancellation_requested assert token.is_cancellation_requested with pytest.raises(ObjectDisposedException): # type: ignore token.throw_if_cancellation_requested()
def test_token_source_works(): source = CancellationTokenSource() assert not source.is_cancellation_requested with source as disp: assert isinstance(disp, Disposable)