-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a Clumsy meal backfill to help with error testing.
- Loading branch information
1 parent
be863f8
commit 4409dea
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
service/src/test/kotlin/app/cash/backfila/development/finedining/ClumsyMealsBackfill.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters