Skip to content

Commit

Permalink
Adding a Clumsy meal backfill to help with error testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpawliszyn committed Dec 21, 2023
1 parent be863f8 commit 4409dea
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app.cash.backfila.development.finedining

import app.cash.backfila.client.BackfillConfig
import app.cash.backfila.client.Description
import app.cash.backfila.client.PrepareBackfillConfig
import app.cash.backfila.client.stat.StaticDatasourceBackfill
import javax.inject.Inject
import wisp.logging.getLogger

class ClumsyMealsBackfill @Inject constructor() : StaticDatasourceBackfill<String, ClumsyMealsBackfill.ClumsyMealsAttributes>() {
var servedPlates = 0 // Keeps track of the number of successfully served plates.
var brokenPlatesSoFar = 0 // When plates break it keeps track of how many have broken so far.
override fun validate(config: PrepareBackfillConfig<ClumsyMealsAttributes>) {
if (config.parameters.blockedEntrance) {
throw RuntimeException("Entrance is BLOCKED. No customers can be served!")
}
}

override fun runOne(item: String, config: BackfillConfig<ClumsyMealsAttributes>) {
Thread.sleep(100L) // Sleep for half a second
// We potentially break on every 25th plate.
if (servedPlates % 25 == 0 && brokenPlatesSoFar < config.parameters.brokenPlates) {
brokenPlatesSoFar++
logger.info { "Broke a plate!" }
throw RuntimeException("Failed to serve: Broke a plate!!!")
}
logger.info { "Poorly served $item" }
servedPlates++
brokenPlatesSoFar = 0
}

@Description("Adaptable clumsiness.")
data class ClumsyMealsAttributes(
@Description("Whether the entrance is blocked or not. Can be used to test Prepare failures.")
val blockedEntrance: Boolean = true,
@Description("How many plates break before a successful plate is served. Can be used to test retry logic.")
val brokenPlates: Int = 1,
)

// Generate the meal place settings for the backfill.
override val staticDatasource: List<String> = (1..5000).map { i -> "place setting $i" }

companion object {
private val logger = getLogger<ClumsyMealsBackfill>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ internal class FineDiningServiceModule : KAbstractModule() {
),
)
install(StaticDatasourceBackfillModule.create<SlowMealsBackfill>())
install(StaticDatasourceBackfillModule.create<ClumsyMealsBackfill>())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class SlowMealsBackfill @Inject constructor() : StaticDatasourceBackfill<String,
}

data class SlowMealsAttributes(
val mealDelayMs: Long = 1000L,
val mealDelayMs: Long = 100L,
)

// Generate the meal place settings for the backfill.
override val staticDatasource: List<String> = (1..1000).map { i -> "place setting $i" }
override val staticDatasource: List<String> = (1..5000).map { i -> "place setting $i" }

companion object {
private val logger = getLogger<SlowMealsBackfill>()
Expand Down

0 comments on commit 4409dea

Please sign in to comment.