Skip to content

Promisification #39

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

Closed
wants to merge 19 commits into from
Closed

Conversation

vadimdemedes
Copy link
Contributor

This PR is related to #38. After this PR is complete and callbacks are removed to the maximum extent, AVA's code should be much cleaner.

Overview

As stated in #38, pinkie-promise and pify modules are used in this PR.

Changes

  • Promisify Runner.prototype.serial
  • Promisify Runner.prototype.concurrent
  • Promisify Runner.prototype.run
  • Promisify Test.prototype.run
  • Use promises in tests

As usual, let me know what you think and let's discuss this!

@sindresorhus
Copy link
Member

Use mz module when promises are needed for core modules (e.g. fs)

You can just use pify for that. See pify.all().

@vadimdemedes
Copy link
Contributor Author

Don't know why, but I knew you would say that, haha

@vadimdemedes
Copy link
Contributor Author

@sindresorhus this PR is ready for you to review, let me know what you think ;)

@@ -1,8 +1,10 @@
'use strict';
var Promise = require('pinkie-promise');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually. I think it's worth using https://github.com/petkaantonov/bluebird here for performance reasons.

this._timeStart = Date.now();
return new Promise(function (resolve, reject) {
this.promise.resolve = resolve;
this.promise.reject = reject;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really think of a better way right now, but this feels weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does. My goal was to switch to promises with a minimum code changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Can you at least add a TODO in there so it's not forgotten that it needs improving?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure

}.bind(this));
}.bind(this), cb);
Runner.prototype.serial = function (tests) {
return Promise.resolve(tests).each(function (test) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inner function can be DRYed up as the Runner.prototype.concurrent has the exact same function content.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thought of that too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to get a quick feedback on the PR as a whole

@sindresorhus
Copy link
Member

You can also remove pinkie-promise from the tests and devDependency and use Bluebird there too.

@vadimdemedes
Copy link
Contributor Author

@sindresorhus Right, good catch, missed that

@sindresorhus
Copy link
Member

The code base is so much cleaner with promises. Excellent work @vdemedes :)

Gonna give it a more thorough review tomorrow. Need sleep now.

@vadimdemedes
Copy link
Contributor Author

Thank you @sindresorhus ;)

@sindresorhus
Copy link
Member

I tried running this on pageres and got an error:

Unhandled rejection TypeError: Cannot read property 'testCount' of undefined
    at exit (/Users/sindresorhus/dev/ava/index.js:52:11)
    at tryCatcher (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/promise.js:503:31)
    at Promise._settlePromiseAt (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/promise.js:577:18)
    at Promise._settlePromises (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/promise.js:693:14)
    at Async._drainQueue (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/sindresorhus/dev/ava/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:371:17)

It seems the stats and results arguments in the exit function are undefined.

(Can you add a regression test for this when fixed?)

if (err) {
this.stats.failCount++;
}
Runner.prototype.addTestResult = function (test) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a private method, meaning prefixed with _: Runner.prototype._addTestResult

@vadimdemedes
Copy link
Contributor Author

@sindresorhus Trying to figure out the right way to test index.js. It is a bit problematic, since it exposes only functions to add tests, so I can't test it without child_process.exec. What do you think?

@sindresorhus
Copy link
Member

Using exec is fine.

@vadimdemedes
Copy link
Contributor Author

@sindresorhus Pushed critical fixes and updated es6 test to use index.js, instead of lib/test.js directly. Also, pageres tests succeed now:

screen shot 2015-09-07 at 1 49 22 pm

But now we have another problem. There's no continuous output, meaning that all output is displayed at the end, when all tests finish. This happens, because Promise.all (https://github.com/sindresorhus/ava/pull/39/files#diff-b3b53682a18f203ac8d29b0e277cad26R56) resolves only when all promises are resolved.

@vadimdemedes
Copy link
Contributor Author

First thing that I came up with to fix that is to use each-async again, instead of Promise.all. Maybe you have some other solution in mind?

@vadimdemedes
Copy link
Contributor Author

@sindresorhus Oh my God, I know how to fix this, never mind ;)

@sindresorhus
Copy link
Member

Yeah, let's use each-async for now. Add a TODO about improving it though.

@vadimdemedes
Copy link
Contributor Author

I came up with a much better solution and without use of each-async ;) Just a few seconds and you'll see the commit!

@vadimdemedes
Copy link
Contributor Author

Here's that change: https://github.com/sindresorhus/ava/pull/39/files#diff-b3b53682a18f203ac8d29b0e277cad26R52.

The logic here is to output test results immediately, as they are available. But, use Promise.all to return from Runner#concurrent, when all the promises are resolved (tests are completed).

sindresorhus pushed a commit that referenced this pull request Sep 8, 2015
Close #39 PR
Fixes #38
@sindresorhus
Copy link
Member

Awesome! I'm really happy about this change. Super work @vdemedes :D

Would you happen to be interested in joining the project? I think you have a lot of good to bring to AVA.

687474703a2f2f7777772e7265616374696f6e676966732e636f6d2f77702d636f6e74656e742f67616c6c6572792f64616e63652d70617274792f74756d626c725f6d306f73727268714d6b31717a6376376e6f345f3235302e676966

@Qix-
Copy link
Contributor

Qix- commented Sep 8, 2015

👍 for @vdemedes on the project. Lots of great stuff there.

@vadimdemedes
Copy link
Contributor Author

@sindresorhus @Qix- Thank you, guys!

Yes, I am definitely interested in joining AVA! I really like it and would love to contribute more!

sindresorhus pushed a commit that referenced this pull request Sep 8, 2015
Close #39 PR
Fixes #38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants