Skip to content

Commit

Permalink
Refactor sort function to use external sort value fetcher
Browse files Browse the repository at this point in the history
Updated the Sort function to accept a SortValueFetcher interface and changed its implementations accordingly. This change improves flexibility and decouples sort value fetching logic from the client type. Also added zap logger dependency to NewClients initialization in tests.
  • Loading branch information
billettc committed Nov 26, 2024
1 parent 76469b5 commit c5f782f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions rpc/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func NewClients[C any](maxBlockFetchDuration time.Duration, rollingStrategy Roll
}
}

func (c *Clients[C]) StartSorting(ctx context.Context, direction SortDirection, every time.Duration) {
func (c *Clients[C]) StartSorting(ctx context.Context, direction SortDirection, sortValueFetcher SortValueFetcher[C], every time.Duration) {
go func() {
for {
c.logger.Info("sorting clients")
err := Sort(ctx, c, direction)
err := Sort(ctx, c, sortValueFetcher, direction)
if err != nil {
c.logger.Warn("sorting", zap.Error(err))
}
Expand Down
4 changes: 3 additions & 1 deletion rpc/rolling_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"go.uber.org/zap"

"github.com/stretchr/testify/require"
)

Expand All @@ -24,7 +26,7 @@ func TestStickyRollingStrategy(t *testing.T) {
rollingStrategy := NewStickyRollingStrategy[*rollClient]()
rollingStrategy.reset()

clients := NewClients(2*time.Second, rollingStrategy)
clients := NewClients(2*time.Second, rollingStrategy, zap.NewNop())
clients.Add(&rollClient{name: "c.1"})
clients.Add(&rollClient{name: "c.2"})
clients.Add(&rollClient{name: "c.3"})
Expand Down
14 changes: 6 additions & 8 deletions rpc/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"sort"
)

type SortValueFetcher interface {
fetchSortValue(ctx context.Context) (sortValue uint64, err error)
type SortValueFetcher[C any] interface {
fetchSortValue(ctx context.Context, client C) (sortValue uint64, err error)
}

type SortDirection int
Expand All @@ -16,7 +16,7 @@ const (
SortDirectionDescending
)

func Sort[C any](ctx context.Context, clients *Clients[C], direction SortDirection) error {
func Sort[C any](ctx context.Context, clients *Clients[C], sortValueFetch SortValueFetcher[C], direction SortDirection) error {
type sortable struct {
clientIndex int
sortValue uint64
Expand All @@ -25,11 +25,9 @@ func Sort[C any](ctx context.Context, clients *Clients[C], direction SortDirecti
for i, client := range clients.clients {
var v uint64
var err error
if s, ok := any(client).(SortValueFetcher); ok {
v, err = s.fetchSortValue(ctx)
if err != nil {
//do nothing
}
v, err = sortValueFetch.fetchSortValue(ctx, client)
if err != nil {
//do nothing
}
sortableValues = append(sortableValues, sortable{i, v})
}
Expand Down
17 changes: 14 additions & 3 deletions rpc/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@ import (
"testing"
"time"

"go.uber.org/zap"

"github.com/stretchr/testify/require"
)

type testSortFetcher struct {
}

func (t testSortFetcher) fetchSortValue(ctx context.Context, client *rollClient) (sortValue uint64, err error) {
return client.sortValue, nil
}

func TestClientsSort(t *testing.T) {
rollingStrategy := NewStickyRollingStrategy[*rollClient]()
rollingStrategy.reset()

clients := NewClients(2*time.Second, rollingStrategy)
clients := NewClients(2*time.Second, rollingStrategy, zap.NewNop())
clients.Add(&rollClient{name: "c.1", sortValue: 100})
clients.Add(&rollClient{name: "c.2", sortValue: 101})
clients.Add(&rollClient{name: "c.3", sortValue: 102})
clients.Add(&rollClient{name: "c.a", sortValue: 103})
clients.Add(&rollClient{name: "c.b", sortValue: 104})

err := Sort(context.Background(), clients, SortDirectionDescending)
fetcher := testSortFetcher{}

err := Sort(context.Background(), clients, fetcher, SortDirectionDescending)
require.NoError(t, err)

var names []string
Expand All @@ -29,7 +40,7 @@ func TestClientsSort(t *testing.T) {

require.Equal(t, []string{"c.b", "c.a", "c.3", "c.2", "c.1"}, names)

err = Sort(context.Background(), clients, SortDirectionAscending)
err = Sort(context.Background(), clients, fetcher, SortDirectionAscending)
require.NoError(t, err)

names = []string{}
Expand Down

0 comments on commit c5f782f

Please sign in to comment.