Skip to content

Commit

Permalink
No longer use env vars for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 14, 2024
1 parent 753288c commit 59ef884
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
8 changes: 0 additions & 8 deletions core/kubernetes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies {

testImplementation(kotlin("test-junit5"))
testImplementation(projects.api)
testImplementation(libs.system.rules)
testImplementation(libs.kord.core)
}

Expand All @@ -35,13 +34,6 @@ testing {
}
}

tasks {
test {
jvmArgs =
listOf("--add-opens", "java.base/java.lang=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED")
}
}

mikbotPlugin {
description = "Plugin providing an /healthz endpoint used for health checking."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package dev.schlaubi.mikbot.core.health

import dev.schlaubi.mikbot.plugin.api.EnvironmentConfig

@Suppress("PropertyName")
open class Config : EnvironmentConfig() {
object Config : EnvironmentConfig() {
val ENABLE_SCALING by getEnv(false, String::toBooleanStrict)
val POD_ID by getEnv(transform = String::toInt)
val SHARDS_PER_POD by getEnv(2, String::toInt)
val TOTAL_SHARDS by getEnv(transform = String::toInt)
val STATEFUL_SET_NAME by this
val NAMESPACE by getEnv("default")
val CONTAINER_NAME by this

companion object : Config()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import io.github.oshai.kotlinlogging.KotlinLogging

private val LOG = KotlinLogging.logger { }

fun calculateShards(config: Config = Config, podId: Int = config.POD_ID): Shards {
val totalShards = config.TOTAL_SHARDS
val firstShard = config.SHARDS_PER_POD * podId
val lastShard = (firstShard + (config.SHARDS_PER_POD - 1)).coerceAtMost(totalShards - 1)
fun calculateShards(shardsPerPod: Int = Config.SHARDS_PER_POD, totalShards: Int = Config.TOTAL_SHARDS, podId: Int = Config.POD_ID): Shards {
val totalShards = totalShards
val firstShard = shardsPerPod * podId
val lastShard = (firstShard + (shardsPerPod - 1)).coerceAtMost(totalShards - 1)

LOG.debug { "Determined shards for $podId ($firstShard..$lastShard)" }
return Shards(totalShards, firstShard..lastShard)
Expand Down
41 changes: 22 additions & 19 deletions core/kubernetes/src/test/kotlin/ShardingCalculatorTest.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
package dev.schlaubi.mikbot.core.kubernetes.test

import dev.kord.gateway.builder.Shards
import dev.schlaubi.mikbot.core.health.Config
import dev.schlaubi.mikbot.core.health.calculateShards
import org.junitpioneer.jupiter.SetEnvironmentVariable
import kotlin.test.Test
import kotlin.test.assertTrue

class ShardingCalculatorTest {
@SetEnvironmentVariable(key = "TOTAL_SHARDS", value = "4")
@Test
fun `test correct shard config for even shard count`() {
val config = Config()

assertSameShardCount(Shards(4, 0..1), calculateShards(config, podId = 0))
assertSameShardCount(Shards(4, 2..3), calculateShards(config, podId = 1))
val totalShards = 4
assertSameShardCount(Shards(4, 0..1), calculateShards(totalShards = totalShards, podId = 0))
assertSameShardCount(Shards(4, 2..3), calculateShards(totalShards = totalShards, podId = 1))
}


@SetEnvironmentVariable(key = "TOTAL_SHARDS", value = "5")
@Test
fun `test correct shard config for uneven shard count`() {
val config = Config()
assertSameShardCount(Shards(5, 0..1), calculateShards(config, podId = 0))
assertSameShardCount(Shards(5, 2..3), calculateShards(config, podId = 1))
assertSameShardCount(Shards(5, listOf(4)), calculateShards(config, podId = 2))
val totalShards = 5

assertSameShardCount(Shards(5, 0..1), calculateShards(totalShards = totalShards, podId = 0))
assertSameShardCount(Shards(5, 2..3), calculateShards(totalShards = totalShards, podId = 1))
assertSameShardCount(Shards(5, listOf(4)), calculateShards(totalShards = totalShards, podId = 2))
}

@SetEnvironmentVariable(key = "TOTAL_SHARDS", value = "3")
@SetEnvironmentVariable(key = "SHARDS_PER_POD", value = "1")
@Test
fun `test correct shard config for one shard per Pod`() {
val config = Config()
val totalShards = 3

assertSameShardCount(Shards(totalShards, listOf(0)), calculateShards(config, podId = 0))
assertSameShardCount(Shards(totalShards, listOf(1)), calculateShards(config, podId = 1))
assertSameShardCount(Shards(totalShards, listOf(2)), calculateShards(config, podId = 2))
val shardsPerPod = 1

assertSameShardCount(
Shards(totalShards, listOf(0)),
calculateShards(totalShards = totalShards, shardsPerPod = shardsPerPod, podId = 0)
)
assertSameShardCount(
Shards(totalShards, listOf(1)),
calculateShards(totalShards = totalShards, shardsPerPod = shardsPerPod, podId = 1)
)
assertSameShardCount(
Shards(totalShards, listOf(2)),
calculateShards(totalShards = totalShards, shardsPerPod = shardsPerPod, podId = 2)
)
}
}

Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ koin = { group = "io.insert-koin", name = "koin-core", version = "3.5.6" }

asm = { group = "org.ow2.asm", name = "asm", version = "9.7" }

system-rules = { group = "org.junit-pioneer", name = "junit-pioneer", version = "2.2.0" }
mikbot-api = { group = "dev.schlaubi", name = "mikbot-api" }

[plugins]
Expand Down

0 comments on commit 59ef884

Please sign in to comment.