Dart Event Queue: How to debug event queue?

2019-07-03 12:12发布

问题:

I'm writing some integration tests which utilize an HttpServer, a bunch of Directory().watch()'ers and possibly some other future/stream listening code.

I'm using the following test configuration:

class LaserServerTestConfiguration extends SimpleConfiguration {
  LaserServer _server;
  LaserServerTestConfiguration(this._server);
  void onDone(bool success) {
    super.onDone(success);
    _server.stop();
  }
}

And my tests look like this:

main() {
  var conf = new LaserConfiguration.fromPath('test/test_config.yaml');
  var server = new LaserServer(conf);
  unittestConfiguration = new LaserServerTestConfiguration(server);

  server.start().then((_) {

    test('test file changed event', () {
      var response = new Completer<Map>();
      response.future.then(expectAsync((e) =>
        expect(e, containsValue('test/sample/sample_test.html'))
      ));
      WebSocket.connect("ws://localhost:2014").then((ws) {
        ws.listen((msg) {
          response.complete(JSON.decode(msg));
          ws.close();
        });
        new File('test/sample/sample.dart').writeAsString("");
      });
    });

  });
}

The problem I have is that after the tests run (and pass) the Dart VM does not exit. Presumably because I still have something pending in the event queue.

How do I debug the event queue? I would like to see what is preventing the Dart VM from exiting at the end of the test run.

You can see in the custom TestConfiguration that I overload onDone(), this gets called and I stop() my server (.close(force: true) on HttpServer and loop all of my Directory().watch()'ers and cancel the subscriptions). But something is still hanging around...

回答1:

The observatory now (Dart VM version: 1.11.0-edge.131775) allows to investigate the message queue in the debugger view.