From c895341425891386f3b87ca26fc5f1e23ada22a6 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Fri, 3 Nov 2023 15:24:49 -0300 Subject: [PATCH] Treat all transport errors as recoverable This is a cherry-pick of 46ee0b0f, to resolve #1823 on the fix/socketrocket-fix branch which one of our customers is using. TODO bring in test changes --- Source/ARTRealtime.m | 22 +++++++--------------- Spec/Test Utilities/TestUtilities.swift | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Source/ARTRealtime.m b/Source/ARTRealtime.m index d4c718a7b..623191853 100644 --- a/Source/ARTRealtime.m +++ b/Source/ARTRealtime.m @@ -1519,16 +1519,8 @@ - (void)realtimeTransportFailed:(id)transport withError:(A } } - switch (transportError.type) { - case ARTRealtimeTransportErrorTypeBadResponse: - case ARTRealtimeTransportErrorTypeOther: - [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createFromNSError:transportError.error]]; - break; - default: { - ARTErrorInfo *error = [ARTErrorInfo createFromNSError:transportError.error]; - [self transitionToDisconnectedOrSuspendedWithError:error]; - } - } + ARTErrorInfo *error = [ARTErrorInfo createFromNSError:transportError.error]; + [self transitionToDisconnectedOrSuspendedWithError:error]; } - (void)realtimeTransportNeverConnected:(id)transport { @@ -1537,7 +1529,7 @@ - (void)realtimeTransportNeverConnected:(id)transport { return; } - [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:@"Transport never connected"]]; + [self transitionToDisconnectedOrSuspendedWithError:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:@"Transport never connected"]]; } - (void)realtimeTransportRefused:(id)transport withError:(ARTRealtimeTransportError *)error { @@ -1547,13 +1539,13 @@ - (void)realtimeTransportRefused:(id)transport withError:( } if (error && error.type == ARTRealtimeTransportErrorTypeRefused) { - [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:[NSString stringWithFormat:@"Connection refused using %@", error.url]]]; + [self transitionToDisconnectedOrSuspendedWithError:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:[NSString stringWithFormat:@"Connection refused using %@", error.url]]]; } else if (error) { - [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createFromNSError:error.error]]; + [self transitionToDisconnectedOrSuspendedWithError:[ARTErrorInfo createFromNSError:error.error]]; } else { - [self transition:ARTRealtimeFailed]; + [self transitionToDisconnectedOrSuspendedWithError:nil]; } } @@ -1563,7 +1555,7 @@ - (void)realtimeTransportTooBig:(id)transport { return; } - [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:@"Transport too big"]]; + [self transitionToDisconnectedOrSuspendedWithError:[ARTErrorInfo createWithCode:ARTClientCodeErrorTransport message:@"Transport too big"]]; } - (void)realtimeTransportSetMsgSerial:(id)transport msgSerial:(int64_t)msgSerial { diff --git a/Spec/Test Utilities/TestUtilities.swift b/Spec/Test Utilities/TestUtilities.swift index 24f3c1ad1..c4a5fe7a5 100644 --- a/Spec/Test Utilities/TestUtilities.swift +++ b/Spec/Test Utilities/TestUtilities.swift @@ -744,6 +744,7 @@ enum FakeNetworkResponse { case requestTimeout(timeout: TimeInterval) case hostInternalError(code: Int) case host400BadRequest + case arbitraryError var error: NSError { switch self { @@ -757,6 +758,8 @@ enum FakeNetworkResponse { return NSError(domain: AblyTestsErrorDomain, code: code, userInfo: [NSLocalizedDescriptionKey: "internal error", NSLocalizedFailureReasonErrorKey: AblyTestsErrorDomain + ".FakeNetworkResponse"]) case .host400BadRequest: return NSError(domain: AblyTestsErrorDomain, code: 400, userInfo: [NSLocalizedDescriptionKey: "bad request", NSLocalizedFailureReasonErrorKey: AblyTestsErrorDomain + ".FakeNetworkResponse"]) + case .arbitraryError: + return NSError(domain: AblyTestsErrorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "error from FakeNetworkResponse.arbitraryError"]) } } @@ -772,6 +775,8 @@ enum FakeNetworkResponse { return ARTRealtimeTransportError(error: error, badResponseCode: code, url: url) case .host400BadRequest: return ARTRealtimeTransportError(error: error, badResponseCode: 400, url: url) + case .arbitraryError: + return ARTRealtimeTransportError(error: error, type: .other, url: url) } } } @@ -857,6 +862,8 @@ class MockHTTP: ARTHttp { requestCallback?(HTTPURLResponse(url: URL(string: "http://cocoa.test.suite")!, statusCode: code, httpVersion: nil, headerFields: nil), nil, nil) case .host400BadRequest: requestCallback?(HTTPURLResponse(url: URL(string: "http://cocoa.test.suite")!, statusCode: 400, httpVersion: nil, headerFields: nil), nil, nil) + case .arbitraryError: + requestCallback?(nil, nil, NSError(domain: AblyTestsErrorDomain, code: 1, userInfo: [NSLocalizedDescriptionKey: "error from FakeNetworkResponse.arbitraryError"])) } } @@ -1232,7 +1239,8 @@ class TestProxyTransport: ARTWebSocketTransport { case .noInternet, .hostUnreachable, .hostInternalError, - .host400BadRequest: + .host400BadRequest, + .arbitraryError: performFakeConnectionError(0.1, error: networkResponse.transportError(for: url)) case .requestTimeout(let timeout): performFakeConnectionError(0.1 + timeout, error: networkResponse.transportError(for: url)) @@ -1613,9 +1621,11 @@ extension ARTWebSocketTransport { } func simulateIncomingError() { - let error = NSError(domain: ARTAblyErrorDomain, code: 0, userInfo: [NSLocalizedDescriptionKey:"Fail test"]) - let webSocketDelegate = self as ARTWebSocketDelegate - webSocketDelegate.webSocket(self.websocket!, didFailWithError: error) + // Simulate receiving an ERROR ProtocolMessage, which should put a client into the FAILED state (per RTN15i) + let protocolMessage = ARTProtocolMessage() + protocolMessage.action = .error + protocolMessage.error = ARTErrorInfo.create(withCode: 50000 /* arbitrarily chosen */, message: "Fail test") + receive(protocolMessage) } }