Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,23 @@ func (p *roundRobin) Pick(
c.lastSelected = nil
return nil, nil //nolint:nilnil
}

// roundRobinState is the JSON payload returned by DumpState.
type roundRobinState struct {
Plugin string `json:"plugin"`
Type string `json:"type"`
}

// DumpState implements [fwkplugin.StateDumper].
// The round-robin cursor is stored per priority band on the band's PolicyState,
// not on the plugin itself, so there is no centralized mutable state to snapshot.
// This dump returns plugin identity information only.
func (p *roundRobin) DumpState() (json.RawMessage, error) {
return json.Marshal(roundRobinState{
Plugin: p.name,
Type: RoundRobinFairnessPolicyType,
})
}

// Ensure roundRobin implements StateDumper.
var _ fwkplugin.StateDumper = &roundRobin{}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package roundrobin

import (
"context"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -282,3 +283,44 @@ func TestRoundRobin_Pick_Concurrency(t *testing.T) {
})
}
}

func TestRoundRobin_DumpState(t *testing.T) {
t.Parallel()

t.Run("default name", func(t *testing.T) {
t.Parallel()
policy := newRoundRobin("")

payload, err := policy.DumpState()
require.NoError(t, err)
require.NotNil(t, payload)

var state roundRobinState
require.NoError(t, json.Unmarshal(payload, &state))
require.Equal(t, RoundRobinFairnessPolicyType, state.Plugin)
require.Equal(t, RoundRobinFairnessPolicyType, state.Type)
})

t.Run("custom name", func(t *testing.T) {
t.Parallel()
policy := newRoundRobin("my-rr-policy")

payload, err := policy.DumpState()
require.NoError(t, err)
require.NotNil(t, payload)

var state roundRobinState
require.NoError(t, json.Unmarshal(payload, &state))
require.Equal(t, "my-rr-policy", state.Plugin)
require.Equal(t, RoundRobinFairnessPolicyType, state.Type)
})

t.Run("valid json output", func(t *testing.T) {
t.Parallel()
policy := newRoundRobin("test")

payload, err := policy.DumpState()
require.NoError(t, err)
require.True(t, json.Valid(payload), "DumpState output should be valid JSON")
})
}
Loading