Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest should wait until all scheduled activities complete before ending a test #15094

Closed
DartBot opened this issue Nov 15, 2013 · 10 comments
Closed
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 15, 2013

This issue was originally filed by Samuel....@gmail.com


What steps will reproduce the problem?

  test("Protect async", () {
    Timer.run(protectAsync0(() => expect(true, isFalse)));
  });

What is the expected output?

Failed tests.

What do you see instead?

unittest-suite-wait-for-done
PASS: Protect async

All 1 tests passed.
unittest-suite-success

What version of the product are you using? On what operating system?

Linux, Dart 1.0.0.3_r30188

@sethladd
Copy link
Contributor

I think you need an expectAsync call in there. See https://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests


Added Area-Library, Library-UnitTest, NeedsInfo labels.

@DartBot
Copy link
Author

DartBot commented Nov 18, 2013

This comment was originally written by Samuel....@gmail.com


Ok, and how to write the test, that the callback will never get called?

What is the purpose of protectAsync then? I am confused, I see no added value in wrapping function in protectAsync, if it isn't able to handle async calls.

Reading the example at https://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests

test('two callbacks', () {
  my_function(/* ... */,
    successCallback:
        expectAsync1(() {}),
    errorCallback:
        protectAsync1((e) =>
            expect(true, isFalse, reason: 'Should not be reached')));
});

I thought that protectAsync can be used to ensure, that callback won't be called.

expectAsync works perfectly in assuring, that the callback gets called. However, I want to assure exact opposite.

@lrhn
Copy link
Member

lrhn commented Dec 16, 2013

Removed Area-Library label.
Added Area-Pkg label.

@lrhn
Copy link
Member

lrhn commented Dec 16, 2013

Removed Area-Pkg, Library-UnitTest labels.
Added Area-UnitTest label.

@kevmoo
Copy link
Member

kevmoo commented Feb 12, 2014

Removed Area-UnitTest label.
Added Area-Pkg, Pkg-Unittest labels.

@DartBot
Copy link
Author

DartBot commented Feb 19, 2014

This comment was originally written by Samuel.Hapa...@gmail.com


Have tested with new dart sdk 1.2 and unittest 0.10.0, nothing has changed.

However, I see currently, that protectAsync is deprecated. What is then new recommended way to assure some asynchronous callback never gets called?

@kevmoo
Copy link
Member

kevmoo commented Feb 19, 2014

test('two callbacks', () {
  my_function(/* ... */,
    successCallback:
        expectAsync1(() {}),
    errorCallback: (_) => fail('Should not be reached');
});

The errorCallback will be called (it is is invoked) within the correct test context.

@kevmoo
Copy link
Member

kevmoo commented Feb 19, 2014

The core issue here: once the last test completes, the test runner finishes and the host process is terminated.

If the last test (or only test) schedules async work it might never be reported if the test system finishes.

As a work-around you can return a future from the test with a delay.

It might be possible to implement this as a feature in unittest, but I'm worried about the introduced complexit.


Removed Type-Defect label.
Added Type-Enhancement, Triaged labels.
Changed the title to: "unittest should wait until all scheduled activities complete before ending a test".

@DartBot
Copy link
Author

DartBot commented Feb 20, 2014

This comment was originally written by samuel....@vacuumapps.com


I have also tried

test("Protect call by throwing Error", () {
    new Timer(new Duration(seconds: 1), () => throw new Error());
    new Future.delayed(new Duration(seconds: 3), () => throw new Error());
  });

But with no success. After looking through the source codes, I found that unittest simply ignores all errors that happen after the test suite ends.

This should be easily fixable to at least print which test failed for what reason.

However, it want help if you run tests using hop runner, which simply calls exit() which terminates all scheduled activities. But that is problem with hop runner.

It seems, it would be great if runZoned could return some Future that completes when everything in zone finishes. We could use it then to determine whether the tests are still running.

@DartBot DartBot added Type-Enhancement area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. labels Feb 20, 2014
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

This issue has been moved to dart-lang/test#236.

@DartBot DartBot closed this as completed Jun 5, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants