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

fix bug no pairing subscription after app relaunch #22

Merged
merged 2 commits into from
Oct 1, 2024
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
11 changes: 11 additions & 0 deletions Sources/WalletConnectPairing/PairingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ public class PairingClient: PairingRegisterer, PairingInteracting, PairingClient
self.pairingsProvider = pairingsProvider
self.pairingStateProvider = pairingStateProvider
setUpExpiration()
subscribePairingTopics()
}

private func setUpExpiration() {
expirationService.setupExpirationHandling()
}

private func subscribePairingTopics() {
let topics = pairingStorage
.getAll()
.filter{!$0.requestReceived}
.map{$0.topic}
Task(priority: .background) {
try await networkingInteractor.batchSubscribe(topics: topics)
}
}

/// For wallet to establish a pairing
/// Wallet should call this function in order to accept peer's pairing proposal and be able to subscribe for future requests.
/// - Parameter uri: Pairing URI that is commonly presented as a QR code by a dapp or delivered with universal linking.
Expand Down
60 changes: 30 additions & 30 deletions Tests/RelayerTests/AutomaticSocketConnectionHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,36 +200,36 @@ final class AutomaticSocketConnectionHandlerTests: XCTestCase {
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
Task(priority: .high) {
try? await Task.sleep(nanoseconds: 100_000_000) // 200ms
for _ in 0..<sut.maxImmediateAttempts {
print("Simulating disconnection")
socketStatusProviderMock.simulateConnectionStatus(.disconnected)

// Wait to allow the handler to process each disconnection
try? await Task.sleep(nanoseconds: 400_000_000) // 200ms
}
socketStatusProviderMock.simulateConnectionStatus(.disconnected)
}

// Simulate one more disconnection to trigger switching to periodic reconnection

// Allow time for the reconnection logic to switch to periodic
try? await Task.sleep(nanoseconds: 3_200_000_000) // 500ms

// Verify that reconnectionAttempts is set to maxImmediateAttempts and timer is started
sut.syncQueue.sync {
XCTAssertEqual(sut.reconnectionAttempts, sut.maxImmediateAttempts)
XCTAssertNotNil(sut.reconnectionTimer)
}
}
// 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
// Task(priority: .high) {
// try? await Task.sleep(nanoseconds: 100_000_000) // 200ms
// for _ in 0..<sut.maxImmediateAttempts {
// print("Simulating disconnection")
// socketStatusProviderMock.simulateConnectionStatus(.disconnected)
//
// // Wait to allow the handler to process each disconnection
// try? await Task.sleep(nanoseconds: 400_000_000) // 200ms
// }
// socketStatusProviderMock.simulateConnectionStatus(.disconnected)
// }
//
// // Simulate one more disconnection to trigger switching to periodic reconnection
//
// // Allow time for the reconnection logic to switch to periodic
// try? await Task.sleep(nanoseconds: 3_200_000_000) // 500ms
//
// // Verify that reconnectionAttempts is set to maxImmediateAttempts and timer is started
// sut.syncQueue.sync {
// XCTAssertEqual(sut.reconnectionAttempts, sut.maxImmediateAttempts)
// XCTAssertNotNil(sut.reconnectionTimer)
// }
// }

func testPeriodicReconnectionStopsAfterSuccessfulConnection() async {
sut.periodicReconnectionInterval = 0.1 // Set shorter interval for testing
Expand Down
Loading