isolate_handler icon indicating copy to clipboard operation
isolate_handler copied to clipboard

testing the library

Open matejthetree opened this issue 4 years ago • 3 comments

I am trying to write a test to check if the library works

void main() async {
  // WidgetsFlutterBinding.ensureInitialized();

  test('library works with primitives', () async {
    final isolates = IsolateHandler();

    var completer = Completer();
    // Set new count and display current count.
    void setCounter(int counter) {
      // We will no longer be needing the isolate, let's dispose of it.
      isolates.kill("counter");
      expectAsync0(() {
        expect(counter, 1);
      });
      completer.complete();
    }

    isolates.spawn<int>(entryPoint,
        name: "counter",
        // Executed every time data is received from the spawned isolate.
        onReceive: setCounter,
        // Executed once when spawned isolate is ready for communication.
        onInitialized: () => isolates.send(0, to: "counter"));

    await completer.future;
  });
}

void entryPoint(Map<String, dynamic> context) {
  // Calling initialize from the entry point with the context is
  // required if communication is desired. It returns a messenger which
  // allows listening and sending information to the main isolate.
  final messenger = HandledIsolate.initialize(context);

  // Triggered every time data is received from the main isolate.
  messenger.listen((req) {
    // Add one to the count and send the new value back to the main
    // isolate.
    if (req is int) {
      messenger.send(req++);
    }
  });
}

If I don't initialize widgets binding, I get Null check operator used on a null value

package:flutter/src/services/platform_channel.dart 142:86  MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 148:36  MethodChannel._invokeMethod
package:flutter/src/services/platform_channel.dart 331:12  MethodChannel.invokeMethod
package:flutter_isolate/flutter_isolate.dart 46:14         FlutterIsolate.spawn
package:isolate_handler/src/handled_isolate.dart 171:37    HandledIsolate._init
package:isolate_handler/src/handled_isolate.dart 99:5      new HandledIsolate
package:isolate_handler/isolate_handler.dart 178:22        IsolateHandler.spawn
test/src/util/isolate/isolate_handler_test.dart 25:14      main.<fn>
test/src/util/isolate/isolate_handler_test.dart 11:41      main.<fn>

And If I uncomment that line, I get MissingPluginException(No implementation found for method spawn_isolate on channel com.rmawatson.flutterisolate/control)

How to run this library inside tests?

matejthetree avatar May 13 '21 16:05 matejthetree

any info on this one, still trying to test the implementation. hard to use in production, if plugin may break anytime with new versions.

matejthetree avatar May 25 '21 12:05 matejthetree

No. I'm not an avid fan of such testing in cases like this (there are cases where things can be tested easily but I don't think this is one of them). If you can come up with a testing scenario that works and can be added, all right, but I'm unlikely to take any steps to add it myself. I'm not even sure a test environment works the same as the real one--don't forget, the main idea of flutter_isolate is to instantiate an extra Flutter engine in the first place.

deakjahn avatar May 25 '21 12:05 deakjahn

Same issue, but just in ios platform

spysoos avatar Dec 16 '21 04:12 spysoos