Skip to content

Commit

Permalink
test: add case for LeaderChangeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
yannan committed May 13, 2024
1 parent 369e880 commit ab356e3
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions fastflow_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fastflow

import (
"context"
"fmt"
"testing"
"time"

"github.com/shiningrush/fastflow/pkg/entity"
"github.com/shiningrush/fastflow/pkg/event"
"github.com/shiningrush/fastflow/pkg/mod"
"github.com/shiningrush/fastflow/pkg/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -190,3 +192,48 @@ tasks:
})
}
}

func Test_LeaderChangeHandler(t *testing.T) {
tests := []struct {
isLeader bool
calledEnsured []bool
closerLenth int
}{
{
isLeader: true,
calledEnsured: []bool{},
closerLenth: 1,
},
{
isLeader: false,
calledEnsured: []bool{true},
closerLenth: 0,
},
}

for _, tc := range tests {
called := []bool{}
keeper := &mod.MockKeeper{}
handler := &LeaderChangedHandler{
opt: &InitialOption{
Keeper: keeper,
},
leaderCloser: []mod.Closer{},
}

closer := &mod.MockCloser{}
closer.On("Close").Run(func(args mock.Arguments) {
called = append(called, true)
}).Return(nil)
handler.leaderCloser = append(handler.leaderCloser, closer)

mockEvent := &event.LeaderChanged{IsLeader: tc.isLeader, WorkerKey: "worker-key"}
ctx := context.Background()

// Simulate the event that leads to leadership
handler.Handle(ctx, mockEvent)

assert.Equal(t, tc.calledEnsured, called)
assert.Equal(t, tc.closerLenth, len(handler.leaderCloser))
}
}

0 comments on commit ab356e3

Please sign in to comment.