(t *testing.T)
| 1211 | } |
| 1212 | |
| 1213 | func TestActivePartitionBatchRing_Get(t *testing.T) { |
| 1214 | const numRuns = 1000 |
| 1215 | |
| 1216 | ring := createPartitionRingWithPartitions(DefaultPartitionRingOptions(), 10, 5, 5) |
| 1217 | activeRing := NewActivePartitionBatchRing(ring) |
| 1218 | buf := [GetBufferSize]InstanceDesc{} |
| 1219 | |
| 1220 | t.Run("should return a ReplicationSet with the active partition owning the input key", func(t *testing.T) { |
| 1221 | for _, withBuffer := range []bool{true, false} { |
| 1222 | t.Run(fmt.Sprintf("buffer: %t", withBuffer), func(t *testing.T) { |
| 1223 | // Randomise the seed but log it in case we need to reproduce the test on failure. |
| 1224 | seed := time.Now().UnixNano() |
| 1225 | rnd := rand.New(rand.NewSource(seed)) |
| 1226 | t.Log("random generator seed:", seed) |
| 1227 | |
| 1228 | var getBuf []InstanceDesc |
| 1229 | if withBuffer { |
| 1230 | getBuf = buf[:0] |
| 1231 | } |
| 1232 | |
| 1233 | for i := 0; i < numRuns; i++ { |
| 1234 | key := uint32(rnd.Intn(math.MaxUint32)) |
| 1235 | expected, err := ring.ActivePartitionForKey(key) |
| 1236 | require.NoError(t, err) |
| 1237 | |
| 1238 | actual, err := activeRing.Get(key, WriteNoExtend, getBuf, nil, nil) |
| 1239 | require.NoError(t, err) |
| 1240 | |
| 1241 | require.Len(t, actual.Instances, 1) |
| 1242 | assert.Equal(t, strconv.Itoa(int(expected)), actual.Instances[0].Id) |
| 1243 | assert.Equal(t, strconv.Itoa(int(expected)), actual.Instances[0].Addr) |
| 1244 | assert.Equal(t, 0, actual.MaxErrors) |
| 1245 | assert.Equal(t, 0, actual.MaxUnavailableZones) |
| 1246 | assert.False(t, actual.ZoneAwarenessEnabled) |
| 1247 | } |
| 1248 | }) |
| 1249 | } |
| 1250 | }) |
| 1251 | |
| 1252 | t.Run("should not allocate memory with buffer is provided", func(t *testing.T) { |
| 1253 | allocs := testing.AllocsPerRun(numRuns, func() { |
| 1254 | _, err := activeRing.Get(12345, WriteNoExtend, buf[:0], nil, nil) |
| 1255 | if err != nil { |
| 1256 | t.Fatal(err) |
| 1257 | } |
| 1258 | }) |
| 1259 | |
| 1260 | require.Equal(t, 0.0, allocs) |
| 1261 | }) |
| 1262 | } |
| 1263 | |
| 1264 | func BenchmarkActivePartitionBatchRing_Get(b *testing.B) { |
| 1265 | benchCases := map[string]struct { |
nothing calls this directly
no test coverage detected