Skip to content

Commit 58b8633

Browse files
committed
Ensure WebSocket connection error notifies client
1 parent 0dbb4aa commit 58b8633

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

appsync/aws-sdk-appsync-events/src/androidTest/java/com/amazonaws/sdk/appsync/events/EventsWebSocketClientTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.test.platform.app.InstrumentationRegistry
1818
import app.cash.turbine.test
1919
import app.cash.turbine.turbineScope
2020
import com.amazonaws.sdk.appsync.core.authorizers.ApiKeyAuthorizer
21+
import com.amazonaws.sdk.appsync.events.data.ConnectException
2122
import com.amazonaws.sdk.appsync.events.data.InvalidInputException
2223
import com.amazonaws.sdk.appsync.events.data.PublishResult
2324
import com.amazonaws.sdk.appsync.events.data.UnauthorizedException
@@ -47,6 +48,7 @@ import org.junit.Test
4748
internal class EventsWebSocketClientTests {
4849
private val eventsConfig = getEventsConfig(InstrumentationRegistry.getInstrumentation().targetContext)
4950
private val apiKeyAuthorizer = ApiKeyAuthorizer(eventsConfig.apiKey)
51+
private val badApiKeyAuthorizer = ApiKeyAuthorizer("bad-api-key")
5052
private val webSocketLogCapture = EventsLibraryLogCapture()
5153
private val defaultChannel = "default/${UUID.randomUUID()}"
5254
private val customChannel = "custom/${UUID.randomUUID()}"
@@ -60,6 +62,12 @@ internal class EventsWebSocketClientTests {
6062
loggerProvider = { _ -> webSocketLogCapture }
6163
)
6264
)
65+
66+
private val badWebSocketClient = events.createWebSocketClient(
67+
badApiKeyAuthorizer,
68+
badApiKeyAuthorizer,
69+
badApiKeyAuthorizer
70+
)
6371
private val backgroundScope = CoroutineScope(Dispatchers.IO)
6472

6573
@After
@@ -109,6 +117,15 @@ internal class EventsWebSocketClientTests {
109117
)
110118
}
111119

120+
@Test
121+
fun testConnectionFailure() = runBlockingWithTimeout {
122+
turbineScope(timeout = 10.seconds) {
123+
badWebSocketClient.subscribe(defaultChannel).test(timeout = 10.seconds) {
124+
awaitError() shouldBe ConnectException(UnauthorizedException())
125+
}
126+
}
127+
}
128+
112129
@Test
113130
fun testPublishWithBadAuth(): Unit = runBlockingWithTimeout {
114131
// Publish the message

appsync/aws-sdk-appsync-events/src/main/java/com/amazonaws/sdk/appsync/events/data/WebSocketMessage.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ internal sealed class WebSocketMessage {
9797

9898
@Serializable
9999
@SerialName("connection_error")
100-
internal data class ConnectionError(val errors: List<EventsError>) : Received()
100+
internal data class ConnectionError(
101+
override val id: String? = null,
102+
override val errors: List<EventsError>
103+
) : Received(), ErrorContainer
101104

102105
@Serializable
103106
internal sealed class Subscription : Received() {
@@ -189,7 +192,8 @@ internal sealed class WebSocketMessage {
189192

190193
internal data class Closed(val reason: WebSocketDisconnectReason) : WebSocketMessage()
191194

192-
// All errors contain an id and errors list
195+
// All errors contain an errors list
196+
// Most events contain an id, which is either a channel id or a publish id
193197
internal interface ErrorContainer {
194198
val id: String?
195199
val errors: List<EventsError>

0 commit comments

Comments
 (0)