LCOV - code coverage report

Current view
top level - src/tokens - _cancelation_token_ref.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines193948.7%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:async';
2
3import 'package:cancelation_token/cancelation_token.dart';
4
5import '../exceptions/squadron_canceled_exception.dart';
6import '../exceptions/squadron_error.dart';
7import '../worker/worker_request.dart';
8import '_squadron_cancelation_token.dart';
9
10/// Cancelation token reference. This special cancelation token is managed by the [WorkerMonitor] and is used to
11/// mirror cancelation tokens presented to Squadron by callers of a worker service. When a [WorkerRequest] is
12/// handled by the platform worker and that request is associated with a cancelation request, the [WorkerMonitor]
13/// will override the request's cancelation token with a [CancelationTokenReference]. The same cancelation may
14/// be used for several service calls, so the [WorkerMonitor] keeps a map of [CancelationTokenReference] and a
15/// reference count that is incremented for each [WorkerRequest] having the same cancelation token and decremented
16/// when processing is finished. When the reference count drops to 0 and the cancelation token was not canceled,
17/// the [CancelationTokenReference] is removed from the map.
18///
19/// When a caller cancels a token, a cancelation notification is sent to all workers in the pool and the
20/// corresponding [CancelationTokenReference] will be canceled. Services executing in the context of a platform
21/// worker will be able to inspect the token's status to interrupt processing gracefully. If the token's status is
22/// not inspected, processing will continue in platform workers, but will be interrupted on caller-side with a
23/// [CanceledException].
24class CancelationTokenReference implements SquadronCancelationToken {
2510 CancelationTokenReference._noToken()
26 : _hasRef = false,
27 id = '' {
2810 _refCount = 1;
29 }
30
31 @override
32 final String id;
33
34 /// Singleton token reference instance for requests containing no token.
3528 static final noToken = CancelationTokenReference._noToken();
36
37 /// Creates a new token reference for [id].
382 CancelationTokenReference(this.id) : _hasRef = true;
39
400 @override
410 SquadronCanceledException? get exception => _exception;
42 SquadronCanceledException? _exception;
43
440 @override
450 bool get isCanceled => (_exception != null);
46
470 @override
480 Future<CanceledException> get onCanceled => _completer.future;
491 final _completer = Completer<SquadronCanceledException>();
50
51 /// Whether this token reference instance has a reference. A token reference
52 /// without a reference does nothing.
53 final bool _hasRef;
54
55 /// Reference counter.
5619 int get refCount => _refCount;
57 int _refCount = 0;
58
59 /// Use this token reference for [request]. Increments the internal reference
60 /// counter and overrides [WorkerRequest.cancelToken] with this token.
619 void usedBy(WorkerRequest request) {
6210 if (_hasRef) {
634 _refCount++;
642 request.overrideCancelToken(this);
659 } else if (request.cancelToken != null) {
660 throw SquadronErrorExt.create('Token reference mismatch');
67 }
68 }
69
70 /// Release this token reference. Decrements the internal reference counter.
719 void release() {
7210 if (_hasRef) {
734 _refCount--;
74 }
75 }
76
772 void update(SquadronCancelationToken token) {
782 final ex = token.exception;
794 if (ex != null && !_completer.isCompleted) {
802 _exception = ex;
814 _completer.complete(ex);
82 }
83 }
84
850 @override
860 void ensureStarted() {/* nothing to do */}
87
880 @override
890 void throwIfCanceled() {
900 if (_exception != null) {
910 throw _exception!;
92 }
930 }
94
950 @override
960 Future<void> refreshAndThrowIfCanceled() => Future(throwIfCanceled);
97
980 @override
99 // no native cancelation token in workers
1000 CancelationToken? get token => throw UnimplementedError();
101
1020 @override
103 // serialization of token references
1040 List serialize() => throw UnimplementedError();
105}
Choose Features