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

core(driver): warn about remaining inflight requests urls #14963

Merged
merged 1 commit into from
May 4, 2023
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
12 changes: 12 additions & 0 deletions core/gather/driver/wait-for-condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,18 @@ async function waitForFullyLoaded(session, networkMonitor, options) {
throw new LighthouseError(LighthouseError.errors.PAGE_HUNG);
}

// Log remaining inflight requests if any.
const inflightRequestUrls = networkMonitor
.getInflightRequests()
.map((request) => request.url);
if (inflightRequestUrls.length > 0) {
log.warn(
'waitFor',
'Remaining inflight requests URLs',
inflightRequestUrls
);
}

return {timedOut: true};
};
});
Expand Down
29 changes: 27 additions & 2 deletions core/test/gather/driver/wait-for-condition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import log from 'lighthouse-logger';

import * as wait from '../../../gather/driver/wait-for-condition.js';
import {
mockCommands,
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('waitForFullyLoaded()', () => {

beforeEach(() => {
session = {sendCommand: fnAny().mockResolvedValue(), setNextProtocolTimeout: fnAny()};
networkMonitor = {};
networkMonitor = {getInflightRequests: fnAny().mockReturnValue([])};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the recommended approach to mocking log to test if it has been called correctly? I found importMock, but it doesn't seem to support mocking external modules.

Copy link
Member

@brendankenny brendankenny Apr 19, 2023

Choose a reason for hiding this comment

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

log.events will emit events when something is logged (edit, well, log.warned, at least), so you can collect those events and assert those without mocking. One example:

/** @type {Array<unknown>} */
const warnings = [];
/** @param {unknown} evt */
const saveWarning = evt => warnings.push(evt);
log.events.on('warning', saveWarning);
const filtered = filters.filterConfigByExplicitFilters(resolvedConfig, {
onlyAudits: null,
onlyCategories: ['timespan', 'thisIsNotACategory'],
skipAudits: null,
});
log.events.off('warning', saveWarning);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if the test is fine or you would like something more and I'll add it :D
codecov seems to be complaining for some reason though as if it didn't catch the test :/

const overrides = {
waitForFcp: createMockWaitForFn(),
Expand Down Expand Up @@ -179,10 +181,17 @@ describe('waitForFullyLoaded()', () => {
expect(await loadPromise).toMatchObject({timedOut: false});
});

it('should timeout when not resolved fast enough', async () => {
it('should timeout and warn when not resolved fast enough', async () => {
options._waitForTestOverrides.waitForLoadEvent = createMockWaitForFn();
options._waitForTestOverrides.waitForNetworkIdle = createMockWaitForFn();
options._waitForTestOverrides.waitForCPUIdle = createMockWaitForFn();
networkMonitor.getInflightRequests.mockReturnValue([{url: 'https://example.com'}]);

/** @type {Array<unknown>} */
const warnings = [];
/** @param {unknown} evt */
const saveWarning = evt => warnings.push(evt);
log.events.on('warning', saveWarning);

const loadPromise = makePromiseInspectable(wait.waitForFullyLoaded(
session,
Expand All @@ -203,6 +212,22 @@ describe('waitForFullyLoaded()', () => {
expect(options._waitForTestOverrides.waitForLoadEvent.getMockCancelFn()).toHaveBeenCalled();
expect(options._waitForTestOverrides.waitForNetworkIdle.getMockCancelFn()).toHaveBeenCalled();
expect(options._waitForTestOverrides.waitForCPUIdle.getMockCancelFn()).toHaveBeenCalled();
// Check for warn logs
log.events.off('warning', saveWarning);
expect(warnings).toEqual([
[
'waitFor',
'Timed out waiting for page load. Checking if page is hung...',
],
[
'waitFor',
'Remaining inflight requests URLs',
[
'https://example.com',
],
],
]);

expect(await loadPromise).toMatchObject({timedOut: true});
});

Expand Down