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

[NoQA] e2e: terminate app only when all network requests were finished #43038

Merged
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
3 changes: 2 additions & 1 deletion src/libs/E2E/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config from '../../../tests/e2e/config';
import Routes from '../../../tests/e2e/server/routes';
import type {NetworkCacheMap, TestConfig, TestResult} from './types';
import {waitForActiveRequestsToBeEmpty} from './utils/NetworkInterceptor';

type NativeCommandPayload = {
text: string;
Expand Down Expand Up @@ -57,7 +58,7 @@ const submitTestResults = (testResult: TestResult): Promise<void> => {
});
};

const submitTestDone = () => fetch(`${SERVER_ADDRESS}${Routes.testDone}`, defaultRequestInit);
const submitTestDone = () => waitForActiveRequestsToBeEmpty().then(() => fetch(`${SERVER_ADDRESS}${Routes.testDone}`, defaultRequestInit));

let currentActiveTestConfig: TestConfig | null = null;

Expand Down
40 changes: 40 additions & 0 deletions src/libs/E2E/utils/NetworkInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @lwc/lwc/no-async-await */
import {DeviceEventEmitter} from 'react-native';
import type {NetworkCacheEntry, NetworkCacheMap} from '@libs/E2E/types';

const LOG_TAG = `[E2E][NetworkInterceptor]`;
Expand Down Expand Up @@ -93,6 +94,33 @@ function hashFetchArgs(args: Parameters<typeof fetch>) {
return `${url}${JSON.stringify(headers)}`;
}

let activeRequestsCount = 0;

const ACTIVE_REQUESTS_QUEUE_IS_EMPTY_EVENT = 'activeRequestsQueueIsEmpty';

/**
* Assures that ongoing network requests are empty. **Highly desirable** to call this function before closing the app.
* Otherwise if some requests are persisted - they will be executed on the next app start. And it can lead to a situation
* where we can have `N * M` requests (where `N` is the number of app run per test and `M` is the number of test suites)
* and such big amount of requests can lead to a situation, where first app run (in test suite to cache network requests)
* may be blocked by spinners and lead to unbelievable big time execution, which eventually will be bigger than timeout and
* will lead to a test failure.
*/
function waitForActiveRequestsToBeEmpty(): Promise<void> {
console.debug('Waiting for requests queue to be empty...', activeRequestsCount);

if (activeRequestsCount === 0) {
return Promise.resolve();
}

return new Promise((resolve) => {
const subscription = DeviceEventEmitter.addListener(ACTIVE_REQUESTS_QUEUE_IS_EMPTY_EVENT, () => {
subscription.remove();
resolve();
});
});
}

/**
* Install a network interceptor by overwriting the global fetch function:
* - Overwrites fetch globally with a custom implementation
Expand Down Expand Up @@ -145,6 +173,8 @@ export default function installNetworkInterceptor(
console.debug('!!! Missed cache hit for url:', url);
}

activeRequestsCount++;

return originalFetch(...args)
.then(async (res) => {
if (networkCache != null) {
Expand All @@ -166,6 +196,16 @@ export default function installNetworkInterceptor(
.then((res) => {
console.debug(LOG_TAG, 'Network cache updated!');
return res;
})
.finally(() => {
console.debug('Active requests count:', activeRequestsCount);

activeRequestsCount--;

if (activeRequestsCount === 0) {
DeviceEventEmitter.emit(ACTIVE_REQUESTS_QUEUE_IS_EMPTY_EVENT);
}
});
};
}
export {waitForActiveRequestsToBeEmpty};
1 change: 0 additions & 1 deletion tests/e2e/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export default {
TESTS_CONFIG: {
[TEST_NAMES.AppStartTime]: {
name: TEST_NAMES.AppStartTime,
warmupRuns: 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

is it correct that we removed that here? if so, 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.

@hannojg because on first app start we call OpenApp endpoint, but when we run app the second time we call ReconnectApp (and there is no cache for ReconnectApp).

Technically it doesn't make such big difference (for this particular test), because we'll update cache second time and it will not affect test results. But I thought it could be better to be consistent and run warmup for all tests two times.

// ... any additional config you might need
},
[TEST_NAMES.OpenChatFinderPage]: {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/testRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const runTests = async (): Promise<void> => {
// by default we do 2 warmups:
// - first warmup to pass a login flow
// - second warmup to pass an actual flow and cache network requests
const iterations = test.warmupRuns ?? 2;
const iterations = 2;
for (let i = 0; i < iterations; i++) {
// Warmup the main app:
await runTestIteration(config.MAIN_APP_PACKAGE, `[MAIN] ${warmupText}. Iteration ${i + 1}/${iterations}`);
Expand Down
Loading