Skip to content

Commit 5f8f9ca

Browse files
Fix CancellableCompute web implementation
1 parent 0e7b355 commit 5f8f9ca

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

lib/src/cancellables/cancellable_compute.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dart:async';
22

33
import 'package:cancellation_token/cancellation_token.dart';
44
import 'cancellable_compute/cancellable_compute_stub.dart'
5-
if (dart.library.html) 'cancellable_compute/cancellable_compute_browser.dart'
5+
if (dart.library.html) 'cancellable_compute/cancellable_compute_web.dart'
66
if (dart.library.io) 'cancellable_compute/cancellable_compute_io.dart';
77

88
/// A cancellable implementation of Flutter's `compute()` method.

test/src/cancellables/cancellable_compute_test.dart renamed to test/src/cancellables/cancellable_compute_io_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@TestOn('vm')
2+
13
import 'dart:async';
24
import 'dart:io';
35

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
@TestOn('chrome')
2+
3+
import 'dart:async';
4+
5+
import 'package:cancellation_token/cancellation_token.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
test('completes with normal result if not cancelled', () {
10+
final CancellationToken token = CancellationToken();
11+
12+
expect(
13+
cancellableCompute(_successIsolateTest, 'Test string', token),
14+
completion(equals('Test string')),
15+
);
16+
});
17+
18+
test('completes with normal result if cancellation token is null', () {
19+
expect(
20+
cancellableCompute(_successIsolateTest, 'Test string', null),
21+
completion(equals('Test string')),
22+
);
23+
});
24+
25+
test('completes with exception if not cancelled and isolate callback throws',
26+
() async {
27+
final CancellationToken token = CancellationToken();
28+
final Exception testException = _TestException();
29+
30+
expect(
31+
cancellableCompute(_errorIsolateTest, testException, token),
32+
throwsA(isA<Exception>()),
33+
);
34+
});
35+
36+
test(
37+
'completes with exception if cancellation token is null and isolate '
38+
'callback throws', () async {
39+
final Exception testException = _TestException();
40+
41+
expect(
42+
cancellableCompute(_errorIsolateTest, testException, null),
43+
throwsA(isA<Exception>()),
44+
);
45+
});
46+
47+
test('detaches from the cancellation token after completing with a value',
48+
() async {
49+
final CancellationToken token = CancellationToken();
50+
51+
await cancellableCompute(_successIsolateTest, 'Test string', null);
52+
53+
expect(token.hasCancellables, isFalse);
54+
});
55+
56+
test('detaches from the cancellation token after completing with an error',
57+
() async {
58+
final CancellationToken token = CancellationToken();
59+
final Exception testException = _TestException();
60+
61+
try {
62+
await cancellableCompute(_errorIsolateTest, testException, null);
63+
} catch (e) {
64+
//
65+
}
66+
67+
expect(token.hasCancellables, isFalse);
68+
});
69+
70+
group('completes with a CancelledException', () {
71+
test('when cancelled before attaching', () {
72+
final CancellationToken token = CancellationToken()..cancel();
73+
74+
expect(
75+
cancellableCompute(_successIsolateTest, 'Test string', token),
76+
throwsA(isA<CancelledException>()),
77+
);
78+
});
79+
80+
test('when cancelled after attaching', () {
81+
final CancellationToken token = CancellationToken();
82+
final Future<String> result =
83+
cancellableCompute(_infiniteLoopIsolateTest, 'Test string', token);
84+
85+
expect(result, throwsA(isA<CancelledException>()));
86+
87+
token.cancel();
88+
});
89+
});
90+
}
91+
92+
Future<String> _successIsolateTest(String input) async {
93+
await Future.delayed(Duration(milliseconds: 100));
94+
return input;
95+
}
96+
97+
Future<String> _errorIsolateTest(Exception input) async {
98+
await Future.delayed(Duration(milliseconds: 100));
99+
throw input;
100+
}
101+
102+
Future<String> _infiniteLoopIsolateTest(String input) async {
103+
while (true) {
104+
await Future.delayed(Duration(milliseconds: 100));
105+
}
106+
}
107+
108+
class _TestException implements Exception {}

0 commit comments

Comments
 (0)