Skip to content

Commit

Permalink
Throw specific exception when disconnected while waiting for frames
Browse files Browse the repository at this point in the history
Resolves:
#379
  • Loading branch information
joffrey-bion committed Sep 13, 2023
1 parent b31cbd2 commit 86a5f36
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal class BaseStompSession(
.dematerializeErrorsAndCompletion()
.filterIsInstance<StompFrame.Receipt>()
.firstOrNull { it.headers.receiptId == receiptId }
?: error("Frames flow closed unexpectedly while waiting for RECEIPT frame with id='$receiptId'")
?: throw SessionDisconnectedException("The STOMP frames flow completed unexpectedly while waiting for RECEIPT frame with id='$receiptId'")
} ?: throw LostReceiptException(receiptId, frame.receiptTimeout, frame)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ class WebSocketClosedUnexpectedly(
val code: Int,
val reason: String?,
) : Exception("the WebSocket was closed while subscriptions were still active. Code: $code Reason: $reason")

/**
* An exception thrown when the STOMP frames flow completed while some consumer was expecting more frames.
*/
class SessionDisconnectedException(message: String) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes

private val TEST_RECEIPT_TIMEOUT: Duration = 500.milliseconds

Expand Down Expand Up @@ -121,6 +122,26 @@ class StompSessionReceiptTests {
assertEquals("because why not", exception.reason)
}

@Test
fun send_autoReceipt_failsOnSessionDisconnected() = runTest {
val (wsSession, stompSession) = connectWithMocks {
autoReceipt = true
receiptTimeout = 5.minutes
}
launch {
wsSession.awaitSendFrameAndSimulateCompletion()
// we simulate a disconnection between the SEND and the RECEIPT
launch {
wsSession.awaitDisconnectFrameAndSimulateCompletion()
wsSession.expectClose()
}
stompSession.disconnect()
}
assertFailsWith(SessionDisconnectedException::class) {
stompSession.sendEmptyMsg("/destination")
}
}

@Test
fun send_manualReceipt_waitsForCorrectReceipt() = runTest {
val (wsSession, stompSession) = connectWithMocks()
Expand Down

0 comments on commit 86a5f36

Please sign in to comment.