Skip to content

Commit 85351d3

Browse files
committed
Handle subscription init timeout gracefully
1 parent 34687a7 commit 85351d3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

servers/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/server/spring/subscriptions/SubscriptionWebSocketHandler.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.expediagroup.graphql.server.execution.subscription.GraphQLWebSocketSe
2222
import com.expediagroup.graphql.server.types.GraphQLSubscriptionStatus
2323
import com.fasterxml.jackson.databind.ObjectMapper
2424
import kotlinx.coroutines.reactive.awaitFirst
25+
import kotlinx.coroutines.reactive.awaitFirstOrNull
2526
import kotlinx.coroutines.reactor.flux
2627
import org.springframework.web.reactive.socket.CloseStatus
2728
import org.springframework.web.reactive.socket.WebSocketHandler
@@ -53,7 +54,7 @@ class SubscriptionWebSocketHandler(
5354
)
5455

5556
override suspend fun closeSession(session: WebSocketSession, reason: GraphQLSubscriptionStatus) {
56-
session.close(CloseStatus(reason.code, reason.reason)).awaitFirst()
57+
session.close(CloseStatus(reason.code, reason.reason)).awaitFirstOrNull()
5758
}
5859

5960
override suspend fun sendSubscriptionMessage(session: WebSocketSession, message: String): WebSocketMessage =

servers/graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/server/spring/subscriptions/SubscriptionWebSocketHandlerTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
package com.expediagroup.graphql.server.spring.subscriptions
1818

1919
import com.expediagroup.graphql.server.execution.subscription.GRAPHQL_WS_PROTOCOL
20+
import com.expediagroup.graphql.server.types.GraphQLSubscriptionStatus
2021
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
22+
import io.mockk.every
2123
import io.mockk.mockk
2224
import org.junit.jupiter.api.Test
2325
import kotlin.test.assertEquals
26+
import kotlinx.coroutines.test.runTest
27+
import org.junit.jupiter.api.assertDoesNotThrow
28+
import org.springframework.web.reactive.socket.WebSocketSession
29+
import reactor.core.publisher.Mono
2430

2531
class SubscriptionWebSocketHandlerTest {
2632

@@ -35,4 +41,16 @@ class SubscriptionWebSocketHandlerTest {
3541
val handler = SubscriptionWebSocketHandler(mockk(), mockk(), mockk(), mockk(), 1_000, jacksonObjectMapper())
3642
assertEquals(expected = listOf(GRAPHQL_WS_PROTOCOL), actual = handler.subProtocols)
3743
}
44+
45+
@Test
46+
fun `verify default subscription handler handles init timeout gracefully`() = runTest {
47+
val handler = SubscriptionWebSocketHandler(mockk(), mockk(), mockk(), mockk(), 1_000, jacksonObjectMapper())
48+
val session = mockk<WebSocketSession>()
49+
50+
every { session.close(any()) } returns Mono.empty()
51+
52+
assertDoesNotThrow {
53+
handler.closeSession(session, GraphQLSubscriptionStatus.CONNECTION_INIT_TIMEOUT)
54+
}
55+
}
3856
}

0 commit comments

Comments
 (0)