Testing Dart class that runs code on Isolate

Issue

I have a Dart class that performs computations on Isolate. Here is my code:

class Mapper {
  SendPort _isolateSendPort;
  Isolate _isolate;

  Mapper() {
    _asyncInit();
  }

  void _asyncInit() async {
    final receivePort = ReceivePort();
    _isolate = await Isolate.spawn(
      _mappingFunction,
      receivePort.sendPort,
    );
    _isolateSendPort = await receivePort.first;
  }

  static void _mappingFunction(SendPort callerSendPort) {
    final newIsolateReceivePort = ReceivePort();
    callerSendPort.send(newIsolateReceivePort.sendPort);

    newIsolateReceivePort.listen((dynamic message) {
        final crossIsolatesMessage =
          message as CrossIsolatesMessage<Input>;

        // some computations...

        crossIsolatesMessage.sender.send(output);
    });
  }

  Future<Output> map(Input input) async {
    final port = ReceivePort();
    _isolateSendPort.send(CrossIsolatesMessage<Input>(
      sender: port.sendPort,
      message: input,
    ));
    return port.map((event) => event as Output).first;
  }

  void dispose() {
    _isolate?.kill(priority: Isolate.immediate);
    _isolate = null;
  }
}

class CrossIsolatesMessage<T> {
  final SendPort sender;
  final T message;

  CrossIsolatesMessage({
    @required this.sender,
    this.message,
  });
}

This code works well when I run Flutter app. But unit test for public method Future<Output> map(Input input) throws an error NoSuchMethodError which meens _isolateSendPort is null.

Here is the unit test code:

test('Mapper map', () {
  final sut = Mapper();
  final inputDummy = Input('123');
  final resultFuture = sut.map(inputDummy);
  final expectedResult = Output('321');
  expectLater(resultFuture, completion(expectedResult));
});

Here is an error:

NoSuchMethodError: The method 'send' was called on null.
Receiver: null
Tried calling: send(Instance of 'CrossIsolatesMessage<Input>')
dart:core                                                  Object.noSuchMethod

Why this error occurs in tests? And what is the right way to write tests for this class?

Solution

Problem solved.

Create of _isolate and _isolateSendPort is an async operation. Thats why _isolateSendPort was null on tests. Call method _asyncInit() from Mapper constructor is wrong way to create an isolate.

Here is working solution with lazy isolate initialization:

class Mapper {
  SendPort _isolateSendPort;
  Isolate _isolate;

  void _initIsolate() async {
    final receivePort = ReceivePort();
    _isolate = await Isolate.spawn(
      _mappingFunction,
      receivePort.sendPort,
    );
    _isolateSendPort = await receivePort.first;
  }

  ...

  Future<Output> map(Input input) async {
    final port = ReceivePort();
    if (_isolateSendPort == null) {
      await _initIsolate();
    }
    _isolateSendPort.send(CrossIsolatesMessage<Input>(
      sender: port.sendPort,
      message: input,
    ));
    return port.map((event) => event as Output).first;
  }

  ...
}

Answered By – Mol0ko

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.