2019-09-05 16:40:52 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-07-13 23:24:15 +01:00
|
|
|
package audit
|
2019-09-05 16:40:52 +01:00
|
|
|
|
|
|
|
import (
|
2020-07-13 23:24:15 +01:00
|
|
|
"context"
|
2019-09-05 16:40:52 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-13 23:24:15 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2020-07-13 23:24:15 +01:00
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/testcontext"
|
2020-12-14 12:54:22 +00:00
|
|
|
"storj.io/common/testrand"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-09-05 16:40:52 +01:00
|
|
|
)
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
func TestQueues(t *testing.T) {
|
2020-07-13 23:24:15 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
queues := NewQueues()
|
|
|
|
q := queues.Fetch()
|
2019-09-05 16:40:52 +01:00
|
|
|
|
|
|
|
_, err := q.Next()
|
2020-07-13 23:24:15 +01:00
|
|
|
require.True(t, ErrEmptyQueue.Has(err), "required ErrEmptyQueue error")
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
testQueue1 := []Segment{testSegment(), testSegment(), testSegment()}
|
2020-08-20 14:29:02 +01:00
|
|
|
err = queues.Push(testQueue1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = queues.WaitForSwap(ctx)
|
2019-09-05 16:40:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
q = queues.Fetch()
|
2020-07-13 23:24:15 +01:00
|
|
|
for _, expected := range testQueue1 {
|
|
|
|
actual, err := q.Next()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Zero(t, q.Size())
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
func TestQueuesPush(t *testing.T) {
|
2020-07-13 23:24:15 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
queues := NewQueues()
|
|
|
|
// when next queue is empty, WaitForSwap should return immediately
|
2021-06-14 16:40:46 +01:00
|
|
|
testQueue1 := []Segment{testSegment(), testSegment(), testSegment()}
|
2020-08-20 14:29:02 +01:00
|
|
|
err := queues.Push(testQueue1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = queues.WaitForSwap(ctx)
|
2019-09-05 16:40:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
// second call to WaitForSwap should block until Fetch is called the first time
|
2021-06-14 16:40:46 +01:00
|
|
|
testQueue2 := []Segment{testSegment(), testSegment()}
|
2020-08-20 14:29:02 +01:00
|
|
|
err = queues.Push(testQueue2)
|
|
|
|
require.NoError(t, err)
|
2020-07-13 23:24:15 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
2020-08-20 14:29:02 +01:00
|
|
|
return queues.WaitForSwap(ctx)
|
2020-07-13 23:24:15 +01:00
|
|
|
})
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
q := queues.Fetch()
|
2020-07-13 23:24:15 +01:00
|
|
|
for _, expected := range testQueue1 {
|
|
|
|
actual, err := q.Next()
|
2019-09-05 16:40:52 +01:00
|
|
|
require.NoError(t, err)
|
2020-07-13 23:24:15 +01:00
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
// second call to Fetch should return testQueue2
|
|
|
|
q = queues.Fetch()
|
2020-07-13 23:24:15 +01:00
|
|
|
item, err := q.Next()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, testQueue2[0], item)
|
|
|
|
require.Equal(t, len(testQueue2)-1, q.Size())
|
|
|
|
|
|
|
|
err = group.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
func TestQueuesPushCancel(t *testing.T) {
|
2020-07-13 23:24:15 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
queues := NewQueues()
|
2020-07-13 23:24:15 +01:00
|
|
|
// when queue is empty, WaitForSwap should return immediately
|
2021-06-14 16:40:46 +01:00
|
|
|
testQueue1 := []Segment{testSegment(), testSegment(), testSegment()}
|
2020-08-20 14:29:02 +01:00
|
|
|
err := queues.Push(testQueue1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = queues.WaitForSwap(ctx)
|
2020-07-13 23:24:15 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctxWithCancel, cancel := context.WithCancel(ctx)
|
2021-06-14 16:40:46 +01:00
|
|
|
testQueue2 := []Segment{testSegment(), testSegment()}
|
2020-08-20 14:29:02 +01:00
|
|
|
err = queues.Push(testQueue2)
|
|
|
|
require.NoError(t, err)
|
2020-07-13 23:24:15 +01:00
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
2020-08-20 14:29:02 +01:00
|
|
|
err = queues.WaitForSwap(ctxWithCancel)
|
2020-07-13 23:24:15 +01:00
|
|
|
require.True(t, errs2.IsCanceled(err))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-08-20 14:29:02 +01:00
|
|
|
// make sure a concurrent call to Push fails
|
|
|
|
err = queues.Push(testQueue2)
|
|
|
|
require.True(t, ErrPendingQueueInProgress.Has(err))
|
2020-07-13 23:24:15 +01:00
|
|
|
|
|
|
|
cancel()
|
2019-09-05 16:40:52 +01:00
|
|
|
|
2020-07-13 23:24:15 +01:00
|
|
|
err = group.Wait()
|
|
|
|
require.NoError(t, err)
|
2019-09-05 16:40:52 +01:00
|
|
|
}
|
2020-12-14 12:54:22 +00:00
|
|
|
|
2021-06-14 16:40:46 +01:00
|
|
|
func testSegment() Segment {
|
2020-12-14 12:54:22 +00:00
|
|
|
return Segment{
|
|
|
|
StreamID: testrand.UUID(),
|
2021-06-14 16:40:46 +01:00
|
|
|
Position: metabase.SegmentPosition{
|
|
|
|
Index: uint32(testrand.Intn(100)),
|
|
|
|
},
|
2020-12-14 12:54:22 +00:00
|
|
|
}
|
|
|
|
}
|