Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Support usage of last* methods in pattern guards
Browse files Browse the repository at this point in the history
- closes #205
  • Loading branch information
krasserm committed Feb 12, 2016
1 parent 56cf325 commit 69f3221
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/scala/com/rbmhtechnology/eventuate/EventsourcedView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,17 @@ trait EventsourcedView extends Actor with Stash with ActorLogging {
*/
private[eventuate] def receiveEvent(event: DurableEvent): Unit = {
val behavior = _eventContext.current
if (behavior.isDefinedAt(event.payload)) try {
_eventHandling = true
receiveEventInternal(event)
behavior(event.payload)
if (!recovering) conditionChanged(currentVectorTime)
} finally _eventHandling = false
val previous = lastHandledEvent

_lastHandledEvent = event
if (behavior.isDefinedAt(event.payload)) {
try {
_eventHandling = true
receiveEventInternal(event)
behavior(event.payload)
if (!recovering) conditionChanged(currentVectorTime)
} finally _eventHandling = false

This comment has been minimized.

Copy link
@volkerstampa

volkerstampa Feb 12, 2016

Contributor

Not related to this change, but why does this actually has to be done in a finally branch? In case of an exception we will anyways get a new actor-instance, right?

This comment has been minimized.

Copy link
@krasserm

krasserm Feb 12, 2016

Author Contributor

That's a left-over from an older implementation that required eventHandling to be false in preRestart. This is obsolete now, will change that.

} else _lastHandledEvent = previous
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ object EventsourcedViewSpec {
add orElse change(eventContext)
}

class TestGuardingView(
val logProbe: ActorRef,
val msgProbe: ActorRef) extends EventsourcedView {

val id = emitterIdA
val eventLog = logProbe

override def onCommand = {
case "last" => msgProbe ! lastHandledEvent
}

override def onEvent = {
case "e1" if lastEmitterId == "x" =>
msgProbe ! "handled"
case "e2" if lastEmitterId == "y" =>
msgProbe ! "handled"
}
}

val event1a = event("a", 1L)
val event1b = event("b", 2L)
val event1c = event("c", 3L)
Expand Down Expand Up @@ -195,6 +214,9 @@ class EventsourcedViewSpec extends TestKit(ActorSystem("test")) with WordSpecLik
def recoveredBehaviorView(): ActorRef =
processRecover(system.actorOf(Props(new TestBehaviorView(logProbe.ref, msgProbe.ref))))

def recoveredGuardingView(): ActorRef =
processRecover(system.actorOf(Props(new TestGuardingView(logProbe.ref, msgProbe.ref))))

def processRecover(actor: ActorRef, instanceId: Int = instanceId): ActorRef = {
logProbe.expectMsg(LoadSnapshot(emitterIdA, instanceId))
logProbe.sender() ! LoadSnapshotSuccess(None, instanceId)
Expand Down Expand Up @@ -473,5 +495,27 @@ class EventsourcedViewSpec extends TestKit(ActorSystem("test")) with WordSpecLik
system.stop(logProbe.ref)
expectTerminated(actor)
}
"support usage of last* methods in pattern guards when guard evaluates to true" in {
val actor = recoveredGuardingView()
val event1 = event("e1", 1L).copy(emitterId = "x")

actor ! Written(event1)
msgProbe.expectMsg("handled")

actor ! "last"
msgProbe.expectMsgType[DurableEvent].payload should be("e1")
}
"support usage of last* methods in pattern guards when guard evaluates to false" in {
val actor = recoveredGuardingView()
val event1 = event("e1", 1L).copy(emitterId = "x")
val event2 = event("e2", 1L).copy(emitterId = "x")

actor ! Written(event1)
actor ! Written(event2)
msgProbe.expectMsg("handled")

actor ! "last"
msgProbe.expectMsgType[DurableEvent].payload should be("e1")
}
}
}

1 comment on commit 69f3221

@volkerstampa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Please sign in to comment.