|
| 1 | +import Ably |
| 2 | +@testable import AblyChat |
| 3 | +import Testing |
| 4 | + |
| 5 | +struct DefaultConnectionTests { |
| 6 | + |
| 7 | + // @spec CHA-CS2a |
| 8 | + // @spec CHA-CS2b |
| 9 | + // @spec CHA-CS3 |
| 10 | + @Test |
| 11 | + func chatClientMustExposeItsCurrentStatus() async throws { |
| 12 | + // Given: An instance of DefaultChatClient |
| 13 | + let client = DefaultChatClient(realtime: MockRealtime.create(), clientOptions: nil) |
| 14 | + |
| 15 | + // When: the connection status object is constructed |
| 16 | + let status = await client.connection.status |
| 17 | + let error = await client.connection.error |
| 18 | + |
| 19 | + // Then: connection status and error exposed and initial status and error of the connection must be whatever status the realtime client returns whilst the connection status object is constructed |
| 20 | + // Should be `initialized` but `DefaultConnection` fires `ConnectionStatusManager` actor events using `Task`, so those events are asynchronous to syncronous connection's constructor. Thus: |
| 21 | + // TODO: revisit together with `DefaultConnection` and https://github.com/ably-labs/ably-chat-swift/issues/49 |
| 22 | + #expect(status == .disconnected) |
| 23 | + #expect(error == nil) |
| 24 | + } |
| 25 | + |
| 26 | + // CHA-CS4e, CHA-CS4f - currently untestable due to subscription is removed once the object is removed from memory |
| 27 | + // @spec CHA-CS4a |
| 28 | + // @spec CHA-CS4b |
| 29 | + // @spec CHA-CS4c |
| 30 | + // @spec CHA-CS4d |
| 31 | + @Test |
| 32 | + func chatClientMustAllowItsConnectionStatusToBeObserved() async throws { |
| 33 | + // Given: An instance of DefaultChatClient and a connection error |
| 34 | + let client = DefaultChatClient(realtime: MockRealtime.create(), clientOptions: nil) |
| 35 | + let connectionError = ARTErrorInfo.createUnknownError() |
| 36 | + |
| 37 | + // When |
| 38 | + // (CHA-CS4d) Clients must be able to register a listener for connection status events and receive such events. |
| 39 | + let subscription = client.connection.onStatusChange() |
| 40 | + |
| 41 | + subscription.emit(.init(current: .disconnected, |
| 42 | + previous: .connecting, |
| 43 | + error: connectionError, |
| 44 | + retryIn: 1)) // arbitrary values |
| 45 | + |
| 46 | + let statusChange = try #require(await subscription.first { _ in true }) |
| 47 | + |
| 48 | + // Then |
| 49 | + // (CHA-CS4a) Connection status update events must contain the newly entered connection status. |
| 50 | + // (CHA-CS4b) Connection status update events must contain the previous connection status. |
| 51 | + // (CHA-CS4c) Connection status update events must contain the connection error (if any) that pertains to the newly entered connection status. |
| 52 | + #expect(statusChange.current == .disconnected) |
| 53 | + #expect(statusChange.previous == .connecting) |
| 54 | + #expect(statusChange.error == connectionError) |
| 55 | + } |
| 56 | + |
| 57 | + // @spec CHA-CS5a1 |
| 58 | + // @spec CHA-CS5a4 |
| 59 | + @Test |
| 60 | + func whenConnectionGoesFromConnectedToDisconnectedTransientDisconnectTimerStarts() async throws { |
| 61 | + // Given: |
| 62 | + // An instance of DefaultChatClient, connected realtime connection and default chat connection |
| 63 | + let realtimeConnection = MockConnection(state: .connected) |
| 64 | + let client = DefaultChatClient(realtime: MockRealtime.create(connection: realtimeConnection), clientOptions: nil) |
| 65 | + let defaultConnection = client.connection as! DefaultConnection |
| 66 | + |
| 67 | + // Transient timer subscription |
| 68 | + let transientTimerSubscription = await defaultConnection.testsOnly_subscribeToTransientDisconnectTimerEvents() |
| 69 | + |
| 70 | + // Status subscription |
| 71 | + let statusSubscription = defaultConnection.onStatusChange() |
| 72 | + |
| 73 | + // When: |
| 74 | + |
| 75 | + // Realtime connection status transitions from CONNECTED to DISCONNECTED |
| 76 | + let connectionError = ARTErrorInfo.create(withCode: 0, message: "Connection error") |
| 77 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected, error: connectionError) |
| 78 | + |
| 79 | + // Then: |
| 80 | + |
| 81 | + // A 5 second transient disconnect timer shall be started |
| 82 | + let timerStartedAt = Date().timeIntervalSince1970 |
| 83 | + let transientTimerEvent = try #require(await transientTimerSubscription.first { _ in true }) |
| 84 | + #expect(transientTimerEvent.active) |
| 85 | + |
| 86 | + // (emitting artificial status change event for subscription awaiting below to return) |
| 87 | + let fakeError = ARTErrorInfo.create(withCode: 0, message: "Fake error") |
| 88 | + statusSubscription.emit(.init(current: .initialized, |
| 89 | + previous: .initialized, |
| 90 | + error: fakeError, |
| 91 | + retryIn: 1)) // arbitrary values |
| 92 | + |
| 93 | + // Then: |
| 94 | + let statusChange1 = try #require(await statusSubscription.first { _ in true }) |
| 95 | + let statusChange2 = try #require(await statusSubscription.first { _ in true }) |
| 96 | + |
| 97 | + // Transient disconnect timer interval is 5 seconds |
| 98 | + #expect(Date().timeIntervalSince1970 - timerStartedAt >= 5) |
| 99 | + |
| 100 | + // Chat client connection status must not change - first emitted status was artificial and was not generated by `transitionToState:` |
| 101 | + #expect(statusChange1.error == fakeError) |
| 102 | + |
| 103 | + // And the second status chage was generated by `transitionToState:` when transient timer has expired (CHA-CS5a4) |
| 104 | + #expect(statusChange2.error == connectionError) |
| 105 | + } |
| 106 | + |
| 107 | + // @spec CHA-CS5a2 |
| 108 | + @Test |
| 109 | + func whenConnectionGoesFromDisconnectedToConnectingNoStatusChangeIsEmitted() async throws { |
| 110 | + // Given: |
| 111 | + // An instance of DefaultChatClient, connected realtime connection and default chat connection |
| 112 | + let realtimeConnection = MockConnection(state: .connected) |
| 113 | + let client = DefaultChatClient(realtime: MockRealtime.create(connection: realtimeConnection), clientOptions: nil) |
| 114 | + let defaultConnection = client.connection as! DefaultConnection |
| 115 | + |
| 116 | + // Transient timer subscription |
| 117 | + let transientTimerSubscription = await defaultConnection.testsOnly_subscribeToTransientDisconnectTimerEvents() |
| 118 | + |
| 119 | + // Status subscription |
| 120 | + let statusSubscription = defaultConnection.onStatusChange() |
| 121 | + |
| 122 | + // When: |
| 123 | + |
| 124 | + // Transient disconnect timer is active |
| 125 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected) // starting timer by going to DISCONNECTED |
| 126 | + let transientTimerEvent = try #require(await transientTimerSubscription.first { _ in true }) |
| 127 | + #expect(transientTimerEvent.active) |
| 128 | + |
| 129 | + // And the realtime connection status changes to CONNECTING |
| 130 | + realtimeConnection.transitionToState(.connecting, event: .connecting) |
| 131 | + |
| 132 | + // Or to DISCONNECTED |
| 133 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected) |
| 134 | + |
| 135 | + // (emitting artificial status change event for subscription awaiting below to return) |
| 136 | + let fakeError = ARTErrorInfo.create(withCode: 0, message: "Fake error") |
| 137 | + statusSubscription.emit(.init(current: .initialized, |
| 138 | + previous: .initialized, |
| 139 | + error: fakeError, |
| 140 | + retryIn: 1)) // arbitrary values |
| 141 | + |
| 142 | + // Then: |
| 143 | + let statusChange1 = try #require(await statusSubscription.first { _ in true }) |
| 144 | + let statusChange2 = try #require(await statusSubscription.first { _ in true }) |
| 145 | + |
| 146 | + // Chat client connection status must not change - first emitted status was artificial and was not generated by the calls to `transitionToState:` |
| 147 | + #expect(statusChange1.error == fakeError) |
| 148 | + |
| 149 | + // And the second status change was generated by `transitionToState:` when transient timer has expired |
| 150 | + #expect(statusChange2.error == nil) |
| 151 | + } |
| 152 | + |
| 153 | + // @spec CHA-CS5a3 |
| 154 | + @Test |
| 155 | + func whenConnectionGoesToConnectedStatusChangeShouldBeEmitted() async throws { |
| 156 | + // Given: |
| 157 | + // An instance of DefaultChatClient, connected realtime connection and default chat connection |
| 158 | + let realtimeConnection = MockConnection(state: .connected) |
| 159 | + let client = DefaultChatClient(realtime: MockRealtime.create(connection: realtimeConnection), clientOptions: nil) |
| 160 | + let defaultConnection = client.connection as! DefaultConnection |
| 161 | + |
| 162 | + // Transient timer subscription |
| 163 | + let transientTimerSubscription = await defaultConnection.testsOnly_subscribeToTransientDisconnectTimerEvents() |
| 164 | + |
| 165 | + // Status subscription |
| 166 | + let statusSubscription = defaultConnection.onStatusChange() |
| 167 | + |
| 168 | + // When: |
| 169 | + |
| 170 | + // Transient disconnect timer is active |
| 171 | + let timerStartedAt = Date().timeIntervalSince1970 |
| 172 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected) // starting timer by going to DISCONNECTED |
| 173 | + let transientTimerEvent = try #require(await transientTimerSubscription.first { _ in true }) |
| 174 | + #expect(transientTimerEvent.active) |
| 175 | + |
| 176 | + // And the realtime connection status changes to CONNECTED |
| 177 | + realtimeConnection.transitionToState(.connected, event: .connected) |
| 178 | + |
| 179 | + let statusChange = try #require(await statusSubscription.first { _ in true }) |
| 180 | + |
| 181 | + // Then: |
| 182 | + |
| 183 | + // The library shall cancel the transient disconnect timer (less than 5 seconds -> was cancelled) |
| 184 | + #expect(Date().timeIntervalSince1970 - timerStartedAt < 1) |
| 185 | + |
| 186 | + // The superseding status change shall be emitted |
| 187 | + #expect(statusChange.current == .connected) |
| 188 | + #expect(statusChange.error == nil) |
| 189 | + } |
| 190 | + |
| 191 | + // @spec CHA-CS5a3 |
| 192 | + @Test |
| 193 | + func whenConnectionGoesToSuspendedStatusChangeShouldBeEmitted() async throws { |
| 194 | + // Given: |
| 195 | + // An instance of DefaultChatClient, connected realtime connection and default chat connection |
| 196 | + let realtimeConnection = MockConnection(state: .connected) |
| 197 | + let client = DefaultChatClient(realtime: MockRealtime.create(connection: realtimeConnection), clientOptions: nil) |
| 198 | + let defaultConnection = client.connection as! DefaultConnection |
| 199 | + |
| 200 | + // Transient timer subscription |
| 201 | + let transientTimerSubscription = await defaultConnection.testsOnly_subscribeToTransientDisconnectTimerEvents() |
| 202 | + |
| 203 | + // Status subscription |
| 204 | + let statusSubscription = defaultConnection.onStatusChange() |
| 205 | + |
| 206 | + // When: |
| 207 | + |
| 208 | + // Transient disconnect timer is active |
| 209 | + let timerStartedAt = Date().timeIntervalSince1970 |
| 210 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected) // starting timer by going to DISCONNECTED |
| 211 | + let transientTimerEvent = try #require(await transientTimerSubscription.first { _ in true }) |
| 212 | + #expect(transientTimerEvent.active) |
| 213 | + |
| 214 | + // And the realtime connection status changes to SUSPENDED |
| 215 | + realtimeConnection.transitionToState(.suspended, event: .suspended) |
| 216 | + |
| 217 | + let statusChange = try #require(await statusSubscription.first { _ in true }) |
| 218 | + |
| 219 | + // Then: |
| 220 | + |
| 221 | + // The library shall cancel the transient disconnect timer (less than 5 seconds -> was cancelled) |
| 222 | + #expect(Date().timeIntervalSince1970 - timerStartedAt < 1) |
| 223 | + |
| 224 | + // The superseding status change shall be emitted |
| 225 | + #expect(statusChange.current == .suspended) |
| 226 | + } |
| 227 | + |
| 228 | + // @spec CHA-CS5a3 |
| 229 | + @Test |
| 230 | + func whenConnectionGoesToFailedStatusChangeShouldBeEmitted() async throws { |
| 231 | + // Given: |
| 232 | + // An instance of DefaultChatClient, connected realtime connection and default chat connection |
| 233 | + let realtimeConnection = MockConnection(state: .connected) |
| 234 | + let client = DefaultChatClient(realtime: MockRealtime.create(connection: realtimeConnection), clientOptions: nil) |
| 235 | + let defaultConnection = client.connection as! DefaultConnection |
| 236 | + |
| 237 | + // Transient timer subscription |
| 238 | + let transientTimerSubscription = await defaultConnection.testsOnly_subscribeToTransientDisconnectTimerEvents() |
| 239 | + |
| 240 | + // Status subscription |
| 241 | + let statusSubscription = defaultConnection.onStatusChange() |
| 242 | + |
| 243 | + // When: |
| 244 | + |
| 245 | + // Transient disconnect timer is active |
| 246 | + let timerStartedAt = Date().timeIntervalSince1970 |
| 247 | + realtimeConnection.transitionToState(.disconnected, event: .disconnected) // starting timer by going to DISCONNECTED |
| 248 | + let transientTimerEvent = try #require(await transientTimerSubscription.first { _ in true }) |
| 249 | + #expect(transientTimerEvent.active) |
| 250 | + |
| 251 | + // And the realtime connection status changes to FAILED |
| 252 | + realtimeConnection.transitionToState(.failed, event: .failed, error: ARTErrorInfo.create(withCode: 0, message: "Connection error")) |
| 253 | + |
| 254 | + let statusChange = try #require(await statusSubscription.first { _ in true }) |
| 255 | + |
| 256 | + // Then: |
| 257 | + |
| 258 | + // The library shall cancel the transient disconnect timer (less than 5 seconds -> was cancelled) |
| 259 | + #expect(Date().timeIntervalSince1970 - timerStartedAt < 1) |
| 260 | + |
| 261 | + // The superseding status change shall be emitted |
| 262 | + #expect(statusChange.current == .failed) |
| 263 | + #expect(statusChange.error != nil) |
| 264 | + } |
| 265 | +} |
0 commit comments