Bug 1768924 - Fix intermittent timeout in early hint test code by splitting up the test case r=necko-reviewers,dragana

Differential Revision: https://phabricator.services.mozilla.com/D146680
This commit is contained in:
Manuel Bucher 2022-05-24 09:09:36 +00:00
Родитель 3a97cb073e
Коммит 409fc3ee88
5 изменённых файлов: 259 добавлений и 205 удалений

Просмотреть файл

@ -80,5 +80,12 @@ support-files =
skip-if =
os == 'linux' && bits == 64 # Bug 1744028 and Bug 1746324
[browser_103_preload.js]
support-files =
early_hint_preload_test_helper.jsm
skip-if =
os == 'linux' && bits == 64 && !debug # Bug 1744028 and Bug 1746324
[browser_103_error.js]
support-files =
early_hint_preload_test_helper.jsm
skip-if =
os == 'linux' && bits == 64 && !debug # Bug 1744028 and Bug 1746324

Просмотреть файл

@ -0,0 +1,130 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
Services.prefs.setCharPref(
"dom.securecontext.allowlist",
"example.com,example.net"
);
Services.prefs.setBoolPref("network.early-hints.enabled", true);
const {
lax_request_count_checking,
test_hint_preload_internal,
test_hint_preload,
} = ChromeUtils.import(
"resource://testing-common/early_hint_preload_test_helper.jsm"
);
// 400 Bad Request
add_task(async function test_103_error_400() {
await test_hint_preload(
"test_103_error_400",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?400",
{ hinted: 1, normal: 1 }
);
});
// 401 Unauthorized
add_task(async function test_103_error_401() {
await test_hint_preload(
"test_103_error_401",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?401",
{ hinted: 1, normal: 1 }
);
});
// 403 Forbidden
add_task(async function test_103_error_403() {
await test_hint_preload(
"test_103_error_403",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?403",
{ hinted: 1, normal: 1 }
);
});
// 404 Not Found
add_task(async function test_103_error_404() {
await test_hint_preload(
"test_103_error_404",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?404",
{ hinted: 1, normal: 1 }
);
});
// 408 Request Timeout
add_task(async function test_103_error_408() {
await test_hint_preload(
"test_103_error_408",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?408",
{ hinted: 1, normal: 1 }
);
});
// 410 Gone
add_task(async function test_103_error_410() {
await test_hint_preload(
"test_103_error_410",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?410",
{ hinted: 1, normal: 0 }
);
});
// 429 Too Many Requests
add_task(async function test_103_error_429() {
await test_hint_preload(
"test_103_error_429",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?429",
{ hinted: 1, normal: 1 }
);
});
// 500 Internal Server Error
add_task(async function test_103_error_500() {
await test_hint_preload(
"test_103_error_500",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?500",
{ hinted: 1, normal: 1 }
);
});
// 502 Bad Gateway
add_task(async function test_103_error_502() {
await test_hint_preload(
"test_103_error_502",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?502",
{ hinted: 1, normal: 1 }
);
});
// 503 Service Unavailable
add_task(async function test_103_error_503() {
await test_hint_preload(
"test_103_error_503",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?503",
{ hinted: 1, normal: 1 }
);
});
// 504 Gateway Timeout
add_task(async function test_103_error_504() {
await test_hint_preload(
"test_103_error_504",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?504",
{ hinted: 1, normal: 1 }
);
});

Просмотреть файл

