11package cz.smarteon.loxone
22
33import io.mockk.mockk
4+ import io.mockk.verify
5+ import org.java_websocket.framing.CloseFrame
46import org.junit.jupiter.api.BeforeEach
57import org.junit.jupiter.api.Test
6- import org.junit.jupiter.params.ParameterizedTest
7- import org.junit.jupiter.params.provider.ValueSource
88import strikt.api.expectCatching
99import strikt.assertions.isSuccess
1010import java.net.URI
@@ -27,13 +27,27 @@ class LoxoneWebsocketClientTest {
2727 }.isSuccess()
2828 }
2929
30- @ParameterizedTest
31- @ValueSource(booleans = [true , false ])
32- fun `should handle onClose` (remote : Boolean ) {
33- client.onClose(1000 , " some reason" , remote)
30+ @Test
31+ fun `should auto-restart on remote close` () {
32+ client.onClose(CloseFrame .NORMAL , " normally closed" , true )
33+
34+ verify { webSocket.connectionClosed(CloseFrame .NORMAL , true ) }
35+ verify { webSocket.autoRestart() }
36+ }
37+
38+ @Test
39+ fun `should auto-restart on abnormal local close (lost connection or pong timeout)` () {
40+ client.onClose(CloseFrame .ABNORMAL_CLOSE , " no pong in time" , false )
41+
42+ verify { webSocket.connectionClosed(CloseFrame .ABNORMAL_CLOSE , false ) }
43+ verify { webSocket.autoRestart() }
44+ }
45+
46+ @Test
47+ fun `should not auto-restart on deliberate local close` () {
48+ client.onClose(CloseFrame .NORMAL , " closed by client" , false )
3449
35- expectCatching { webSocket.wsClosed() }.isSuccess()
36- expectCatching { webSocket.connectionClosed(1000 , remote) }.isSuccess()
37- if (remote) expectCatching { webSocket.autoRestart() }.isSuccess()
50+ verify { webSocket.connectionClosed(CloseFrame .NORMAL , false ) }
51+ verify(exactly = 0 ) { webSocket.autoRestart() }
3852 }
3953}
0 commit comments