Kotlin 199 - Intermittent Test
GitHub Decentralized Repo
GitHub Centralized Repo
I have a test that sometimes fails. I have no notion of how that can be happening. Let’s find out.
It’s not failing often. It’s hard for me even to imagine why it would, there’s not much randomness to speak of going on. Let’s review the test:
@Test
fun `saucer changes direction`() {
// test ensures that we cache the saucer rather than create new ones
// cycle receives ELAPSED TIME!
val mix = SpaceObjectCollection()
val game = Game(mix) // makes game without the standard init
game.cycle(0.1) // ELAPSED seconds
game.cycle(7.2) //ELAPSED seconds
val saucer = mix.targets.first() as Saucer
assertThat(saucer.velocity.x > 0.0)
mix.performWithTransaction { it.remove(saucer) }
game.cycle(7.3)
game.cycle(14.4)
game.cycle(14.5)
val nextSaucer = mix.targets.find { it is Saucer}
val actual = nextSaucer as Saucer
assertThat(actual.velocity.x < 0.0)
}
It’s failing on nextSaucer going nil. I add a print to see what is in targets, and I see that there are some asteroids as well as the Saucer … and when it fails, sure enough, the Saucer isn’t there. I’ll bet that the Saucer is colliding with an asteroid and being destroyed. I’ll put a print in there.
Yes! When the test fails, the Saucer has hit an Asteroid! That removes it from the mix and the test fails.
So … how can we fix this? I’m of course certain that the code actually works … it’s just that I’d really like to have a test. Must think about it.
I can’t remove the asteroids, the game would just create new ones. Can I make them not collide? Maybe there’s some way to give the Saucer a huge negative kill radius. Let’s see about that.
Yes, I patched in this code:
init {
if ( SaucerTesting ) killRadius = -50000.0
direction = -1.0
wakeUp()
}
… with a new global SaucerTesting, and when I set it true, the test never fails.
I’m going to roll back that patch and comment out the test for now. Don’t have a better idea for how to make it deterministic at this level. Commit: Comment out intermittent test until I get a decent idea.
I understand the issue and don’t know how to fix the test. Something will come to me. Good enough for now.