@ -1,8 +1,8 @@
"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { TelemetryTestUtils } = ChromeUtils.import(
"resource://testing-common/TelemetryTestUtils.jsm"
);
"use strict";
Services.prefs.setCharPref(
"dom.securecontext.allowlist",
@ -11,97 +11,13 @@ Services.prefs.setCharPref(
Services.prefs.setBoolPref("network.early-hints.enabled", true);
// TODO: remove this and do strict request count
async function lax_request_count_checking(testName, got, expected) {
// stringify to pretty print assert output
let g = JSON.stringify(got);
let e = JSON.stringify(expected);
// each early hint request can starts one hinted request, but doesn't yet
// complete the early hint request during the test case
await Assert.ok(
got.hinted <= expected.hinted,
`${testName}: unexpected amount of hinted request made expected at most ${expected.hinted} (${e}), got ${got.hinted} (${g})`
);
// when the early hint request doesn't complete fast enough, another request
// is currently sent from the main document
let expected_normal = expected.normal + expected.hinted;
await Assert.ok(
got.normal <= expected_normal,
`${testName}: unexpected amount of normal request made expected at most ${expected_normal} (${e}), got ${got.normal} (${g})`
);
}
async function test_hint_preload(
testName,
requestFrom,
imgUrl,
expectedRequestCount,
uuid = undefined
) {
// generate a uuid if none were passed
if (uuid == undefined) {
uuid = Services.uuid.generateUUID();
}
await test_hint_preload_internal(
testName,
requestFrom,
[[imgUrl, uuid.toString()]],
expectedRequestCount
);
}
// - testName is just there to be printed during Asserts when failing
// - the baseUrl can't have query strings, because they are currently used to pass
// the early hint the server responds with
// - urls are in the form [[url1, uuid1], ...]. The uuids are there to make each preload
// unique and not available in the cache from other test cases
// - expectedRequestCount is the sum of all requested objects { normal: count, hinted: count }
async function test_hint_preload_internal(
testName,
requestFrom,
imgUrls,
expectedRequestCount
) {
// reset the count
let headers = new Headers();
headers.append("X-Early-Hint-Count-Start", "");
await fetch(
"http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
{ headers }
);
let requestUrl =
requestFrom +
"/browser/netwerk/test/browser/early_hint_main_html.sjs?" +
new URLSearchParams(imgUrls).toString(); // encode the hinted images as query string
await BrowserTestUtils.withNewTab(
{
gBrowser,
url: requestUrl,
waitForLoad: true,
},
async function() {}
);
let gotRequestCount = await fetch(
"http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
).then(response => response.json());
// TODO: Switch to stricter counting method after fixing https://bugzilla.mozilla.org/show_bug.cgi?id=1753730#c11
await lax_request_count_checking(
testName,
gotRequestCount,
expectedRequestCount
);
/* stricter counting method:
await Assert.deepEqual(
gotRequestCount,
expectedRequestCount,
testName + ": Unexpected amount of requests made"
);
*/
}
const {
lax_request_count_checking,
test_hint_preload_internal,
test_hint_preload,
} = ChromeUtils.import(
"resource://testing-common/early_hint_preload_test_helper.jsm"
);
// TODO testing:
// * Abort main document load while early hint is still loading -> early hint should be aborted
@ -373,116 +289,6 @@ add_task(async function test_preload_csp_imgsrc_none() {
Services.cache2.clear();
});
// 400 Bad Request
add_task(async function test_103_error_400() {
await test_hint_preload(
"test_103_error_400",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?400",
{ hinted: 1, normal: 1 }
);
});
// 401 Unauthorized
add_task(async function test_103_error_401() {
await test_hint_preload(
"test_103_error_401",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?401",
{ hinted: 1, normal: 1 }
);
});
// 403 Forbidden
add_task(async function test_103_error_403() {
await test_hint_preload(
"test_103_error_403",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?403",
{ hinted: 1, normal: 1 }
);
});
// 404 Not Found
add_task(async function test_103_error_404() {
await test_hint_preload(
"test_103_error_404",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?404",
{ hinted: 1, normal: 1 }
);
});
// 408 Request Timeout
add_task(async function test_103_error_408() {
await test_hint_preload(
"test_103_error_408",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?408",
{ hinted: 1, normal: 1 }
);
});
// 410 Gone
add_task(async function test_103_error_410() {
await test_hint_preload(
"test_103_error_410",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?410",
{ hinted: 1, normal: 0 }
);
});
// 429 Too Many Requests
add_task(async function test_103_error_429() {
await test_hint_preload(
"test_103_error_429",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?429",
{ hinted: 1, normal: 1 }
);
});
// 500 Internal Server Error
add_task(async function test_103_error_500() {
await test_hint_preload(
"test_103_error_500",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?500",
{ hinted: 1, normal: 1 }
);
});
// 502 Bad Gateway
add_task(async function test_103_error_502() {
await test_hint_preload(
"test_103_error_502",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?502",
{ hinted: 1, normal: 1 }
);
});
// 503 Service Unavailable
add_task(async function test_103_error_503() {
await test_hint_preload(
"test_103_error_503",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?503",
{ hinted: 1, normal: 1 }
);
});
// 504 Gateway Timeout
add_task(async function test_103_error_504() {
await test_hint_preload(
"test_103_error_504",
"http://example.com",
"http://example.com/browser/netwerk/test/browser/early_hint_error.sjs?504",
{ hinted: 1, normal: 1 }
);
});
// Test that preloads in iframes don't get triggered
add_task(async function test_103_iframe() {
// reset the count

Просмотреть файл

@ -0,0 +1,110 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const EXPORTED_SYMBOLS = [
"lax_request_count_checking",
"test_hint_preload",
"test_hint_preload_internal",
];
const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
const { BrowserTestUtils } = ChromeUtils.import(
"resource://testing-common/BrowserTestUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { gBrowser } = Services.wm.getMostRecentWindow("navigator:browser");
// TODO: remove this and do strict request count
async function lax_request_count_checking(testName, got, expected) {
// stringify to pretty print assert output
let g = JSON.stringify(got);
let e = JSON.stringify(expected);
// each early hint request can starts one hinted request, but doesn't yet
// complete the early hint request during the test case
await Assert.ok(
got.hinted <= expected.hinted,
`${testName}: unexpected amount of hinted request made expected at most ${expected.hinted} (${e}), got ${got.hinted} (${g})`
);
// when the early hint request doesn't complete fast enough, another request
// is currently sent from the main document
let expected_normal = expected.normal + expected.hinted;
await Assert.ok(
got.normal <= expected_normal,
`${testName}: unexpected amount of normal request made expected at most ${expected_normal} (${e}), got ${got.normal} (${g})`
);
}
async function test_hint_preload(
testName,
requestFrom,
imgUrl,
expectedRequestCount,
uuid = undefined
) {
// generate a uuid if none were passed
if (uuid == undefined) {
uuid = Services.uuid.generateUUID();
}
await test_hint_preload_internal(
testName,
requestFrom,
[[imgUrl, uuid.toString()]],
expectedRequestCount
);
}
// - testName is just there to be printed during Asserts when failing
// - the baseUrl can't have query strings, because they are currently used to pass
// the early hint the server responds with
// - urls are in the form [[url1, uuid1], ...]. The uuids are there to make each preload
// unique and not available in the cache from other test cases
// - expectedRequestCount is the sum of all requested objects { normal: count, hinted: count }
async function test_hint_preload_internal(
testName,
requestFrom,
imgUrls,
expectedRequestCount
) {
// reset the count
let headers = new Headers();
headers.append("X-Early-Hint-Count-Start", "");
await fetch(
"http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
{ headers }
);
let requestUrl =
requestFrom +
"/browser/netwerk/test/browser/early_hint_main_html.sjs?" +
new URLSearchParams(imgUrls).toString(); // encode the hinted images as query string
await BrowserTestUtils.withNewTab(
{
gBrowser,
url: requestUrl,
waitForLoad: true,
},
async function() {}
);
let gotRequestCount = await fetch(
"http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
).then(response => response.json());
// TODO: Switch to stricter counting method after fixing https://bugzilla.mozilla.org/show_bug.cgi?id=1753730#c11
await lax_request_count_checking(
testName,
gotRequestCount,
expectedRequestCount
);
/* stricter counting method:
await Assert.deepEqual(
gotRequestCount,
expectedRequestCount,
testName + ": Unexpected amount of requests made"
);
*/
}

Просмотреть файл

@ -15,6 +15,7 @@ XPCSHELL_TESTS_MANIFESTS += [
]
TESTING_JS_MODULES += [
"browser/early_hint_preload_test_helper.jsm",
"unit/test_http3_prio_helpers.js",
]