PersistentActor에서 컨텍스트를 사용할 때 이상한 동작이 발생합니다 (지속성이 문제의 원인과 관련이 있는지 확실하지 않음). 내 코드는 다음과 같습니다.Akka의 컨텍스트. 이상한 동작으로
class MyActor extends PersistentActor {
import context._
// Messages
case object Start
case object Ready
case object MessageX
// Events
case object Started
def recieveRecover: Receive = { /* Not relevant, I think */}
def receiveCommand: Receive = idle
def idle: Receive = {
case Start =>
persist(Started) { _ =>
sender() ! Ready
become(ready)
}
}
def ready: Receive = {
case MessageX => doSomething()
}
}
그리고 동일한 MyActorSpec 파일에서 두 가지 테스트가 있습니다. 첫 번째는 단순히 '대기'상태를 테스트하고 두 번째는 '준비'상태를 테스트합니다
"Test A" in {
val actorRef = system.actorOf(MyActor.props(), "test-A-actor")
actorRef ! Start
expectMsg(Ready)
}
"Test B" in {
val actorRef = system.actorOf(MyActor.props(), "test-B-actor")
actorRef ! Start
expectMsg(Ready) /* It fails here because for some reason the actorRef
was created with its 'receiveCommand' block equal to
the 'ready' block and not equal to the 'idle' block as its suppossed to.
So it timeouts here waiting for the message which is not handled in
the 'ready' block */
actorRef ! MessageX
testSomethingAboutTheMessageX()
}
내가 두 테스트를 실행하는 경우, 첫 번째는 성공하지만 두 번째는 준비 메시지를 기다리는 실패 (로 두 번째 테스트에서 주석에서 설명했다). 두 번째 테스트 만 통과하면 통과합니다. 그래서 액터를 정의 할 때 내가 뭔가 잘못했는지 확신 할 수 없습니다.
UPDATE : (두 배우가 idle
상태에서 만든) 내가 제안으로 Started
이벤트에 대한 지속성합니다 (persist(Started)
부분)를 제거하려 예상대로 테스트를했다. 따라서, 첫 번째 액터 인스턴스의 이벤트가 지속되고 두 번째 액터 인스턴스가이를 재생하고 있으며 이는 액터 인스턴스가 동일한 persistenceId
(DOH!)으로 생성되므로 발생합니다. 따라서 테스트를 독립적으로 만드는 방법은 각각의 액터를 다른 persistenceId
으로 인스턴스화하는 것입니다.
어떤 테스트가 실행되는지에 따라 결과가 변경된다는 사실은 테스트가 독립적이지 않다는 것을 나타냅니다. 공유 상태가 없음을 확인하기 위해 각 테스트 내에서 새로운 '시스템'을 만드는 것이 좋습니다. – millhouse