|
| 1 | +package aggregators |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "google.golang.org/protobuf/proto" |
| 8 | + |
| 9 | + "github.com/smartcontractkit/chainlink-protos/cre/go/values" |
| 10 | + "github.com/smartcontractkit/chainlink-protos/cre/go/values/pb" |
| 11 | +) |
| 12 | + |
| 13 | +// TestMode_twoWayTieIsDeterministicAcrossRepeatedCalls targets a bimodal tie: two |
| 14 | +// distinct values each appear the same number of times (here, 3× "tie-a" and 3× "tie-b"). |
| 15 | +// The winner must not depend on map iteration order, so repeated calls with the same |
| 16 | +// multiset must return an observation equal under protobuf semantics every time. |
| 17 | +func TestMode_twoWayTieIsDeterministicAcrossRepeatedCalls(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + a, err := values.Wrap("tie-a") |
| 21 | + require.NoError(t, err) |
| 22 | + b, err := values.Wrap("tie-b") |
| 23 | + require.NoError(t, err) |
| 24 | + items := []values.Value{a, a, a, b, b, b} |
| 25 | + |
| 26 | + var baseline *pb.Value |
| 27 | + for i := range 400 { |
| 28 | + got, maxCount, err := mode(items) |
| 29 | + require.NoError(t, err) |
| 30 | + require.Equal(t, 3, maxCount) |
| 31 | + |
| 32 | + gotProto := values.Proto(got) |
| 33 | + if i == 0 { |
| 34 | + baseline = proto.Clone(gotProto).(*pb.Value) |
| 35 | + continue |
| 36 | + } |
| 37 | + require.True(t, proto.Equal(baseline, gotProto), |
| 38 | + "iteration %d: mode() must pick the same tied winner on every call", i) |
| 39 | + } |
| 40 | +} |
0 commit comments