Skip to content

Commit

Permalink
fix(queue): argh Kotlin when expressions don't work quite how I thoug…
Browse files Browse the repository at this point in the history
…ht (#1309)
  • Loading branch information
robfletcher committed Apr 28, 2017
1 parent 84d1e3f commit c5dbe10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ interface MessageHandler<M : Message> : (Message) -> Unit {
val repository: ExecutionRepository

override fun invoke(message: Message): Unit =
when (message.javaClass) {
messageType -> {
@Suppress("UNCHECKED_CAST")
handle(message as M)
}
else ->
throw IllegalArgumentException("Unsupported message type ${message.javaClass.simpleName}")
if (messageType.isAssignableFrom(message.javaClass)) {
@Suppress("UNCHECKED_CAST")
handle(message as M)
} else {
throw IllegalArgumentException("Unsupported message type ${message.javaClass.simpleName}")
}

fun handle(message: M): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.netflix.spinnaker.orca.pipeline.model.Pipeline
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.reset
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.verifyZeroInteractions
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
Expand All @@ -34,16 +35,16 @@ class MessageHandlerSpec : Spek({
val repository: ExecutionRepository = mock()
val handleCallback: (Message) -> Unit = mock()

val handler = object : MessageHandler<StartExecution> {
val handler = object : MessageHandler<ConfigurationError> {
override val queue
get() = queue

override val repository
get() = repository

override val messageType = StartExecution::class.java
override val messageType = ConfigurationError::class.java

override fun handle(message: StartExecution) {
override fun handle(message: ConfigurationError) {
handleCallback.invoke(message)
}
}
Expand All @@ -66,4 +67,18 @@ class MessageHandlerSpec : Spek({
verifyZeroInteractions(handleCallback)
}
}

describe("when the handler is passed a sub-type of message") {
val message = InvalidExecutionId(Pipeline::class.java, "1", "foo")

afterGroup(::resetMocks)

action("a message is handled") {
handler.invoke(message)
}

it("does not invoke the handler") {
verify(handleCallback).invoke(message)
}
}
})

0 comments on commit c5dbe10

Please sign in to comment.