Skip to content

Commit

Permalink
add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Aug 20, 2024
1 parent d491f53 commit fe17f13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol {
subject.filter {
switch $0 {
case .success(let response): return response.id == id || response.type == .connectionError
case .failure(let error): return true
case .failure: return true
}
}
.map { result -> AppSyncSubscriptionEvent? in
Expand Down Expand Up @@ -430,7 +430,7 @@ extension AppSyncRealTimeClient {
}
}

private func monitorHeartBeats(_ connectionAck: JSONValue?) {
internal func monitorHeartBeats(_ connectionAck: JSONValue?) {
let timeoutMs = connectionAck?.connectionTimeoutMs?.intValue ?? 0
log.debug("[AppSyncRealTimeClient] Starting heart beat monitor with interval \(timeoutMs) ms")
let cancellable = heartBeats.eraseToAnyPublisher()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,32 @@ class AppSyncRealTimeClientTests: XCTestCase {
await fulfillment(of: [startTriggered, errorReceived], timeout: 2)

}

func testReconnect_whenHeartBeatSignalIsNotReceived() async throws {
var cancellables = Set<AnyCancellable>()
let timeout = 1.0
let mockWebSocketClient = MockWebSocketClient()
let mockAppSyncRequestInterceptor = MockAppSyncRequestInterceptor()
let appSyncClient = AppSyncRealTimeClient(
endpoint: URL(string: "https://example.com")!,
requestInterceptor: mockAppSyncRequestInterceptor,
webSocketClient: mockWebSocketClient
)

// start monitoring
await appSyncClient.monitorHeartBeats(.object([
"connectionTimeoutMs": 100
]))

let reconnect = expectation(description: "webSocket triggers event to connection")
await mockWebSocketClient.actionSubject.sink { action in
switch action {
case .connect:
reconnect.fulfill()
default: break
}
}.store(in: &cancellables)
await fulfillment(of: [reconnect], timeout: 2)
}

}

0 comments on commit fe17f13

Please sign in to comment.