Skip to content
Open
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 @@ -91,6 +91,8 @@ class OkHttpOcppWampClient(

override fun onConnectionFailure(t: Throwable) {
error = t
// notify before releasing connect(), whose cleanup drops the listener
connectionListener?.onConnectionFailure(t)
latch.countDown()
}

Expand Down Expand Up @@ -177,9 +179,10 @@ class OkHttpOcppWampClient(
}

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
val previousState = connectionState
connectionState = ConnectionState.DISCONNECTED
wampConnection = null
if (connectionState == ConnectionState.CONNECTING) {
if (previousState == ConnectionState.CONNECTING) {
if (t is ConnectException) {
logger.warn("[$ocppId] web socket failed to connect to $serverUri: $t")
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.izivia.ocpp.wamp

import com.izivia.ocpp.OcppVersion.OCPP_1_6
import com.izivia.ocpp.wamp.client.ConnectionListener
import com.izivia.ocpp.wamp.client.OcppWampClient
import org.http4k.core.Uri
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.api.expectThrows
import strikt.assertions.isEqualTo
import strikt.assertions.isTrue
import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

/**
* Nothing listens on the target port, so every attempt fails while the client is still connecting.
*/
class OkHttpOcppWampClientConnectionFailureTest {
private val target = Uri.of("ws://localhost:${getFreePort()}/ws")

@Test
fun `should notify a connection failure, not a connection lost, when connecting fails`() {
val listener = RecordingListener()
val client = newClient(autoReconnect = false)

expectThrows<IOException> { client.connect(listener) }

expectThat(listener.failures.get()).isEqualTo(1)
expectThat(listener.losses.get()).isEqualTo(0)
}

@Test
fun `should not report a connection lost while auto-reconnect attempts fail`() {
val listener = RecordingListener()
val client = newClient(autoReconnect = true)

client.connect(listener)
try {
expectThat(listener.firstFailure.await(5, TimeUnit.SECONDS)).isTrue()
} finally {
client.close()
}

expectThat(listener.losses.get()).isEqualTo(0)
}

private fun newClient(autoReconnect: Boolean) =
OcppWampClient.newClient(target, "chargepoint1", OCPP_1_6, timeoutInMs = 5_000, autoReconnect = autoReconnect)

private class RecordingListener : ConnectionListener {
val failures = AtomicInteger()
val losses = AtomicInteger()
val firstFailure = CountDownLatch(1)

override fun onConnectionFailure(t: Throwable) {
failures.incrementAndGet()
firstFailure.countDown()
}

override fun onConnectionLost(t: Throwable?) {
losses.incrementAndGet()
}
}
}
Loading