2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-02 00:25:41 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-04-23 09:47:16 +01:00
|
|
|
package queue_test
|
2018-10-02 00:25:41 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-10-02 00:25:41 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2019-04-16 19:14:09 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-10-02 00:25:41 +01:00
|
|
|
)
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
func TestInsertSelect(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: "abc",
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
err := q.Insert(ctx, seg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
s, err := q.Select(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = q.Delete(ctx, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, pb.Equal(s, seg))
|
2018-12-21 15:11:19 +00:00
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:41:53 +01:00
|
|
|
func TestInsertDuplicate(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: "abc",
|
|
|
|
LostPieces: []int32{int32(1), int32(3)},
|
|
|
|
}
|
|
|
|
err := q.Insert(ctx, seg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = q.Insert(ctx, seg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-02 00:25:41 +01:00
|
|
|
func TestDequeueEmptyQueue(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
_, err := q.Select(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, storage.ErrEmptyQueue.Has(err), "error should of class EmptyQueue")
|
2018-12-21 15:11:19 +00:00
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequential(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-12-21 15:11:19 +00:00
|
|
|
|
|
|
|
q := db.RepairQueue()
|
|
|
|
|
|
|
|
const N = 100
|
|
|
|
var addSegs []*pb.InjuredSegment
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
seg := &pb.InjuredSegment{
|
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
err := q.Insert(ctx, seg)
|
|
|
|
require.NoError(t, err)
|
2018-12-21 15:11:19 +00:00
|
|
|
addSegs = append(addSegs, seg)
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
list, err := q.SelectN(ctx, N)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, list, N)
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
require.True(t, pb.Equal(addSegs[i], &list[i]))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
|
|
|
// TODO: fix out of order issue
|
2018-12-21 15:11:19 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = q.Delete(ctx, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := s.LostPieces[0]
|
|
|
|
require.True(t, pb.Equal(addSegs[expected], s))
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-02 00:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel(t *testing.T) {
|
2019-04-22 21:04:17 +01:00
|
|
|
t.Skip("flaky")
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
q := db.RepairQueue()
|
|
|
|
const N = 100
|
|
|
|
errs := make(chan error, N*2)
|
2019-04-16 19:14:09 +01:00
|
|
|
entries := make(chan *pb.InjuredSegment, N)
|
2018-12-27 09:56:25 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(N)
|
|
|
|
// Add to queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
2019-04-16 19:14:09 +01:00
|
|
|
err := q.Insert(ctx, &pb.InjuredSegment{
|
2018-12-27 09:56:25 +00:00
|
|
|
Path: strconv.Itoa(i),
|
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
if len(errs) > 0 {
|
|
|
|
for err := range errs {
|
|
|
|
t.Error(err)
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
t.Fatal("unexpected queue.Insert errors")
|
|
|
|
}
|
2018-10-08 20:37:27 +01:00
|
|
|
|
|
|
|
wg.Add(N)
|
2019-04-16 19:14:09 +01:00
|
|
|
// Remove from queue concurrently
|
2018-10-08 20:37:27 +01:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
err = q.Delete(ctx, s)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
|
|
|
|
entries <- s
|
2018-10-08 20:37:27 +01:00
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
close(entries)
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
if len(errs) > 0 {
|
|
|
|
for err := range errs {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal("unexpected queue.Select/Delete errors")
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var items []*pb.InjuredSegment
|
|
|
|
for segment := range entries {
|
|
|
|
items = append(items, segment)
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
sort.Slice(items, func(i, k int) bool {
|
|
|
|
return items[i].LostPieces[0] < items[k].LostPieces[0]
|
|
|
|
})
|
|
|
|
|
2018-10-08 20:37:27 +01:00
|
|
|
// check if the enqueued and dequeued elements match
|
|
|
|
for i := 0; i < N; i++ {
|
2019-04-16 19:14:09 +01:00
|
|
|
require.Equal(t, items[i].LostPieces[0], int32(i))
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
})
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|