Skip to content

Handle subscription init timeout gracefully #1915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.expediagroup.graphql.server.execution.subscription.GRAPHQL_WS_PROTOCO
import com.expediagroup.graphql.server.execution.subscription.GraphQLWebSocketServer
import com.expediagroup.graphql.server.types.GraphQLSubscriptionStatus
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactor.flux
import org.springframework.web.reactive.socket.CloseStatus
import org.springframework.web.reactive.socket.WebSocketHandler
Expand Down Expand Up @@ -53,7 +53,7 @@ class SubscriptionWebSocketHandler(
)

override suspend fun closeSession(session: WebSocketSession, reason: GraphQLSubscriptionStatus) {
session.close(CloseStatus(reason.code, reason.reason)).awaitFirst()
session.close(CloseStatus(reason.code, reason.reason)).awaitFirstOrNull()
}

override suspend fun sendSubscriptionMessage(session: WebSocketSession, message: String): WebSocketMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
package com.expediagroup.graphql.server.spring.subscriptions

import com.expediagroup.graphql.server.execution.subscription.GRAPHQL_WS_PROTOCOL
import com.expediagroup.graphql.server.types.GraphQLSubscriptionStatus
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.assertDoesNotThrow
import org.springframework.web.reactive.socket.WebSocketSession
import reactor.core.publisher.Mono

class SubscriptionWebSocketHandlerTest {

Expand All @@ -35,4 +41,16 @@ class SubscriptionWebSocketHandlerTest {
val handler = SubscriptionWebSocketHandler(mockk(), mockk(), mockk(), mockk(), 1_000, jacksonObjectMapper())
assertEquals(expected = listOf(GRAPHQL_WS_PROTOCOL), actual = handler.subProtocols)
}

@Test
fun `verify default subscription handler handles init timeout gracefully`() = runTest {
val handler = SubscriptionWebSocketHandler(mockk(), mockk(), mockk(), mockk(), 1_000, jacksonObjectMapper())
val session = mockk<WebSocketSession>()

every { session.close(any()) } returns Mono.empty()

assertDoesNotThrow {
handler.closeSession(session, GraphQLSubscriptionStatus.CONNECTION_INIT_TIMEOUT)
}
}
}