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

Refactor methods in viewer into performance-impl #6252

Merged
merged 3 commits into from Nov 23, 2016

Conversation

muxin
Copy link
Contributor

@muxin muxin commented Nov 18, 2016

Part of #6159

@@ -257,6 +257,7 @@ var forbiddenTerms = {
whitelist: [
'src/service/viewer-impl.js',
'src/service/storage-impl.js',
'src/service/performance-impl.js',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erwinmombay I saw we have whitelist rule for sendMessage but not the sendMessageCancelUnsent (previously sendMessageUnreliable). Do we need to add rules for sendMessageCancelUnsent 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.

@dvoytenko WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

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

LGTM in general

});
});

it('should ignore all calls to tick', () => {
perf.tick('start0');
return perf.coreServicesAvailable().then(() => {
expect(tickSpy.callCount).to.equal(0);
expect(viewerSendMessageStub.withArgs('tick').callCount).to.equal(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

expect(x).to.not.be.called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

});
});

it('should ignore all calls to flush', () => {
perf.tick('start0');
perf.flush();
return perf.coreServicesAvailable().then(() => {
expect(flushTicksSpy.callCount).to.equal(0);
expect(viewerSendMessageCancelUnsentStub.withArgs('sendCsi')
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -221,7 +221,7 @@ export class Performance {
* @private
*/
setFlushParams_(params) {
this.viewer_.setFlushParams(params);
this.viewer_.sendMessageCancelUnsent('setFlushParams', params, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the false optional?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we make it optional - we need to change the default value to true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can make the default value to false, since most of the case is not waiting for response

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth doing a quick math. But I think most important calls actually do wait on response. We had a couple of annoying errors related to this. E.g.

return promise1.then(() => {
  return viewer.sendMessage('message', payload);
}).then(result => {
});

The expectation here is that it'll block on the message response. In actuality this block will not block and instead immediately yield.

This error was so bad that I made third argument to be required. I wonder if we should just split methods to sendMessage and sendMessageNoResult. This would remove the ugly Promise|undefined response type and make types more complete.

/cc @lannka

Copy link
Contributor

Choose a reason for hiding this comment

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

I argued for getting rid of the parameter entirely. Always return the promise, cause we generate it anyways.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dvoytenko 2 methods make sense. sendMessage and sendMessageAwaitResponse.

@jridgewell that's different, we still need to pass true|false to viewer to inform whether a response is expected. Simply removing the param at our side would not solve the problem @dvoytenko has mentioned.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can set it to true for everything?

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the boolean was introduced to reduce the unncessary response postMessage from search viewer. On the other hand, I'm also not sure if search viewer can handle true for every message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to create another PR for the refactoring of awaitResponse.
I'm not sure whether the viewer is respecting the true/false value set by us or just having predefined behavior depending on the eventType (postMessage for certain type of event without checking the awaitResponse param)

label,
from: opt_from,
value: opt_value,
});
}, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

This argument should be annotated everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

*/
flush() {
if (this.isMessagingReady_ && this.isPerformanceTrackingOn_) {
this.viewer_.flushTicks();
this.viewer_.sendMessageCancelUnsent('sendCsi', undefined, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto, const arguments need annotating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@muxin muxin force-pushed the refactor-performance-in-viewer branch from 750557f to d5603ab Compare November 21, 2016 21:46
// coreServicesAvailable calls flush once.
return perf.coreServicesAvailable().then(() => {
expect(flushTicksSpy.callCount).to.equal(1);
expect(viewerSendMessageCancelUnsentStub.withArgs('sendCsi')
.callCount).to.equal(1);
Copy link
Member

Choose a reason for hiding this comment

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

since we're in the process of making the test nicer too, can you try out the spy.should.have.callCount(n) syntax? https://github.com/domenic/sinon-chai

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean expect(spy).to.have.callCount(n)? I see that we are using expect everywhere, not spy.should.have....

Copy link
Contributor

Choose a reason for hiding this comment

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

would this work? expect(viewerSendMessageCancelUnsentStub.withArgs('sendCsi')).to.be.calledOnce

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lannka Later in this test we have callCount = 2, callCount = 3 ... It seems we still need to assert the callCount

@muxin muxin force-pushed the refactor-performance-in-viewer branch from d5603ab to 8e5478c Compare November 22, 2016 01:31
@muxin
Copy link
Contributor Author

muxin commented Nov 22, 2016

PTAL
For the awaitResponse refactoring, I am going to create another PR. I vote for having methods like sendMessage(eventType, data), sendMessageAwaitResponse(eventType, data), sendMessageCancelUnsent(eventType, data) and sendMessageCancelUnsentAwaitResponse(eventType, data)

@muxin
Copy link
Contributor Author

muxin commented Nov 22, 2016

@dvoytenko PTAL

@muxin muxin merged commit a908f23 into ampproject:master Nov 23, 2016
@muxin muxin deleted the refactor-performance-in-viewer branch November 23, 2016 01:42
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
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.

None yet

5 participants