Skip to content

Commit

Permalink
🔥Undo av command contexts: yoink
Browse files Browse the repository at this point in the history
Vi har ikke bruk for undo lenger. Nå som alt kjører i en
transaksjon, blir alt (inkludert tilstandsendringer på kommandokjeden)
uansett rullet tilbake til forrige tilstand når noe går galt.

Co-authored-by: Christian Skovborg Gule <[email protected]>
Co-authored-by: Øydis Kind Refsum <[email protected]>
  • Loading branch information
3 people committed Nov 1, 2024
1 parent 60b156c commit 2dae0d0
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,6 @@ internal class Kommandofabrikk(
} else {
logg.info("${command.name} er suspendert")
}
} catch (err: Exception) {
command.undo(commandContext)
throw err
} finally {
commandContextObservers.forEach { commandContext.avregistrerObserver(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ internal interface Command {

fun resume(context: CommandContext) = true

fun undo(context: CommandContext) {}

fun hash(): String = name

val name: String get() = this::class.java.simpleName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ internal abstract class MacroCommand : Command {
return run(context, commands.subList(currentIndex, commands.size))
}

final override fun undo(context: CommandContext) {
logg.info("Reverserer utførelse av ${this::class.simpleName}")
context.register(this)
historikk.forEach { it.undo(context) }
context.clear()
}

final override fun hash(): String {
return name + commands.joinToString { it.hash() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ internal class OpprettKoblingTilHendelseCommand(
vedtakDao.opprettKobling(vedtaksperiodeId, meldingId)
return true
}

override fun undo(context: CommandContext) {
vedtakDao.fjernKobling(vedtaksperiodeId, meldingId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ internal class OpprettKoblingTilUtbetalingCommand(
utbetalingRepository.opprettKobling(vedtaksperiodeId, utbetalingId)
return true
}

override fun undo(context: CommandContext) {
utbetalingRepository.fjernKobling(vedtaksperiodeId, utbetalingId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ internal class CommandContextTest {
) : Command {
var executed = false
var resumed = false
var undo = false

val id: UUID = HENDELSE

Expand All @@ -247,8 +246,5 @@ internal class CommandContextTest {
return resumeAction(this)
}

override fun undo(context: CommandContext) {
undo = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package no.nav.helse.modell.kommando

import no.nav.helse.modell.kommando.CommandContext.Companion.ferdigstill
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import java.util.*
import org.junit.jupiter.api.Assertions.assertEquals
import java.util.UUID

internal class MacroCommandTest {
private val constants: MutableList<String> = mutableListOf()
private var executeCount: Int = 0
private var resumeCount: Int = 0
private var undoCount: Int = 0

private lateinit var context: CommandContext

Expand All @@ -22,7 +21,6 @@ internal class MacroCommandTest {
constants.clear()
executeCount = 0
resumeCount = 0
undoCount = 0
context = CommandContext(UUID.randomUUID())
}

Expand Down Expand Up @@ -116,91 +114,6 @@ internal class MacroCommandTest {
assertRekkefølge("A", "B før")
}

@Test
fun `Undo gjør ingenting dersom ingen kommandoer er kjørt`() {
val macroCommand =
command(
execute = { constants.add("B før"); true },
undo = { constants.add("B etter") }
) +
command(
execute = { constants.add("C før"); true },
undo = { constants.add("C etter") }
)
macroCommand.undo(context)
assertRekkefølge()
assertTellere(0, 0, 0)
}

@Test
fun `Undo reverserer alle kommandoer`() {
val macroCommand =
command(
execute = { constants.add("B før"); true },
undo = { constants.add("B etter") }
) +
command(
execute = { constants.add("C før"); true },
undo = { constants.add("C etter") }
)
macroCommand.execute(context)
macroCommand.undo(context)
assertRekkefølge("B før", "C før", "C etter", "B etter")
assertTellere(2, 0, 2)
}

@Test
fun `Undo reverserer alle kjørte kommandoer når en kommando suspender`() {
val macroCommand =
command(
execute = { constants.add("B før"); false },
undo = { constants.add("B etter") }
) +
command(
execute = { constants.add("C før"); true },
undo = { constants.add("C etter") }
)
macroCommand.execute(context)
macroCommand.undo(context)
assertRekkefølge("B før", "B etter")
assertTellere(1, 0, 1)
}

@Test
fun `Undo reverserer riktig etter restore`() {
val macroCommand =
command(
execute = { constants.add("B før"); false },
undo = { constants.add("B etter") }
) +
command(
execute = { constants.add("C før"); true },
undo = { constants.add("C etter") }
)
macroCommand.undo(CommandContext(UUID.randomUUID(), listOf(1)))
assertRekkefølge("C etter", "B etter")
assertTellere(0, 0, 2)
}

@Test
fun `Undo reverserer riktig etter resume`() {
val macroCommand =
command(
execute = { constants.add("B før"); true },
undo = { constants.add("B undo") }
) +
command(
execute = { constants.add("C før"); false },
resume = { constants.add("C etter"); true },
undo = { constants.add("C undo") }
)
context = CommandContext(UUID.randomUUID(), listOf(1))
macroCommand.resume(context)
macroCommand.undo(context)
assertRekkefølge("C etter", "C undo", "B undo")
assertTellere(0, 1, 2)
}

@Test
fun `Utfører ikke commands etter at context er ferdigstilt i execute`() {
val macroCommand =
Expand Down Expand Up @@ -230,19 +143,13 @@ internal class MacroCommandTest {
assertEquals(konstanter.toList(), constants)
}

private fun assertTellere(expectedExecuteCount: Int, expectedResumeCount: Int, expectedUndoCount: Int) {
assertEquals(expectedExecuteCount, executeCount)
assertEquals(expectedResumeCount, resumeCount)
assertEquals(expectedUndoCount, undoCount)
}

private operator fun Command.plus(other: Command): MacroCommand {
return object : MacroCommand() {
override val commands: List<Command> = listOf(this@plus, other)
}
}

private fun command(execute: Command.(context: CommandContext) -> Boolean, resume: Command.(context: CommandContext) -> Boolean = { true }, undo: () -> Unit = {}): Command {
private fun command(execute: Command.(context: CommandContext) -> Boolean, resume: Command.(context: CommandContext) -> Boolean = { true }): Command {
return object : Command {
override fun execute(context: CommandContext): Boolean {
executeCount += 1
Expand All @@ -254,10 +161,6 @@ internal class MacroCommandTest {
return resume(this, context)
}

override fun undo(context: CommandContext) {
undoCount += 1
return undo()
}
}
}
}

0 comments on commit 2dae0d0

Please sign in to comment.