2022-12-08 14:27:21 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rangedloop_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2022-12-21 20:58:08 +00:00
|
|
|
"time"
|
2022-12-08 14:27:21 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/uuid"
|
2022-12-08 14:27:21 +00:00
|
|
|
"storj.io/storj/satellite/metabase/rangedloop"
|
|
|
|
"storj.io/storj/satellite/metabase/rangedloop/rangedlooptest"
|
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
|
|
|
)
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
func TestLoopCount(t *testing.T) {
|
2022-12-08 14:27:21 +00:00
|
|
|
for _, parallelism := range []int{1, 2, 3} {
|
|
|
|
for _, nSegments := range []int{0, 1, 2, 11} {
|
|
|
|
for _, nObservers := range []int{0, 1, 2} {
|
|
|
|
t.Run(
|
|
|
|
fmt.Sprintf("par%d_seg%d_obs%d", parallelism, nSegments, nObservers),
|
|
|
|
func(t *testing.T) {
|
2022-12-21 20:58:08 +00:00
|
|
|
runCountTest(t, parallelism, nSegments, nObservers)
|
2022-12-08 14:27:21 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
func runCountTest(t *testing.T, parallelism int, nSegments int, nObservers int) {
|
2022-12-08 14:27:21 +00:00
|
|
|
batchSize := 2
|
2022-12-21 20:58:08 +00:00
|
|
|
ctx := testcontext.New(t)
|
2022-12-08 14:27:21 +00:00
|
|
|
|
|
|
|
observers := []rangedloop.Observer{}
|
|
|
|
for i := 0; i < nObservers; i++ {
|
|
|
|
observers = append(observers, &rangedlooptest.CountObserver{})
|
|
|
|
}
|
|
|
|
|
|
|
|
loopService := rangedloop.NewService(
|
|
|
|
zaptest.NewLogger(t),
|
|
|
|
rangedloop.Config{
|
2022-12-21 20:58:08 +00:00
|
|
|
BatchSize: batchSize,
|
|
|
|
Parallelism: parallelism,
|
2022-12-08 14:27:21 +00:00
|
|
|
},
|
2022-12-09 15:40:23 +00:00
|
|
|
&rangedlooptest.RangeSplitter{
|
2022-12-08 14:27:21 +00:00
|
|
|
Segments: make([]segmentloop.Segment, nSegments),
|
|
|
|
},
|
|
|
|
observers,
|
|
|
|
)
|
|
|
|
|
2022-12-21 20:58:08 +00:00
|
|
|
observerDurations, err := loopService.RunOnce(ctx)
|
2022-12-08 14:27:21 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-21 20:58:08 +00:00
|
|
|
require.Len(t, observerDurations, nObservers)
|
2022-12-08 14:27:21 +00:00
|
|
|
|
|
|
|
for _, observer := range observers {
|
|
|
|
countObserver := observer.(*rangedlooptest.CountObserver)
|
|
|
|
require.Equal(t, nSegments, countObserver.NumSegments)
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 20:58:08 +00:00
|
|
|
|
|
|
|
func TestLoopDuration(t *testing.T) {
|
|
|
|
t.Skip("Flaky test because it validates concurrency by measuring time")
|
|
|
|
|
|
|
|
nSegments := 8
|
|
|
|
nObservers := 2
|
|
|
|
parallelism := 4
|
|
|
|
batchSize := 2
|
|
|
|
sleepIncrement := time.Millisecond * 10
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
|
|
|
|
observers := []rangedloop.Observer{}
|
|
|
|
for i := 0; i < nObservers; i++ {
|
|
|
|
observers = append(observers, &rangedlooptest.SleepObserver{
|
|
|
|
Duration: sleepIncrement,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
segments := []segmentloop.Segment{}
|
|
|
|
for i := 0; i < nSegments; i++ {
|
|
|
|
streamId, err := uuid.FromBytes([]byte{byte(i), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
|
|
|
require.NoError(t, err)
|
|
|
|
segments = append(segments, segmentloop.Segment{
|
|
|
|
StreamID: streamId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
loopService := rangedloop.NewService(
|
|
|
|
zaptest.NewLogger(t),
|
|
|
|
rangedloop.Config{
|
|
|
|
BatchSize: batchSize,
|
|
|
|
Parallelism: parallelism,
|
|
|
|
},
|
|
|
|
&rangedlooptest.RangeSplitter{
|
|
|
|
Segments: segments,
|
|
|
|
},
|
|
|
|
observers,
|
|
|
|
)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
observerDurations, err := loopService.RunOnce(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
duration := time.Since(start)
|
|
|
|
expectedDuration := time.Duration(int64(nSegments) * int64(sleepIncrement) * int64(nObservers) / int64(parallelism))
|
|
|
|
require.Equal(t, expectedDuration, duration.Truncate(sleepIncrement))
|
|
|
|
|
|
|
|
require.Len(t, observerDurations, nObservers)
|
|
|
|
for _, observerDuration := range observerDurations {
|
|
|
|
expectedSleep := time.Duration(int64(nSegments) * int64(sleepIncrement))
|
|
|
|
require.Equal(t, expectedSleep, observerDuration.Duration.Round(sleepIncrement))
|
|
|
|
}
|
|
|
|
}
|