Skip to content

Commit

Permalink
🚑️ Remove accidental StackOverflow on beforeSpec (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColman authored Jun 27, 2023
1 parent 6ef6af8 commit cf08e8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import org.testcontainers.containers.GenericContainer
* @param afterShutdown a callback that is invoked only once, just after the container is stopped.
* If the container is never started, this callback will not be invoked.
*/
class ContainerExtension<T : GenericContainer<T>>(
class ContainerExtension<T : GenericContainer<*>>(
private val container: T,
private val mode: ContainerLifecycleMode = ContainerLifecycleMode.Project,
private val beforeStart: () -> Unit = {},
Expand Down Expand Up @@ -86,19 +86,19 @@ class ContainerExtension<T : GenericContainer<T>>(
}

override suspend fun beforeTest(testCase: TestCase) {
beforeTest(testCase)
beforeTest.invoke(testCase)
}

override suspend fun afterTest(testCase: TestCase, result: TestResult) {
afterTest(testCase)
}

override suspend fun beforeSpec(spec: Spec) {
beforeSpec(spec)
beforeSpec.invoke(spec)
}

override suspend fun afterSpec(spec: Spec) {
afterSpec(spec)
afterSpec.invoke(spec)
if (mode == ContainerLifecycleMode.Spec && container.isRunning) close()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ private val container = GenericContainer("redis:5.0.3-alpine").apply {
withExposedPorts(6379)
}

private val ext = SharedTestContainerExtension(container) {
JedisPool(container.host, container.firstMappedPort)
}
private val ext = ContainerExtension(container)

class SharedTestContainerExtensionTest1 : FunSpec() {
init {

val jedis = install(ext)
val installed = install(ext)
val jedis = JedisPool(installed.host, installed.firstMappedPort)


test("should be initialized in the spec") {
jedis.resource.set("foo", "bar")
Expand All @@ -34,7 +34,8 @@ class SharedTestContainerExtensionTest1 : FunSpec() {
class SharedTestContainerExtensionTest2 : FunSpec() {
init {

val jedis = install(ext)
val installed = install(ext)
val jedis = JedisPool(installed.host, installed.firstMappedPort)

test("this spec should share the container") {
jedis.resource.get("foo") shouldBe "bar"
Expand Down

0 comments on commit cf08e8d

Please sign in to comment.