In the following code, I just send one message (int 1) to the testActor,
but in expectMsgPF, it receive duplicate messages (two 1s).
Please help.
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.testkit.javadsl.TestKit;
public class Main {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("system");
new TestKit(system) {{
ActorRef sender = system.actorOf(SendingActor.props(getTestActor()), "sender");
// just send 1 once.
sender.tell(1, getRef());
expectMsgPF("test", m -> {
// print 1 twice.
System.out.println(m);
return 0;
});
}};
TestKit.shutdownActorSystem(system);
}
}
class SendingActor extends AbstractActor {
private ActorRef receiver;
static Props props(ActorRef receiver) {
return Props.create(SendingActor.class, receiver);
}
public SendingActor(ActorRef receiver) {
this.receiver = receiver;
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(o -> {
receiver.tell(o, self());
})
.build();
}
}
Oops, this is because the JavaPartialFunction.apply is invoked two times.
I will fix it.
@taigua
FYI, the issue is fixed and the pr is merged.
@Hawstein
Thanks. I downloaded your patch and made a personal build, it works.
Most helpful comment
Oops, this is because the JavaPartialFunction.apply is invoked two times.
I will fix it.