From a0c1d5d7f756e7c7aa7a0a2cd7a8f27bd65e4828 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 21 Feb 2025 11:49:36 -0700 Subject: [PATCH 1/6] Change offline status when Pusher PINGPONG fails --- src/libs/actions/User.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 574225b20f38..38f3d8e36667 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -57,6 +57,7 @@ import {openOldDotLink} from './Link'; import {showReportActionNotification} from './Report'; import {resendValidateCode as sessionResendValidateCode} from './Session'; import Timing from './Timing'; +import NetworkConnection from '@libs/NetworkConnection'; let currentUserAccountID = -1; let currentEmail = ''; @@ -915,6 +916,7 @@ function subscribeToPusherPong() { // When any PONG event comes in, reset this flag so that checkforLatePongReplies will resume looking for missed PONGs pongHasBeenMissed = false; + NetworkConnection.setOfflineStatus(false, 'PONG event was recieved from the server so assuming that means the client is back online'); }); } @@ -938,6 +940,9 @@ function pingPusher() { const pingID = NumberUtils.rand64(); const pingTimestamp = Date.now(); + // Reset this flag so that checkforLatePongReplies will resume looking for missed PONGs (in the case we are coming back online after being offline for a bit) + pongHasBeenMissed = false; + // In local development, there can end up being multiple intervals running because when JS code is replaced with hot module replacement, the old interval is not cleared // and keeps running. This little bit of logic will attempt to keep multiple pings from happening. if (pingTimestamp - lastPingSentTimestamp < PING_INTERVAL_LENGTH_IN_SECONDS * 1000) { @@ -972,6 +977,7 @@ function checkforLatePongReplies() { // When going offline, reset the pingpong state so that when the network reconnects, the client will start fresh lastPingSentTimestamp = Date.now(); pongHasBeenMissed = true; + NetworkConnection.setOfflineStatus(true, 'PONG event was no recieved from the server in time so assuming that means the client is offline'); } else { Log.info(`[Pusher PINGPONG] Last PONG event was ${timeSinceLastPongReceived} ms ago so not going offline`); } From 01fc003b5de3c64b6cd3ee78ce200262cb2fb6b1 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 21 Feb 2025 12:51:51 -0700 Subject: [PATCH 2/6] Change import order --- src/libs/actions/User.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 38f3d8e36667..33c8350d3a12 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -31,6 +31,7 @@ import Navigation from '@libs/Navigation/Navigation'; import {isOffline} from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; import * as NumberUtils from '@libs/NumberUtils'; +import NetworkConnection from '@libs/NetworkConnection'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import Pusher from '@libs/Pusher'; import type {PingPongEvent} from '@libs/Pusher/types'; @@ -57,7 +58,6 @@ import {openOldDotLink} from './Link'; import {showReportActionNotification} from './Report'; import {resendValidateCode as sessionResendValidateCode} from './Session'; import Timing from './Timing'; -import NetworkConnection from '@libs/NetworkConnection'; let currentUserAccountID = -1; let currentEmail = ''; From eea519b1ed1e9fccf25e47e5cbc4ae5245d4062f Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 21 Feb 2025 13:30:55 -0700 Subject: [PATCH 3/6] Moving import --- src/libs/actions/User.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 33c8350d3a12..25f1130c40e0 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -30,8 +30,8 @@ import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; import {isOffline} from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; -import * as NumberUtils from '@libs/NumberUtils'; import NetworkConnection from '@libs/NetworkConnection'; +import * as NumberUtils from '@libs/NumberUtils'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import Pusher from '@libs/Pusher'; import type {PingPongEvent} from '@libs/Pusher/types'; From de6559f2fe59cb658e669009492202d8d8d1e75c Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 25 Feb 2025 08:58:02 -0700 Subject: [PATCH 4/6] Keep pusher from timing out in the tests --- jest/setup.ts | 7 +++++++ src/libs/actions/User.ts | 3 +++ 2 files changed, 10 insertions(+) diff --git a/jest/setup.ts b/jest/setup.ts index 2fc4d289471c..30b90eeb4251 100644 --- a/jest/setup.ts +++ b/jest/setup.ts @@ -89,6 +89,13 @@ jest.mock('@src/libs/actions/Timing', () => ({ end: jest.fn(), })); +// Mock this one method from the User action so that the Pusher PINGPONG does not run during tests. It can cause the network to go offline which causes tests to fail because of timeouts. +// eslint-disable-next-line @typescript-eslint/no-unsafe-return +jest.mock('@src/libs/actions/User', () => ({ + ...jest.requireActual('@src/libs/actions/User'), + initializePusherPingPong: jest.fn(), +})); + jest.mock('../modules/background-task/src/NativeReactNativeBackgroundTask', () => ({ defineTask: jest.fn(), onBackgroundTaskExecution: jest.fn(), diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 25f1130c40e0..67978c5feadf 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -1558,4 +1558,7 @@ export { subscribeToActiveGuides, setIsDebugModeEnabled, resetValidateActionCodeSent, + + // This is only exported so that it can be mocked, and not have the Pusher PINGPONG run while tests are running (because it will make the network go offline and cause tests to timeout) + initializePusherPingPong, }; From a4103ec3d111f8c2aa112476f62deeabcd9bce81 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 25 Feb 2025 10:32:19 -0700 Subject: [PATCH 5/6] Skip pinpong when tests are running --- jest/setup.ts | 7 ------- src/libs/actions/User.ts | 8 +++++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/jest/setup.ts b/jest/setup.ts index 30b90eeb4251..2fc4d289471c 100644 --- a/jest/setup.ts +++ b/jest/setup.ts @@ -89,13 +89,6 @@ jest.mock('@src/libs/actions/Timing', () => ({ end: jest.fn(), })); -// Mock this one method from the User action so that the Pusher PINGPONG does not run during tests. It can cause the network to go offline which causes tests to fail because of timeouts. -// eslint-disable-next-line @typescript-eslint/no-unsafe-return -jest.mock('@src/libs/actions/User', () => ({ - ...jest.requireActual('@src/libs/actions/User'), - initializePusherPingPong: jest.fn(), -})); - jest.mock('../modules/background-task/src/NativeReactNativeBackgroundTask', () => ({ defineTask: jest.fn(), onBackgroundTaskExecution: jest.fn(), diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 67978c5feadf..77638eaf29f7 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -986,6 +986,11 @@ function checkforLatePongReplies() { let pingPusherIntervalID: ReturnType; let checkforLatePongRepliesIntervalID: ReturnType; function initializePusherPingPong() { + // Skip doing the ping pong during tests so that the network isn't knocked offline, forcing tests to timeout + if (process.env.NODE_ENV === 'test') { + return; + } + // Only run the ping pong from the leader client if (!ActiveClientManager.isClientTheLeader()) { Log.info("[Pusher PINGPONG] Not starting PING PONG because this instance isn't the leader client"); @@ -1558,7 +1563,4 @@ export { subscribeToActiveGuides, setIsDebugModeEnabled, resetValidateActionCodeSent, - - // This is only exported so that it can be mocked, and not have the Pusher PINGPONG run while tests are running (because it will make the network go offline and cause tests to timeout) - initializePusherPingPong, }; From 63c0b37570d64aeb967a1520f83461339a480b89 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 26 Feb 2025 13:39:43 -0700 Subject: [PATCH 6/6] Update src/libs/actions/User.ts Co-authored-by: Monil Bhavsar --- src/libs/actions/User.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 77638eaf29f7..d403cc1c3108 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -977,7 +977,7 @@ function checkforLatePongReplies() { // When going offline, reset the pingpong state so that when the network reconnects, the client will start fresh lastPingSentTimestamp = Date.now(); pongHasBeenMissed = true; - NetworkConnection.setOfflineStatus(true, 'PONG event was no recieved from the server in time so assuming that means the client is offline'); + NetworkConnection.setOfflineStatus(true, 'PONG event was not received from the server in time so assuming that means the client is offline'); } else { Log.info(`[Pusher PINGPONG] Last PONG event was ${timeSinceLastPongReceived} ms ago so not going offline`); }