Skip to content

testing the library #23

@matejthetree

Description

@matejthetree

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions