test: Calling compute (so spawning new isolates) breaks tests
- Dominant language
- Dart
- Stars
- 12.5k
- Forks
- 3.4k
- PR merge metrics
- No merged PRs in 30d
Description
**Description**
It looks like the `compute` method breaks the `blocTest` utility when executed. Consider this code:
```dart
import 'dart:io';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
int intenseTask(String value) {
int len = 0;
for (var i = 0; i < 99999999; ++i) {
len = i;
}
sleep(const Duration(seconds: 1));
return value.length + len;
}
class Demo extends Bloc {
Demo() : super(0) {
on(_onString);
}
Future _onString(String value, Emitter emit) async {
if (value.isEmpty) {
emit(0);
} else {
final result = await compute(intenseTask, value);
emit(result);
}
}
}
void main() {
group('Demo test', () {
blocTest(
'Works',
build: () => Demo(),
act: (bloc) => bloc.add(''),
expect: () => const [0],
);
blocTest(
"Doesn't work",
build: () => Demo(),
act: (bloc) => bloc.add('using the compute method'),
verify: (bloc) => expect(bloc.state, greaterThan(0)),
);
});
}
```
The `'Works'` test runs fine since we aren't calling `compute` but `'Doesn't work'` fails with this error:
> Expected: a value greater than <0>
> Actual: <0>
> Which: is not a value greater than <0>
>
> Error resuming isolate isolates/2923108837404163: -32000 Bad state: The client closed with pending request "resume".
Am I missing something to make this work? Or is this a bug related to isolates and the bloc's running zone?
Contributor guide
Assessment
This issue has not been assessed yet.