deakjahn / isolate_handler

Effortless isolates abstraction layer with support for MethodChannel calls.
https://pub.dev/packages/isolate_handler
MIT License
52 stars 16 forks source link

testing the library #23

Open matejthetree opened 3 years ago

matejthetree commented 3 years ago

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 commented 3 years ago

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

deakjahn commented 3 years ago

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.

spysoos commented 2 years ago

Same issue, but just in ios platform