Skip to content

Commit

Permalink
fix: fix deadletter count metric bug (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey authored Feb 23, 2025
1 parent d77d313 commit 0da9081
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
41 changes: 41 additions & 0 deletions actor/dead_letter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,47 @@ func TestDeadletter(t *testing.T) {
err = sys.Stop(ctx)
assert.NoError(t, err)
})
t.Run("With GetDeadlettersCount for a specific actor", func(t *testing.T) {
ctx := context.TODO()
sys, _ := NewActorSystem("testSys", WithLogger(log.DiscardLogger))

// start the actor system
err := sys.Start(ctx)
assert.NoError(t, err)

// wait for complete start
util.Pause(time.Second)

// create the black hole actor
actor := &mockUnhandledMessageActor{}
actorName := "actorName"
actorRef, err := sys.Spawn(ctx, actorName, actor)
assert.NoError(t, err)
assert.NotNil(t, actorRef)

// wait a while
util.Pause(time.Second)

// every message sent to the actor will result in deadletter
for i := 0; i < 5; i++ {
require.NoError(t, Tell(ctx, actorRef, new(testpb.TestSend)))
}

util.Pause(time.Second)

actorID := actorRef.ID()
reply, err := Ask(ctx, sys.getDeadletter(), &internalpb.GetDeadlettersCount{
ActorId: &actorID,
}, 500*time.Millisecond)
require.NoError(t, err)
require.NotNil(t, reply)
response, ok := reply.(*internalpb.DeadlettersCount)
require.True(t, ok)
require.EqualValues(t, 5, response.GetTotalCount())

err = sys.Stop(ctx)
assert.NoError(t, err)
})
t.Run("With GetDeadletters", func(t *testing.T) {
ctx := context.TODO()
sys, _ := NewActorSystem("testSys", WithLogger(log.DiscardLogger))
Expand Down
2 changes: 1 addition & 1 deletion actor/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,7 @@ func (pid *PID) suspend(reason string) {
// getDeadlettersCount gets deadletter
func (pid *PID) getDeadlettersCount(ctx context.Context) int64 {
var (
name = pid.Name()
name = pid.ID()
to = pid.ActorSystem().getDeadletter()
from = pid.ActorSystem().getSystemGuardian()
message = &internalpb.GetDeadlettersCount{
Expand Down
36 changes: 36 additions & 0 deletions actor/pid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3709,3 +3709,39 @@ func TestLogger(t *testing.T) {
buffer.Reset()
})
}
func TestDeadletterCountMetric(t *testing.T) {
ctx := context.TODO()
sys, _ := NewActorSystem("testSys", WithLogger(log.DiscardLogger))

// start the actor system
err := sys.Start(ctx)
assert.NoError(t, err)

// wait for complete start
util.Pause(time.Second)

// create the black hole actor
actor := &mockUnhandledMessageActor{}
actorName := "actorName"
actorRef, err := sys.Spawn(ctx, actorName, actor)
assert.NoError(t, err)
assert.NotNil(t, actorRef)

// wait a while
util.Pause(time.Second)

// every message sent to the actor will result in deadletter
for i := 0; i < 5; i++ {
require.NoError(t, Tell(ctx, actorRef, new(testpb.TestSend)))
}

util.Pause(time.Second)

metric := actorRef.Metric(ctx)
require.NotNil(t, metric)

require.EqualValues(t, 5, metric.DeadlettersCount())

err = sys.Stop(ctx)
assert.NoError(t, err)
}

0 comments on commit 0da9081

Please sign in to comment.