Skip to content

Commit

Permalink
Remove generics from shuffler
Browse files Browse the repository at this point in the history
  • Loading branch information
ferglor committed Apr 5, 2024
1 parent 768c2d1 commit ae7093f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pkg/v3/flows/conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ func NewSampler(
logger: logger,
getter: getter,
ratio: ratio,
shuffler: random.Shuffler[common.UpkeepPayload]{Source: random.NewCryptoRandSource()},
shuffler: random.Shuffler{Source: random.NewCryptoRandSource()},
}
}

type shuffler[T any] interface {
Shuffle([]T) []T
type shuffler interface {
Shuffle([]common.UpkeepPayload) []common.UpkeepPayload
}

type sampler struct {
logger *log.Logger

ratio types.Ratio
getter common.ConditionalUpkeepProvider
shuffler shuffler[common.UpkeepPayload]
shuffler shuffler
}

func (s *sampler) Value(ctx context.Context) ([]common.UpkeepPayload, error) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/v3/random/shuffler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package random

import "math/rand"
import (
"math/rand"

type Shuffler[T any] struct {
common "github.com/smartcontractkit/chainlink-common/pkg/types/automation"
)

type Shuffler struct {
Source rand.Source
}

func (s Shuffler[T]) Shuffle(a []T) []T {
func (s Shuffler) Shuffle(a []common.UpkeepPayload) []common.UpkeepPayload {
r := rand.New(s.Source)
r.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
Expand Down
54 changes: 46 additions & 8 deletions pkg/v3/random/shuffler_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
package random

import (
"github.com/smartcontractkit/chainlink-common/pkg/types/automation"
"math/rand"
"testing"

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

func TestShuffler_Shuffle(t *testing.T) {
shuffler := Shuffler[int]{Source: rand.NewSource(0)}
arr := []int{1, 2, 3, 4, 5}
shuffler := Shuffler{Source: rand.NewSource(0)}
arr := []automation.UpkeepPayload{
{WorkID: "1"},
{WorkID: "2"},
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "5"},
}
arr = shuffler.Shuffle(arr)
assert.Equal(t, arr, []int{3, 4, 2, 1, 5})
assert.Equal(t, arr, []automation.UpkeepPayload{
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "2"},
{WorkID: "1"},
{WorkID: "5"}},
)

// Sorting again using a used shuffler should yield a different result
arr = []int{1, 2, 3, 4, 5}
arr = []automation.UpkeepPayload{
{WorkID: "1"},
{WorkID: "2"},
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "5"},
}
arr = shuffler.Shuffle(arr)
assert.Equal(t, arr, []int{3, 4, 1, 5, 2})
assert.Equal(t, arr, []automation.UpkeepPayload{
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "1"},
{WorkID: "5"},
{WorkID: "2"}},
)

// Sorting again using a new shuffler with the same pseudo-random source should yield the same result
shuffler2 := Shuffler[int]{Source: rand.NewSource(0)}
arr2 := []int{1, 2, 3, 4, 5}
shuffler2 := Shuffler{Source: rand.NewSource(0)}
arr2 := []automation.UpkeepPayload{
{WorkID: "1"},
{WorkID: "2"},
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "5"},
}

arr2 = shuffler2.Shuffle(arr2)
assert.Equal(t, arr2, []int{3, 4, 2, 1, 5})
assert.Equal(t, arr2, []automation.UpkeepPayload{
{WorkID: "3"},
{WorkID: "4"},
{WorkID: "2"},
{WorkID: "1"},
{WorkID: "5"}},
)
}

func TestShuffler_ShuffleString(t *testing.T) {
Expand Down

0 comments on commit ae7093f

Please sign in to comment.