Skip to content

Commit 39da6fd

Browse files
authored
Reuse test server port (#57)
* Reuse test server port * Fix tests
1 parent cb5eb6a commit 39da6fd

11 files changed

Lines changed: 41 additions & 13 deletions

File tree

samples/guides/src/main/kotlin/app/cash/tempest/guides/DataModeling.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ class DataModeling(private val db: MusicDb) {
3434
val mapper = DynamoDBMapper(client, mapperConfig)
3535
val db: MusicDb = LogicalDb(mapper)
3636
}
37-
}
37+
}

samples/guides2/src/main/kotlin/app/cash/tempest2/guides/AsynchronousProgramming.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class AsynchronousProgramming(
3232
.expressionValues(mapOf(":playlist_version" to AttributeValue.builder().n("$playlist_version").build()))
3333
.build()
3434
}
35-
}
35+
}

samples/guides2/src/main/kotlin/app/cash/tempest2/guides/DataModeling.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ class DataModeling(private val db: MusicDb) {
2929
.build()
3030
val db: MusicDb = LogicalDb(enhancedClient)
3131
}
32-
}
32+
}

samples/guides2/src/main/kotlin/app/cash/tempest2/guides/QueryNScan.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class QueryNScan(
8686
.expression("run_length > :duration")
8787
.expressionValues(
8888
mapOf(
89-
":duration" to AttributeValue.builder().s(duration.toString()).build()))
89+
":duration" to AttributeValue.builder().s(duration.toString()).build()
90+
)
91+
)
9092
.build()
9193
}
9294

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ include(":samples:guides-junit5")
1919
include(":samples:guides2")
2020
include(":samples:guides2-junit4")
2121
include(":samples:guides2-junit5")
22-
include(":samples:javarecord")
22+
// Don't enable this module, which depends on Java 14.
23+
// include(":samples:javarecord")
2324
include(":samples:musiclibrary")
2425
include(":samples:musiclibrary2")
2526
include(":samples:musiclibrary-testing")

tempest-testing-junit4/src/main/kotlin/app/cash/tempest/testing/TestDynamoDb.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TestDynamoDb private constructor(
7979
}
8080

8181
fun build(): TestDynamoDb {
82-
val port = port ?: pickRandomPort()
82+
val port = port ?: defaultPort(serverFactory.toString())
8383
return TestDynamoDb(
8484
DefaultTestDynamoDbClient(tables, port),
8585
serverFactory.create(port)
@@ -88,6 +88,12 @@ class TestDynamoDb private constructor(
8888
}
8989

9090
companion object {
91+
private val defaultPorts = ConcurrentHashMap<String, Int>()
92+
fun defaultPort(key: String): Int {
93+
// Only pick random port once to share one test server with multiple tests.
94+
return defaultPorts.getOrPut(key, ::pickRandomPort)
95+
}
96+
9197
private val runningServers = ConcurrentHashMap.newKeySet<String>()
9298
private val log = getLogger<TestDynamoDb>()
9399
}

tempest-testing-junit5/src/main/kotlin/app/cash/tempest/testing/TestDynamoDb.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TestDynamoDb private constructor(
8181
}
8282

8383
fun build(): TestDynamoDb {
84-
val port = port ?: pickRandomPort()
84+
val port = port ?: defaultPort(serverFactory.toString())
8585
return TestDynamoDb(
8686
DefaultTestDynamoDbClient(tables, port),
8787
serverFactory.create(port)
@@ -90,6 +90,12 @@ class TestDynamoDb private constructor(
9090
}
9191

9292
companion object {
93+
private val defaultPorts = ConcurrentHashMap<String, Int>()
94+
fun defaultPort(key: String): Int {
95+
// Only pick random port once to share one test server with multiple tests.
96+
return defaultPorts.getOrPut(key, ::pickRandomPort)
97+
}
98+
9399
private val runningServers = ConcurrentHashMap.newKeySet<String>()
94100
private val log = getLogger<TestDynamoDb>()
95101
}

tempest2-testing-junit4/src/main/kotlin/app/cash/tempest2/testing/TestDynamoDb.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TestDynamoDb private constructor(
7979
}
8080

8181
fun build(): TestDynamoDb {
82-
val port = port ?: pickRandomPort()
82+
val port = port ?: defaultPort(serverFactory.toString())
8383
return TestDynamoDb(
8484
DefaultTestDynamoDbClient(tables, port),
8585
serverFactory.create(port)
@@ -88,6 +88,12 @@ class TestDynamoDb private constructor(
8888
}
8989

9090
companion object {
91+
private val defaultPorts = ConcurrentHashMap<String, Int>()
92+
fun defaultPort(key: String): Int {
93+
// Only pick random port once to share one test server with multiple tests.
94+
return defaultPorts.getOrPut(key, ::pickRandomPort)
95+
}
96+
9197
private val runningServers = ConcurrentHashMap.newKeySet<String>()
9298
private val log = getLogger<TestDynamoDb>()
9399
}

tempest2-testing-junit5/src/main/kotlin/app/cash/tempest2/testing/TestDynamoDb.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TestDynamoDb private constructor(
8181
}
8282

8383
fun build(): TestDynamoDb {
84-
val port = port ?: pickRandomPort()
84+
val port = port ?: defaultPort(serverFactory.toString())
8585
return TestDynamoDb(
8686
DefaultTestDynamoDbClient(tables, port),
8787
serverFactory.create(port)
@@ -90,6 +90,12 @@ class TestDynamoDb private constructor(
9090
}
9191

9292
companion object {
93+
private val defaultPorts = ConcurrentHashMap<String, Int>()
94+
fun defaultPort(key: String): Int {
95+
// Only pick random port once to share one test server with multiple tests.
96+
return defaultPorts.getOrPut(key, ::pickRandomPort)
97+
}
98+
9399
private val runningServers = ConcurrentHashMap.newKeySet<String>()
94100
private val log = getLogger<TestDynamoDb>()
95101
}

tempest2/src/main/kotlin/app/cash/tempest2/internal/DynamoDbLogicalDb.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ internal class DynamoDbLogicalDb(
150150
val writeRequest = toTransactionWriteRequest(writeSet)
151151
return dynamoDbEnhancedClient.transactWriteItems(writeRequest)
152152
.exceptionally { e ->
153-
if (e is TransactionCanceledException) {
154-
toTransactionWriteException(writeSet, e) as Void
153+
// `e` is a java.util.concurrent.CancellationException.
154+
if (e.cause is TransactionCanceledException) {
155+
toTransactionWriteException(writeSet, e.cause as TransactionCanceledException) as Void
155156
} else {
156157
throw e
157158
}

0 commit comments

Comments
 (0)