Skip to content

Commit

Permalink
fix testSwitchesToPeriodicReconnectionAfterMaxImmediateAttempts
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Sep 19, 2024
1 parent 79bbe71 commit d45d0bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ class WebSocketMock: WebSocketConnecting {
var onDisconnect: ((Error?) -> Void)?
var sendCallCount: Int = 0
var isConnected: Bool = false
var blockConnection = false

func connect() {
guard !blockConnection else {
return
}
isConnected = true
onConnect?()
}
Expand Down
11 changes: 5 additions & 6 deletions Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {
networkMonitor = NetworkMonitoringMock()
appStateObserver = AppStateObserverMock()

let defaults = RuntimeKeyValueStorage()
let logger = ConsoleLogger(prefix: "", loggingLevel: .debug)
let keychainStorageMock = DispatcherKeychainStorageMock()

backgroundTaskRegistrar = BackgroundTaskRegistrarMock()
subscriptionsTracker = SubscriptionsTrackerMock()
Expand Down Expand Up @@ -118,7 +116,7 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {

await sut.handleDisconnection()

await fulfillment(of: [expectation], timeout: 5.0)
await fulfillment(of: [expectation], timeout: 15.0)
}

func testNotReconnectOnDisconnectForegroundWhenNoSubscriptions() async {
Expand Down Expand Up @@ -201,20 +199,21 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {
// Simulate entering foreground
appStateObserver.onWillEnterForeground?()

await fulfillment(of: [expectation], timeout: 5.0)

XCTAssertTrue(webSocketSession.isConnected)
await fulfillment(of: [expectation], timeout: 15.0)
}

func testSwitchesToPeriodicReconnectionAfterMaxImmediateAttempts() async {
subscriptionsTracker.isSubscribedReturnValue = true // Ensure subscriptions exist to allow reconnection
sut.periodicReconnectionInterval = 3 // Set shorter interval for testing
webSocketSession.blockConnection = true
sut.connect() // Start connection process

// Simulate immediate reconnection attempts
for _ in 0..<sut.maxImmediateAttempts {
print("Simulating disconnection")
socketStatusProviderMock.simulateConnectionStatus(.disconnected)
webSocketSession.isConnected = false

// Wait to allow the handler to process each disconnection
try? await Task.sleep(nanoseconds: 500_000_000) // 200ms
}
Expand Down

0 comments on commit d45d0bd

Please sign in to comment.