LCOV - code coverage report

Current view
top level - src/_impl/native - _channel.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines465583.6%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:async';
2import 'dart:convert';
3import 'dart:isolate' as vm;
4
5import 'package:logger/web.dart';
6
7import '../../channel.dart' show Channel;
8import '../../channel.dart';
9import '../../exceptions/exception_manager.dart';
10import '../../exceptions/squadron_error.dart';
11import '../../exceptions/squadron_exception.dart';
12import '../../exceptions/worker_exception.dart';
13import '../../tokens/_squadron_cancelation_token.dart';
14import '../../typedefs.dart';
15import '../../worker/worker_request.dart';
16import '../../worker/worker_response.dart';
17import '../xplat/_disconnected_channel.dart';
18import '../xplat/_forward_stream_controller.dart';
19import '../xplat/_result_stream.dart';
20
21part '_channel_impl.dart';
22
23// Stub implementations.
24
25/// Starts an [Isolate] using the [entryPoint] and sends a start
26/// [WorkerRequest] with [startArguments]. The future completes after the
27/// worker [Isolate]'s main program has provided the [SendPort] via
28/// [WorkerChannel.connect].
2910Future<Channel> openChannel(
30 EntryPoint entryPoint,
31 ExceptionManager exceptionManager,
32 Logger? logger,
33 List startArguments, [
34 PlatformThreadHook? hook,
35]) async {
3610 final completer = Completer<Channel>();
37 Channel? channel;
38
3910 void failure(Object error, [StackTrace? stackTrace]) {
4010 if (!completer.isCompleted) {
416 completer.completeError(SquadronException.from(error, stackTrace));
42 }
43 }
44
459 void success(Channel channel) {
469 if (!completer.isCompleted) {
479 completer.complete(channel);
48 }
49 }
50
5110 final receiver = vm.ReceivePort();
5210 final exitPort = vm.ReceivePort();
5310 final errorPort = vm.ReceivePort();
54
5520 exitPort.listen((message) {
5620 failure(SquadronErrorExt.create('Connection to worker failed'));
572 logger?.t('Isolate terminated.');
589 channel?.close();
5910 receiver.close();
6010 errorPort.close();
6110 exitPort.close();
62 });
63
6410 errorPort.listen((message) {
65 SquadronException? error;
66 try {
670 final data = jsonDecode(message[0]);
680 if (data is List) {
690 error = exceptionManager.deserialize(data.cast<String>());
70 }
71 } catch (_) {
72 // not a String representing a SquadronException
73 }
74
750 error ??= WorkerException(
760 message[0],
770 SquadronException.loadStackTrace(message[1]),
78 );
79
800 logger?.d(() => 'Unhandled error from Isolate: ${error?.message}.');
810 failure(error);
82 });
83
8410 final disconnected = DisconnectedChannel(exceptionManager, logger);
85
8619 receiver.listen((message) {
879 final response = WorkerResponseExt.from(message);
889 if (!response.unwrapInPlace(disconnected)) {
89 return;
90 }
91
929 final error = response.error;
93 if (error != null) {
944 logger?.e(() => 'Connection to Isolate failed: ${response.error}');
952 failure(error);
969 } else if (response.endOfStream) {
971 logger?.w('Disconnecting from Isolate');
981 channel?.close();
999 } else if (!completer.isCompleted) {
1002 logger?.t('Connected to Isolate');
10118 channel = _VmChannel._(response.result, logger, exceptionManager);
1029 success(channel!);
103 } else {
1040 logger?.e(() => 'Unexpected response: $response');
105 }
106 });
107
10820 final startRequest = WorkerRequest.start(receiver.sendPort, startArguments);
10910 startRequest.wrapInPlace();
11010 final isolate = await vm.Isolate.spawn(
111 entryPoint,
112 startRequest,
113 errorsAreFatal: false,
11410 onExit: exitPort.sendPort,
11510 onError: errorPort.sendPort,
116 );
117
118 try {
11910 final channel = await completer.future;
1201 await hook?.call(isolate);
1212 logger?.t('Created Isolate');
122 return channel;
123 } catch (ex) {
1243 logger?.e(() => 'Connection to Isolate failed: $ex');
1253 isolate.kill(priority: vm.Isolate.beforeNextEvent);
126 rethrow;
127 }
128}
129
130/// Creates a [_VmChannel] from a [SendPort].
1313Channel? deserialize(PlatformChannel? channelInfo,
132 [Logger? logger, ExceptionManager? exceptionManager]) =>
133 (channelInfo == null)
134 ? null
1353 : _VmChannel._(
136 channelInfo,
137 logger,
1383 exceptionManager ?? ExceptionManager(),
139 );
Choose Features