Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/leadership/elector.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func (e *Elector) Start(ctx context.Context) error {
// We'll send to this channel anytime a leader resigns on the key with `name`
e.leaderResignedChan = make(chan struct{})

e.requestResignChan = make(chan struct{})
// Buffered to 1 so a send from handleLeadershipNotification doesn't block
// if keepLeadershipLoop hasn't entered its select yet.
e.requestResignChan = make(chan struct{}, 1)

var sub *notifier.Subscription
if e.notifier == nil {
Expand Down
25 changes: 25 additions & 0 deletions internal/leadership/elector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,31 @@ func testElector[TElectorBundle any](
startstoptest.Stress(ctx, t, elector)
})

t.Run("RequestResignImmediatelyAfterElection", func(t *testing.T) {
t.Parallel()

elector, _ := setup(t, nil)

startElector(ctx, t, elector)

elector.testSignals.GainedLeadership.WaitOrTimeout()

// Send a resign request immediately after gaining leadership.
// GainedLeadership is signaled _before_ keepLeadershipLoop is
// entered, so the resign request arrives before the loop's
// select. This only works if requestResignChan is buffered;
// with an unbuffered channel the send would be dropped by the
// default case since nobody is receiving yet.
payload, err := json.Marshal(DBNotification{
Action: DBNotificationKindRequestResign,
})
require.NoError(t, err)
elector.handleLeadershipNotification(ctx, notifier.NotificationTopicLeadership, string(payload))

elector.testSignals.ResignedLeadership.WaitOrTimeout()
elector.testSignals.GainedLeadership.WaitOrTimeout()
})

t.Run("RequestResignStress", func(t *testing.T) {
t.Parallel()

Expand Down
Loading