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"
|
2020-07-13 23:24:15 +01:00
|
|
|
"time"
|
2019-09-05 16:40:52 +01:00
|
|
|
|
|
|
|
"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"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2020-07-13 23:24:15 +01:00
|
|
|
"storj.io/common/testcontext"
|
2019-09-05 16:40:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueue(t *testing.T) {
|
2020-07-13 23:24:15 +01:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
q := &Queue{}
|
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
|
|
|
|
|
|
|
testQueue1 := []storj.Path{"a", "b", "c"}
|
2020-07-13 23:24:15 +01:00
|
|
|
err = q.WaitForSwap(ctx, testQueue1)
|
2019-09-05 16:40:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueueWaitForSwap(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
q := &Queue{}
|
|
|
|
// when queue is empty, WaitForSwap should return immediately
|
|
|
|
testQueue1 := []storj.Path{"a", "b", "c"}
|
|
|
|
err := q.WaitForSwap(ctx, testQueue1)
|
2019-09-05 16:40:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-13 23:24:15 +01:00
|
|
|
testQueue2 := []storj.Path{"d", "e"}
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
return q.WaitForSwap(ctx, testQueue2)
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for WaitForSwap to set onEmpty callback so we can test that consuming the queue frees it.
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
for range ticker.C {
|
|
|
|
q.mu.Lock()
|
|
|
|
if q.onEmpty != nil {
|
|
|
|
q.mu.Unlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
q.mu.Unlock()
|
|
|
|
}
|
|
|
|
ticker.Stop()
|
2019-09-05 16:40:52 +01:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// next call to Next() should swap queues and free WaitForSwap
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueueWaitForSwapCancel(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
q := &Queue{}
|
|
|
|
// when queue is empty, WaitForSwap should return immediately
|
|
|
|
testQueue1 := []storj.Path{"a", "b", "c"}
|
|
|
|
err := q.WaitForSwap(ctx, testQueue1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctxWithCancel, cancel := context.WithCancel(ctx)
|
|
|
|
testQueue2 := []storj.Path{"d", "e"}
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
err = q.WaitForSwap(ctxWithCancel, testQueue2)
|
|
|
|
require.True(t, errs2.IsCanceled(err))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for WaitForSwap to set onEmpty callback so we can test that canceling the context frees it.
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
for range ticker.C {
|
|
|
|
q.mu.Lock()
|
|
|
|
if q.onEmpty != nil {
|
|
|
|
q.mu.Unlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
q.mu.Unlock()
|
2019-09-05 16:40:52 +01:00
|
|
|
}
|
2020-07-13 23:24:15 +01:00
|
|
|
ticker.Stop()
|
|
|
|
|
|
|
|
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
|
|
|
}
|