|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + "math" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.temporal.io/sdk/log" |
| 10 | + |
| 11 | + "github.com/PeerDB-io/peerdb/flow/generated/protos" |
| 12 | +) |
| 13 | + |
| 14 | +func intRangeOf(t *testing.T, partition *protos.QRepPartition) *protos.IntPartitionRange { |
| 15 | + t.Helper() |
| 16 | + r, ok := partition.Range.Range.(*protos.PartitionRange_IntRange) |
| 17 | + require.True(t, ok) |
| 18 | + return r.IntRange |
| 19 | +} |
| 20 | + |
| 21 | +func uintRangeOf(t *testing.T, partition *protos.QRepPartition) *protos.UIntPartitionRange { |
| 22 | + t.Helper() |
| 23 | + r, ok := partition.Range.Range.(*protos.PartitionRange_UintRange) |
| 24 | + require.True(t, ok) |
| 25 | + return r.UintRange |
| 26 | +} |
| 27 | + |
| 28 | +func assertContiguousRanges[T int64 | uint64](t *testing.T, ranges [][2]T, minVal, maxVal T) { |
| 29 | + t.Helper() |
| 30 | + require.NotEmpty(t, ranges) |
| 31 | + require.Equal(t, minVal, ranges[0][0]) |
| 32 | + require.Equal(t, maxVal, ranges[len(ranges)-1][1]) |
| 33 | + for i := range ranges { |
| 34 | + require.Less(t, ranges[i][0], ranges[i][1]) |
| 35 | + if i+1 < len(ranges) { |
| 36 | + require.Equal(t, ranges[i][1], ranges[i+1][0]) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestComputeRanges(t *testing.T) { |
| 42 | + t.Run("even division", func(t *testing.T) { |
| 43 | + ranges := ComputeRanges[int64](1, 100, 10) |
| 44 | + require.Len(t, ranges, 10) |
| 45 | + assertContiguousRanges(t, ranges, int64(1), int64(100)) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("span smaller than partition count", func(t *testing.T) { |
| 49 | + ranges := ComputeRanges[int64](0, 5, 10) |
| 50 | + require.Len(t, ranges, 5) |
| 51 | + assertContiguousRanges(t, ranges, int64(0), int64(5)) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("two adjacent values", func(t *testing.T) { |
| 55 | + ranges := ComputeRanges[int64](7, 8, 4) |
| 56 | + require.Len(t, ranges, 1) |
| 57 | + assertContiguousRanges(t, ranges, int64(7), int64(8)) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("full int64 domain does not overflow", func(t *testing.T) { |
| 61 | + ranges := ComputeRanges[int64](math.MinInt64, math.MaxInt64, 7) |
| 62 | + require.Len(t, ranges, 7) |
| 63 | + assertContiguousRanges(t, ranges, int64(math.MinInt64), int64(math.MaxInt64)) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("full uint64 domain does not overflow", func(t *testing.T) { |
| 67 | + ranges := ComputeRanges[uint64](0, math.MaxUint64, 7) |
| 68 | + require.Len(t, ranges, 7) |
| 69 | + assertContiguousRanges(t, ranges, uint64(0), uint64(math.MaxUint64)) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("same min/max values", func(t *testing.T) { |
| 73 | + ranges := ComputeRanges[int64](7, 7, 4) |
| 74 | + require.Len(t, ranges, 1) |
| 75 | + require.Equal(t, int64(7), ranges[0][0]) |
| 76 | + require.Equal(t, int64(7), ranges[len(ranges)-1][1]) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func TestAddPartitionsWithRange(t *testing.T) { |
| 81 | + logger := log.NewStructuredLogger(slog.Default()) |
| 82 | + |
| 83 | + t.Run("negative to positive int range covers domain contiguously", func(t *testing.T) { |
| 84 | + helper := NewPartitionHelper(logger) |
| 85 | + require.NoError(t, helper.AddPartitionsWithRange(int64(-100), int64(100), 4)) |
| 86 | + |
| 87 | + partitions := helper.GetPartitions() |
| 88 | + require.Len(t, partitions, 4) |
| 89 | + require.Equal(t, int64(-100), intRangeOf(t, partitions[0]).Start) |
| 90 | + require.Equal(t, int64(100), intRangeOf(t, partitions[len(partitions)-1]).End) |
| 91 | + for i := 1; i < len(partitions); i++ { |
| 92 | + require.Equal(t, intRangeOf(t, partitions[i-1]).End+1, intRangeOf(t, partitions[i]).Start) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("int range spanning full int64 domain does not overflow", func(t *testing.T) { |
| 97 | + helper := NewPartitionHelper(logger) |
| 98 | + require.NoError(t, helper.AddPartitionsWithRange(int64(math.MinInt64), int64(math.MaxInt64), 7)) |
| 99 | + |
| 100 | + partitions := helper.GetPartitions() |
| 101 | + require.Len(t, partitions, 7) |
| 102 | + require.Equal(t, int64(math.MinInt64), intRangeOf(t, partitions[0]).Start) |
| 103 | + require.Equal(t, int64(math.MaxInt64), intRangeOf(t, partitions[len(partitions)-1]).End) |
| 104 | + for i, partition := range partitions { |
| 105 | + r := intRangeOf(t, partition) |
| 106 | + require.LessOrEqual(t, r.Start, r.End) |
| 107 | + if i > 0 { |
| 108 | + require.Equal(t, intRangeOf(t, partitions[i-1]).End+1, r.Start) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("uint range spanning full uint64 domain does not overflow", func(t *testing.T) { |
| 114 | + helper := NewPartitionHelper(logger) |
| 115 | + require.NoError(t, helper.AddPartitionsWithRange(uint64(0), uint64(math.MaxUint64), 7)) |
| 116 | + |
| 117 | + partitions := helper.GetPartitions() |
| 118 | + require.Len(t, partitions, 7) |
| 119 | + require.Equal(t, uint64(0), uintRangeOf(t, partitions[0]).Start) |
| 120 | + require.Equal(t, uint64(math.MaxUint64), uintRangeOf(t, partitions[len(partitions)-1]).End) |
| 121 | + for i, partition := range partitions { |
| 122 | + r := uintRangeOf(t, partition) |
| 123 | + require.LessOrEqual(t, r.Start, r.End) |
| 124 | + if i > 0 { |
| 125 | + require.Equal(t, uintRangeOf(t, partitions[i-1]).End+1, r.Start) |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("span smaller than partition count yields fewer partitions", func(t *testing.T) { |
| 131 | + helper := NewPartitionHelper(logger) |
| 132 | + require.NoError(t, helper.AddPartitionsWithRange(int64(0), int64(5), 10)) |
| 133 | + |
| 134 | + partitions := helper.GetPartitions() |
| 135 | + require.Len(t, partitions, 5) |
| 136 | + require.Equal(t, int64(5), intRangeOf(t, partitions[len(partitions)-1]).End) |
| 137 | + }) |
| 138 | +} |
0 commit comments