Dart test complex async

59 Views Asked by At

I'm struggling with testing the piece of functionality that is highly asynchronous . Overal idea is following:

  • function A puts data to local storage
  • function B monitors local storage and syncs changes to remote storage

So I need to test the function A and ensure that data not only put to local storage but also synced to remote one. My test looks like that:

    test("Sending data", () async {
      final comm = await Client.create("client1");
      final result = await comm.sendData('client2');
      expect(result, isTrue);
      SyncService.sync(onDone: () async {
        final rComm = await Client.create('client2');
        final keys = await rComm.getData();
        expect(keys.length, 1); // <------ never does that
      });
    });

The problem is that test execution doesn't wait for onDone callback to be called. The SyncService.sync() function is synchronous and just puts a sync request to the queue. The queue processing function is async. So if I understand Dart execution flow correctly it's put to the end of microtasks queue. My task then would be to put checking of the synchronisation result to the end of microtasks queue after SyncService.sync() is called. But in any case the test would finish first and microtasks that are scheduled afterwards wouldn't be tested anyway.

So how to handle that?

0

There are 0 best solutions below