Replies: 4 comments 6 replies
-
@d1r7y Yeah with the latest commit hash in the main branch you should be able to subscribe to the event stream and handle the |
Beta Was this translation helpful? Give feedback.
-
@d1r7y This is what I can think of regarding your use case:
These are rough ideas. Bear in mind the TestKit is to help assert that an actor is receiving the message it is supposed to. Things like clustering and co are not yet implemented in the TestKit. Also I do have extensive unit tests in the framework you can look at and pick up some clues. |
Beta Was this translation helpful? Give feedback.
-
@d1r7y this is what I came up with: type A1 struct{}
var _ actors.Actor = (*A1)(nil)
func (x *A1) PreStart(ctx *actors.Context) error {
return nil
}
func (x *A1) Receive(ctx *actors.ReceiveContext) {
switch ctx.Message().(type) {
case *testpb.TestPing:
// create A2 first before sending the message
cid := ctx.Spawn("A2", &A2{})
if cid != nil && !cid.Equals(actors.NoSender) {
ctx.Tell(ctx.Sender(), new(testpb.TestPong))
}
}
}
func (x *A1) PostStop(ctx *actors.Context) error {
return nil
}
type A2 struct{}
var _ actors.Actor = (*A2)(nil)
func (x *A2) PreStart(ctx *actors.Context) error {
return nil
}
func (x *A2) Receive(ctx *actors.ReceiveContext) {
switch ctx.Message().(type) {
case *testpb.TestPing:
ctx.Tell(ctx.Sender(), new(testpb.TestPong))
}
}
func (x *A2) PostStop(ctx *actors.Context) error {
return nil
} and this is the test: func TestTestProbe(t *testing.T) {
// create a test context
ctx := context.TODO()
// create a test kit
testkit := New(ctx, t, WithLogging(log.ErrorLevel))
// create the A1
testkit.Spawn(ctx, "A1", &A1{})
// create the test probe
probe := testkit.NewProbe(ctx)
// send a message to the actor to be tested
probe.Send("A1", new(testpb.TestPing))
// A1 receives the message and spawns A2 then we expect a message TestPong and no more messages after that
msg := new(testpb.TestPong)
probe.ExpectMessage(msg)
probe.ExpectNoMessage()
// Assert that A1 send a message to A2 we just need to check that A2 can receive a message
// send a message to the actor to be tested
probe.Send("A2", new(testpb.TestPing))
msg = new(testpb.TestPong)
probe.ExpectMessage(msg)
probe.ExpectNoMessage()
probe.Stop()
testkit.Shutdown(ctx)
} |
Beta Was this translation helpful? Give feedback.
-
@d1r7y closing this discussion for lack of activities. Feel free to bring it up whenever necessary. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a pipeline model where actors, depending on their input messages, will spawn subsequent child actor(s) and send them messages.
I would like to be able to validate that, given a specific message, the actor under test is spawning the correct child actor(s) and sending them appropriate messages.
To be specific:
I want to test actor A1. In response to receiving message M1, A1 will spawn A2 and send it message M2. I would like to be able to write some unit tests which send different messages to A1 and validate the appropriate behavior.
I've looked at testkit and it seems good if I want to validate the response for a message sent to an actor.
Do you have any ideas on how to test my requirements above? My current thinking is to hook into the event stream to know when A2 is created, but I don't know how to inspect M2 sent by A1 to A2...
Beta Was this translation helpful? Give feedback.
All reactions