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

Ad test failures #8969

Merged
merged 5 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ describes.sandboxed('amp-ad-network-adsense-impl', {}, () => {
});
// Not using arrow function here because otherwise the way closure behaves
// prevents me from calling this.timeout(5000).
it('with multiple slots', function() {
// TODO(@tdrl, #8965): Make this pass reliably on Travis.
it.skip('with multiple slots', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about we only skip it on old chrome?

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 prefer this to make A4A fix it faster. 😈

Copy link
Contributor

Choose a reason for hiding this comment

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

LOL. so 😈 !

// When ran locally, this test tends to exceed 2000ms timeout.
this.timeout(5000);
// Reset counter for purpose of this test.
Expand Down
10 changes: 5 additions & 5 deletions extensions/amp-ad/0.1/test/test-amp-ad-3p-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('amp-ad-3p-impl', () => {
let sandbox;
let ad3p;
let win;
const whenFirstVisible = Promise.resolve();

beforeEach(() => {
sandbox = sinon.sandbox.create();
Expand All @@ -57,7 +58,7 @@ describe('amp-ad-3p-impl', () => {
ad3p.buildCallback();
// Turn the doc to visible so prefetch will be proceeded.
stubService(sandbox, win, 'viewer', 'whenFirstVisible')
.returns(Promise.resolve());
.returns(whenFirstVisible);
});
});

Expand Down Expand Up @@ -179,10 +180,10 @@ describe('amp-ad-3p-impl', () => {
});

describe('preconnectCallback', () => {
it('should add preconnect and prefech to DOM header', done => {
it('should add preconnect and prefech to DOM header', () => {
ad3p.buildCallback();
ad3p.preconnectCallback();
setTimeout(() => {
return whenFirstVisible.then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this the reason why the test is flaky? I can't see why

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could be 1 of two things:

Copy link
Contributor

Choose a reason for hiding this comment

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

Wow! that's a good catch!!!

Copy link
Contributor

Choose a reason for hiding this comment

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

@jridgewell could you further explain what does "setTimeout is throttled" mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Chrome throttles weirdly. 🤷‍♂️

let fetches = win.document.querySelectorAll('link[rel=prefetch]');
if (!fetches.length) {
fetches = win.document.querySelectorAll('link[rel=preload]');
Expand All @@ -197,8 +198,7 @@ describe('amp-ad-3p-impl', () => {
win.document.querySelectorAll('link[rel=preconnect]');
expect(preconnects[preconnects.length - 1]).to.have.property('href',
'https://testsrc/');
done();
}, 0);
});
});
});

Expand Down
71 changes: 39 additions & 32 deletions extensions/amp-ad/0.1/test/test-amp-ad-xorigin-iframe-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ describe('amp-ad-xorigin-iframe-handler', () => {
sentinel: 'amp3ptest' + testIndex,
});
const expectResponsePromise = iframe.expectMessageFromParent(
'amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
'embed-size-changed');
const renderStartPromise = signals.whenSignal('render-start');
return Promise.all([renderStartPromise, initPromise]).then(() => {
return initPromise;
}).then(() => {
expect(iframe.style.visibility).to.equal('');
return expectResponsePromise;
}).then(data => {
expect(data).to.jsonEqual({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
});
});
});

Expand Down Expand Up @@ -263,12 +263,13 @@ describe('amp-ad-xorigin-iframe-handler', () => {
type: 'send-embed-state',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
inViewport: false,
pageHidden: false,
type: 'embed-state',
sentinel: 'amp3ptest' + testIndex,
}));
return iframe.expectMessageFromParent('embed-state').then(data => {
expect(data).to.jsonEqual({inViewport: false,
pageHidden: false,
type: 'embed-state',
sentinel: 'amp3ptest' + testIndex,
});
});
});

it('should be able to use embed-size API, change size deny', () => {
Expand All @@ -285,12 +286,14 @@ describe('amp-ad-xorigin-iframe-handler', () => {
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-denied',
sentinel: 'amp3ptest' + testIndex,
}));
return iframe.expectMessageFromParent('embed-size-denied').then(data => {
expect(data).to.jsonEqual({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-denied',
sentinel: 'amp3ptest' + testIndex,
});
});
});

it('should be able to use embed-size API, change size succeed', () => {
Expand All @@ -307,12 +310,14 @@ describe('amp-ad-xorigin-iframe-handler', () => {
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
return iframe.expectMessageFromParent('embed-size-changed').then(data => {
expect(data).to.jsonEqual({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
});
});
});

it('should be able to use embed-size API to resize height only', () => {
Expand All @@ -328,12 +333,14 @@ describe('amp-ad-xorigin-iframe-handler', () => {
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: undefined,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
return iframe.expectMessageFromParent('embed-size-changed').then(data => {
expect(data).to.jsonEqual({
requestedWidth: undefined,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
});
});
});
});
});
3 changes: 2 additions & 1 deletion src/iframe-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ function getSentinel_(iframe, opt_is3P) {
* @param {*} data
* @returns {?Object} object message
* @private
* @visibleForTesting
*/
function parseIfNeeded(data) {
export function parseIfNeeded(data) {
if (typeof data == 'string') {
if (data.charAt(0) == '{') {
data = tryParseJson(data, e => {
Expand Down
31 changes: 29 additions & 2 deletions test/functional/test-iframe-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('test-iframe-createIframeWithMessageStub', () => {
const data1 = {
foo: 'bar',
test: true,
type: 'test',
};

const data2 = {
Expand Down Expand Up @@ -57,10 +58,36 @@ describe('test-iframe-createIframeWithMessageStub', () => {

it('should echo back message to parent window', () => {
iframe.contentWindow.postMessage(data1, '*');
return iframe.expectMessageFromParent(data1).then(() => {
return iframe.expectMessageFromParent('test').then(() => {
iframe.contentWindow.postMessage(data2, '*');
return iframe.expectMessageFromParent((data, msg) => {
expect(data).to.jsonEqual(data2);
expect(msg).to.jsonEqual(data2);
return true;
});
}).then(() => {
iframe.expectMessageFromParent(data2);
iframe.contentWindow.postMessage('test-' + JSON.stringify(data2), '*');
return iframe.expectMessageFromParent((data, msg) => {
expect(data).to.equal(null);
expect(msg).to.equal('test-' + JSON.stringify(data2));
return true;
});
}).then(() => {
iframe.contentWindow.postMessage('amp-' + JSON.stringify(data2), '*');
return iframe.expectMessageFromParent((data, msg) => {
expect(data).to.jsonEqual(data2);
expect(msg).to.equal('amp-' + JSON.stringify(data2));
return true;
});
}).then(() => {
iframe.contentWindow.postMessage(data2, '*');
return iframe.expectMessageFromParent(() => {
throw new Error('test');
}).then(() => {
throw new Error('should not get here');
}, () => {
expect(true).to.equal(true);
});
});
});
});
43 changes: 30 additions & 13 deletions testing/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {FakeLocation} from './fake-dom';
import {ampdocServiceFor} from '../src/ampdoc';
import {cssText} from '../build/css';
import {deserializeMessage, isAmpMessage} from '../src/3p-frame-messaging';
import {parseIfNeeded} from '../src/iframe-helper';
import {
installAmpdocServices,
installRuntimeServices,
Expand Down Expand Up @@ -318,26 +319,42 @@ export function createIframeWithMessageStub(win) {
/**
* Returns a Promise that resolves when the iframe acknowledged the reception
* of the specified message.
* @param {function(?Object, !Object|string)|string} callbackOrType
* A callback that determines if this is the message we expected. If a
* string is passed, the determination is based on whether the message's
* type matches the string.
*/
element.expectMessageFromParent = msg => {
return new Promise(resolve => {
const listener = event => {
let expectMsg = msg;
let actualMsg = event.data.receivedMessage;
if (typeof expectMsg !== 'string') {
expectMsg = JSON.stringify(expectMsg);
actualMsg = JSON.stringify(actualMsg);
element.expectMessageFromParent = (callbackOrType) => {
let filter;
if (typeof callbackOrType === 'string') {
filter = (data) => {
return 'type' in data && data.type == callbackOrType;
};
} else {
filter = callbackOrType;
}

return new Promise((resolve, reject) => {
function listener(event) {
if (event.source != element.contentWindow || !event.data.testStubEcho) {
return;
}
if (event.source == element.contentWindow
&& event.data.testStubEcho
&& expectMsg == actualMsg) {
const message = event.data.receivedMessage;
const data = parseIfNeeded(message);
try {
if (filter(data, message)) {
win.removeEventListener('message', listener);
resolve(data || message);
}
} catch (e) {
win.removeEventListener('message', listener);
resolve(msg);
reject(e);
}
};
}
win.addEventListener('message', listener);
});
};

return element;
}

Expand Down