Skip to content

Commit

Permalink
refactor: add some test to testkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Feb 3, 2024
1 parent f932be1 commit cb01a9f
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 14 deletions.
4 changes: 4 additions & 0 deletions protos/test/pb/v1/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ message TestUnstashAll {}

message Ping {}
message Pong {}

message Wait {
uint64 duration = 1;
}
88 changes: 75 additions & 13 deletions test/data/pb/v1/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion testkit/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package testkit

import (
"context"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/tochemey/goakt/actors"
Expand Down Expand Up @@ -104,6 +106,57 @@ func TestTestProbe(t *testing.T) {
probe.ExpectMessageOfType(msg.ProtoReflect().Type())
probe.ExpectNoMessage()

t.Cleanup(func() {
probe.Stop()
testkit.Shutdown(ctx)
})
})
t.Run("Assert message received within a time period", func(t *testing.T) {
// create a test context
ctx := context.TODO()
// create a test kit
testkit := New(ctx, t)

// create the actor
pinger := testkit.Spawn(ctx, "pinger", &pinger{})
// create the test probe
probe := testkit.NewProbe(ctx)

// create the message to expect
msg := new(testpb.Pong)
// send a message to the actor to be tested
duration := time.Second
probe.Send(pinger, &testpb.Wait{Duration: uint64(duration)})

actual := probe.ExpectMessageWithin(2*time.Second, msg)
require.Equal(t, prototext.Format(msg), prototext.Format(actual))
probe.ExpectNoMessage()

t.Cleanup(func() {
probe.Stop()
testkit.Shutdown(ctx)
})
})
t.Run("Assert message type within a time period", func(t *testing.T) {
// create a test context
ctx := context.TODO()
// create a test kit
testkit := New(ctx, t)

// create the actor
pinger := testkit.Spawn(ctx, "pinger", &pinger{})
// create the test probe
probe := testkit.NewProbe(ctx)

// create the message to expect
msg := new(testpb.Pong)
// send a message to the actor to be tested
duration := time.Second
probe.Send(pinger, &testpb.Wait{Duration: uint64(duration)})

probe.ExpectMessageOfTypeWithin(2*time.Second, msg.ProtoReflect().Type())
probe.ExpectNoMessage()

t.Cleanup(func() {
probe.Stop()
testkit.Shutdown(ctx)
Expand All @@ -119,9 +172,21 @@ func (t pinger) PreStart(_ context.Context) error {
}

func (t pinger) Receive(ctx actors.ReceiveContext) {
switch ctx.Message().(type) {
switch x := ctx.Message().(type) {
case *testpb.Ping:
_ = ctx.Self().Tell(ctx.Context(), ctx.Sender(), new(testpb.Pong))
case *testpb.Wait:
// delay for a while before sending the reply
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
time.Sleep(time.Duration(x.Duration))
wg.Done()
}()
// block until timer is up
wg.Wait()
// reply the sender
_ = ctx.Self().Tell(ctx.Context(), ctx.Sender(), new(testpb.Pong))
}
}

Expand Down

0 comments on commit cb01a9f

Please sign in to comment.