(t *testing.T)
| 172 | } |
| 173 | |
| 174 | func TestPartitionRing_ShuffleShard(t *testing.T) { |
| 175 | t.Run("should honor the shard size", func(t *testing.T) { |
| 176 | const numActivePartitions = 5 |
| 177 | |
| 178 | opts := DefaultPartitionRingOptions() |
| 179 | // Set a size so we can assert the options are preserved on update. |
| 180 | opts.ShuffleShardCacheSize = 1 |
| 181 | ring := createPartitionRingWithPartitions(opts, numActivePartitions, 0, 0) |
| 182 | |
| 183 | // Request a shard size up to the number of existing partitions. |
| 184 | for shardSize := 1; shardSize <= numActivePartitions; shardSize++ { |
| 185 | subring, err := ring.ShuffleShard("tenant-id", shardSize) |
| 186 | require.NoError(t, err) |
| 187 | assert.Equal(t, shardSize, subring.PartitionsCount()) |
| 188 | assert.Equal(t, subring.PartitionsCount(), ring.ShuffleShardSize(shardSize)) |
| 189 | } |
| 190 | |
| 191 | // Request a shard size greater than the number of existing partitions. |
| 192 | subring, err := ring.ShuffleShard("tenant-id", numActivePartitions+1) |
| 193 | require.NoError(t, err) |
| 194 | assert.Equal(t, numActivePartitions, subring.PartitionsCount()) |
| 195 | assert.Equal(t, subring.PartitionsCount(), ring.ShuffleShardSize(numActivePartitions+1)) |
| 196 | assert.Equal(t, opts, subring.opts) |
| 197 | }) |
| 198 | |
| 199 | t.Run("should never return INACTIVE or PENDING partitions", func(t *testing.T) { |
| 200 | const ( |
| 201 | numActivePartitions = 5 |
| 202 | numInactivePartitions = 5 |
| 203 | numPendingPartitions = 5 |
| 204 | ) |
| 205 | |
| 206 | ring := createPartitionRingWithPartitions(DefaultPartitionRingOptions(), numActivePartitions, numInactivePartitions, numPendingPartitions) |
| 207 | |
| 208 | // We test negative values of shardSize as well as sizes above current number of partition count. |
| 209 | for shardSize := -5; shardSize <= ring.PartitionsCount()+5; shardSize++ { |
| 210 | subring, err := ring.ShuffleShard("tenant-id", shardSize) |
| 211 | require.NoError(t, err) |
| 212 | |
| 213 | for _, partition := range subring.Partitions() { |
| 214 | assert.Equal(t, PartitionActive, partition.State) |
| 215 | } |
| 216 | assert.Equal(t, subring.PartitionsCount(), ring.ShuffleShardSize(shardSize)) |
| 217 | } |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | // This test asserts on shard stability across multiple invocations and given the same input ring. |
| 222 | func TestPartitionRing_ShuffleShard_Stability(t *testing.T) { |
nothing calls this directly
no test coverage detected