Skip to content

Commit 107dc48

Browse files
committed
chore: Fix sporadically failing Tests
... due to simultaneous access to shared resources. Due to a Gradle or Library update it seems that the debug and release tests are executed using multiple threads, which might lead to one thread starting the corresponding Databroker Container, while the other closes it. This might lead to timeouts and therefore failing tests. Fixed it, by creating a container for each test class. Previously only one container was created for each testset (testRelease / testDebug)
1 parent 1e7df6f commit 107dc48

File tree

11 files changed

+31
-22
lines changed

11 files changed

+31
-22
lines changed

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/authentication/DataBrokerConnectorAuthenticationTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class DataBrokerConnectorAuthenticationTest : BehaviorSpec({
7272
val jwtFile = JwtType.READ_WRITE_ALL
7373

7474
val dataBrokerConnector = dataBrokerConnectorProvider.createSecure(
75+
port = databrokerContainer!!.port,
7576
jwtFileStream = jwtFile.asInputStream(),
7677
)
7778

@@ -104,6 +105,7 @@ class DataBrokerConnectorAuthenticationTest : BehaviorSpec({
104105
and("a secure DataBrokerConnector with a READ_ALL JWT") {
105106
val jwtFile = JwtType.READ_ALL
106107
val dataBrokerConnector = dataBrokerConnectorProvider.createSecure(
108+
port = databrokerContainer!!.port,
107109
jwtFileStream = jwtFile.asInputStream(),
108110
)
109111

@@ -135,6 +137,7 @@ class DataBrokerConnectorAuthenticationTest : BehaviorSpec({
135137
and("a secure DataBrokerConnector with a READ_WRITE_ALL_VALUES_ONLY JWT") {
136138
val jwtFile = JwtType.READ_WRITE_ALL_VALUES_ONLY
137139
val dataBrokerConnector = dataBrokerConnectorProvider.createSecure(
140+
port = databrokerContainer!!.port,
138141
jwtFileStream = jwtFile.asInputStream(),
139142
)
140143

@@ -189,6 +192,7 @@ class DataBrokerConnectorAuthenticationTest : BehaviorSpec({
189192

190193
and("a secure DataBrokerConnector with no JWT") {
191194
val dataBrokerConnector = dataBrokerConnectorProvider.createSecure(
195+
port = databrokerContainer!!.port,
192196
jwtFileStream = null,
193197
)
194198

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/DataBrokerConfig.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 - 2024 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2023 - 2025 Contributors to the Eclipse Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,5 +23,5 @@ import java.util.concurrent.TimeUnit
2323

2424
const val DATABROKER_HOST = "127.0.0.1"
2525

26-
const val DATABROKER_TIMEOUT_SECONDS = 5L
26+
const val DATABROKER_TIMEOUT_SECONDS = 10L
2727
val DATABROKER_TIMEOUT_UNIT = TimeUnit.SECONDS

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/DataBrokerConnectionTest.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class DataBrokerConnectionTest : BehaviorSpec({
6363

6464
given("A successfully established connection to the DataBroker") {
6565
val dataBrokerConnectorProvider = DataBrokerConnectorProvider()
66-
val connector = dataBrokerConnectorProvider.createInsecure()
66+
val connector = dataBrokerConnectorProvider.createInsecure(
67+
port = databrokerContainer!!.port,
68+
)
6769
val dataBrokerConnection = connector.connect()
6870

6971
and("A request with a valid VSS Path") {

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/DataBrokerConnectorProvider.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DataBrokerConnectorProvider {
3636
lateinit var managedChannel: ManagedChannel
3737
fun createInsecure(
3838
host: String = DATABROKER_HOST,
39-
port: Int = DATABROKER_PORT,
39+
port: Int,
4040
jwtFileStream: InputStream? = null,
4141
): DataBrokerConnector {
4242
managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build()
@@ -56,7 +56,7 @@ class DataBrokerConnectorProvider {
5656

5757
fun createSecure(
5858
host: String = DATABROKER_HOST,
59-
port: Int = DATABROKER_PORT,
59+
port: Int,
6060
overrideAuthority: String = "",
6161
rootCertFileStream: InputStream = TestResourceFile("tls/CA.pem").inputStream(),
6262
jwtFileStream: InputStream? = JwtType.READ_WRITE_ALL.asInputStream(),

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/DataBrokerConnectorSecureTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class DataBrokerConnectorSecureTest : BehaviorSpec({
5252
val tlsCertificate = TestResourceFile("tls/CA.pem")
5353

5454
val dataBrokerConnector = dataBrokerConnectorProvider.createSecure(
55+
port = databrokerContainer!!.port,
5556
rootCertFileStream = tlsCertificate.inputStream(),
5657
)
5758

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/DataBrokerConnectorTest.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class DataBrokerConnectorTest : BehaviorSpec({
4747
val dataBrokerConnectorProvider = DataBrokerConnectorProvider()
4848

4949
and("an insecure DataBrokerConnector with valid Host and Port") {
50-
val dataBrokerConnector = dataBrokerConnectorProvider.createInsecure()
50+
val dataBrokerConnector = dataBrokerConnectorProvider.createInsecure(
51+
port = databrokerContainer!!.port,
52+
)
5153

5254
`when`("Trying to establish an insecure connection") {
5355
val connection = dataBrokerConnector.connect()

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/docker/DataBrokerDockerContainer.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.github.dockerjava.httpclient5.ApacheDockerHttpClient
3535
import org.eclipse.kuksa.connectivity.databroker.DATABROKER_TIMEOUT_SECONDS
3636
import java.time.Duration
3737
import java.util.concurrent.TimeUnit
38+
import java.util.concurrent.atomic.AtomicInteger
3839

3940
private const val KEY_PROPERTY_DATABROKER_TAG = "databroker.tag"
4041
private const val KEY_ENV_DATABROKER_TAG = "DATABROKER_TAG"
@@ -62,8 +63,9 @@ private const val DEFAULT_DATABROKER_TAG = "main"
6263
*/
6364
abstract class DataBrokerDockerContainer(
6465
protected val containerName: String,
65-
protected val port: Int,
6666
) {
67+
val port: Int = atomicPortNumber.getAndIncrement()
68+
6769
protected open val hostConfig: HostConfig = HostConfig.newHostConfig()
6870
.withNetworkMode("host")
6971
.withAutoRemove(true)
@@ -134,9 +136,13 @@ abstract class DataBrokerDockerContainer(
134136

135137
dockerClient.waitContainerCmd(containerId)
136138
.exec(WaitContainerResultCallback())
137-
.awaitCompletion(5, TimeUnit.SECONDS)
139+
.awaitCompletion(10, TimeUnit.SECONDS)
138140
} catch (_: NotModifiedException) {
139141
// thrown when a container is already started
140142
}
141143
}
144+
145+
companion object {
146+
private val atomicPortNumber = AtomicInteger(55560)
147+
}
142148
}

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/docker/InsecureDataBrokerDockerContainer.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 - 2024 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2023 - 2025 Contributors to the Eclipse Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,18 +21,16 @@ package org.eclipse.kuksa.connectivity.databroker.docker
2121

2222
import com.github.dockerjava.api.command.CreateContainerResponse
2323
import org.eclipse.kuksa.connectivity.databroker.DATABROKER_CONTAINER_NAME
24-
import org.eclipse.kuksa.connectivity.databroker.DATABROKER_PORT
2524

2625
// no tls, no authentication
2726
class InsecureDataBrokerDockerContainer(
2827
containerName: String = DATABROKER_CONTAINER_NAME,
29-
port: Int = DATABROKER_PORT,
30-
) : DataBrokerDockerContainer(containerName, port) {
28+
) : DataBrokerDockerContainer(containerName) {
3129

3230
@Suppress("ArgumentListWrapping", "ktlint:standard:argument-list-wrapping") // better key-value pair readability
3331
override fun createContainer(tag: String): CreateContainerResponse {
3432
return dockerClient.createContainerCmd("$repository:$tag")
35-
.withName(containerName)
33+
.withName("${containerName}_${System.nanoTime()}")
3634
.withHostConfig(hostConfig)
3735
.withCmd(
3836
"--port", "$port",

kuksa-sdk/src/test/kotlin/org/eclipse/kuksa/connectivity/databroker/docker/SecureDataBrokerDockerContainer.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ import com.github.dockerjava.api.model.Bind
2525
import com.github.dockerjava.api.model.HostConfig
2626
import com.github.dockerjava.api.model.Volume
2727
import org.eclipse.kuksa.connectivity.databroker.DATABROKER_CONTAINER_NAME
28-
import org.eclipse.kuksa.connectivity.databroker.DATABROKER_PORT
2928
import org.eclipse.kuksa.test.TestResourceFile
3029

3130
// tls enabled, authentication enabled
3231
class SecureDataBrokerDockerContainer(
3332
containerName: String = DATABROKER_CONTAINER_NAME,
34-
port: Int = DATABROKER_PORT,
35-
) : DataBrokerDockerContainer(containerName, port) {
33+
) : DataBrokerDockerContainer(containerName) {
3634

3735
private val authenticationFolder = TestResourceFile("authentication").toString()
3836
private val authenticationMount = "/resources/authentication"
@@ -49,7 +47,7 @@ class SecureDataBrokerDockerContainer(
4947
@Suppress("ArgumentListWrapping", "ktlint:standard:argument-list-wrapping") // better key-value pair readability
5048
override fun createContainer(tag: String): CreateContainerResponse {
5149
return dockerClient.createContainerCmd("$repository:$tag")
52-
.withName(containerName)
50+
.withName("${containerName}_${System.nanoTime()}")
5351
.withHostConfig(hostConfig)
5452
.withCmd(
5553
"--port", "$port",

kuksa-sdk/src/testDebug/kotlin/org/eclipse/kuksa/connectivity/databroker/DebugDataBrokerConfig.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 - 2024 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2023 - 2025 Contributors to the Eclipse Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,5 +19,4 @@
1919

2020
package org.eclipse.kuksa.connectivity.databroker
2121

22-
const val DATABROKER_PORT = 55557
23-
const val DATABROKER_CONTAINER_NAME = "databroker_testDebug"
22+
const val DATABROKER_CONTAINER_NAME = "databroker_test_debug"

kuksa-sdk/src/testRelease/kotlin/org/eclipse/kuksa/connectivity/databroker/ReleaseDataBrokerConfig.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818

1919
package org.eclipse.kuksa.connectivity.databroker
2020

21-
const val DATABROKER_PORT = 55558
22-
const val DATABROKER_CONTAINER_NAME = "databroker_testRelease"
21+
const val DATABROKER_CONTAINER_NAME = "databroker_test_release"

0 commit comments

Comments
 (0)