Skip to content

Commit

Permalink
repeated start and stop test
Browse files Browse the repository at this point in the history
  • Loading branch information
travishaagen committed Oct 12, 2024
1 parent 3cb56eb commit ca462df
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")
testImplementation("org.testcontainers:junit-jupiter:$testcontainersVersion")
testImplementation("io.rest-assured:rest-assured:$restAssuredVersion")
testImplementation("io.rest-assured:kotlin-extensions:$restAssuredVersion")
}

group = "com.onepeloton.locust4k"
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/com/onepeloton/locust4k/LocustWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,7 @@ class LocustWorker(
taskInstance.execute(stats, taskContext)
}
if (isActive.not()) {
logger.warn {
"Task (${taskInstance.name()}) no longer active. Was CancellationException suppressed?"
}
logger.info { "Task (${taskInstance.name()}) cancelled (inactive)" }
return@launch
}
} catch (e: CancellationException) {
Expand Down
65 changes: 61 additions & 4 deletions src/test/kotlin/com/onepeloton/locust4k/examples/ExampleAppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.time.Duration

class ExampleAppTest : TestcontainersBase() {
@Test
fun singleWorkerWithLocustMasterEndingTest() {
fun singleUserWithHeadlessLocustMasterEndingTest() {
val locustMasterContainer = buildLocustMasterContainer(runtimeSeconds = 5)
val locustWorkerExampleContainer = buildLocustWorkerExampleContainer()
try {
Expand Down Expand Up @@ -39,6 +39,7 @@ class ExampleAppTest : TestcontainersBase() {
workerLogs,
stringContainsInOrder(
"Starting Locust Worker",
"Connecting",
"Connected to Locust",
"Received ack",
"Spawn message from controller",
Expand All @@ -62,7 +63,7 @@ class ExampleAppTest : TestcontainersBase() {
}

@Test
fun singleWorkerMultipleUsersWithLocustMasterEndingTest() {
fun multipleUsersWithHeadlessLocustMasterEndingTest() {
val locustMasterContainer = buildLocustMasterContainer(users = 3, spawnRate = 2, runtimeSeconds = 5)
val locustWorkerExampleContainer = buildLocustWorkerExampleContainer()
try {
Expand Down Expand Up @@ -93,6 +94,7 @@ class ExampleAppTest : TestcontainersBase() {
workerLogs,
stringContainsInOrder(
"Starting Locust Worker",
"Connecting",
"Connected to Locust",
"Received ack",
"Spawn message from controller",
Expand All @@ -105,8 +107,8 @@ class ExampleAppTest : TestcontainersBase() {
"beforeExecuteLoop invoked",
"Spawning Complete",
"Quit message",
"afterExecuteLoop invoked",
"afterExecuteLoop invoked",
// next two messages each repeated 3 times, but order inconsistent
"Task (exampleTask) cancelled",
"afterExecuteLoop invoked",
"Stats consumer stopped",
"Stats reporter stopped",
Expand All @@ -119,4 +121,59 @@ class ExampleAppTest : TestcontainersBase() {
stopContainers(locustMasterContainer, locustWorkerExampleContainer)
}
}

@Test
fun singleUserWithRepeatedStartAndStop() {
val locustMasterContainer = buildLocustMasterContainer(headless = false)
val locustWorkerExampleContainer = buildLocustWorkerExampleContainer()
try {
// given
startContainers(locustMasterContainer, locustWorkerExampleContainer)

LogMessageWaitStrategy()
.withRegEx(".*1 workers connected.*\\s")
.withStartupTimeout(Duration.ofSeconds(5))
.waitUntilReady(locustMasterContainer)

// when
repeat(2) {
startLoadTest(locustMasterContainer)

LogMessageWaitStrategy()
.withRegEx(".* Spawning Complete message from controller.*\\s")
.withStartupTimeout(Duration.ofSeconds(5))
.waitUntilReady(locustWorkerExampleContainer)

stopLoadTest(locustMasterContainer)
}
val workerLogs = locustWorkerExampleContainer.logs

// then
assertThat(
workerLogs,
stringContainsInOrder(
"Starting Locust Worker",
"Connecting",
"Connected to Locust",
"Received ack",
// first run
"Spawn message from controller",
"Stats reset",
"+1 (-0) user-units",
"Spawning Complete",
"Stop message from controller",
"Ack message from controller",
// second run
"Spawn message from controller",
"Stats reset",
"+1 (-0) user-units",
"Spawning Complete",
"Stop message from controller",
"Ack message from controller",
),
)
} finally {
stopContainers(locustMasterContainer, locustWorkerExampleContainer)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.onepeloton.locust4k.examples

import io.github.oshai.kotlinlogging.KotlinLogging
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.junit.jupiter.api.fail
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.Network
Expand Down Expand Up @@ -99,4 +102,30 @@ abstract class TestcontainersBase {
}
}
}

protected fun startLoadTest(
locustMasterContainer: GenericContainer<*>,
users: Int = 1,
spawnRate: Int = 1,
) {
Given {
port(locustMasterContainer.getMappedPort(8089))
formParam("user_count", users)
formParam("spawn_rate", spawnRate)
} When {
post("/swarm")
} Then {
statusCode(200)
}
}

protected fun stopLoadTest(locustMasterContainer: GenericContainer<*>) {
Given {
port(locustMasterContainer.getMappedPort(8089))
} When {
get("/stop")
} Then {
statusCode(200)
}
}
}

0 comments on commit ca462df

Please sign in to comment.