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"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-10-02 00:25:41 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/errs2"
|
|
|
|
"storj.io/storj/private/testcontext"
|
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{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte("abc"),
|
2018-12-21 15:11:19 +00:00
|
|
|
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{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte("abc"),
|
2019-04-22 17:41:53 +01:00
|
|
|
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{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte(strconv.Itoa(i)),
|
2018-12-21 15:11:19 +00:00
|
|
|
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)
|
2019-12-10 16:32:54 +00:00
|
|
|
|
|
|
|
sort.SliceStable(list, func(i, j int) bool { return list[i].LostPieces[0] < list[j].LostPieces[0] })
|
|
|
|
|
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) {
|
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
|
2019-04-16 19:14:09 +01:00
|
|
|
entries := make(chan *pb.InjuredSegment, N)
|
2019-04-24 11:15:46 +01:00
|
|
|
|
|
|
|
var inserts errs2.Group
|
2018-12-27 09:56:25 +00:00
|
|
|
// Add to queue concurrently
|
|
|
|
for i := 0; i < N; i++ {
|
2019-04-24 11:15:46 +01:00
|
|
|
i := i
|
|
|
|
inserts.Go(func() error {
|
|
|
|
return q.Insert(ctx, &pb.InjuredSegment{
|
2019-07-11 18:26:07 +01:00
|
|
|
Path: []byte(strconv.Itoa(i)),
|
2018-12-27 09:56:25 +00:00
|
|
|
LostPieces: []int32{int32(i)},
|
|
|
|
})
|
2019-04-24 11:15:46 +01:00
|
|
|
})
|
2018-12-27 09:56:25 +00:00
|
|
|
}
|
2019-04-24 11:15:46 +01:00
|
|
|
require.Empty(t, inserts.Wait(), "unexpected queue.Insert errors")
|
2018-10-02 00:25:41 +01:00
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
// Remove from queue concurrently
|
2019-04-24 11:15:46 +01:00
|
|
|
var remove errs2.Group
|
2018-10-08 20:37:27 +01:00
|
|
|
for i := 0; i < N; i++ {
|
2019-04-24 11:15:46 +01:00
|
|
|
remove.Go(func() error {
|
2019-04-16 19:14:09 +01:00
|
|
|
s, err := q.Select(ctx)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
2019-04-24 11:15:46 +01:00
|
|
|
return err
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:14:09 +01:00
|
|
|
err = q.Delete(ctx, s)
|
2018-10-08 20:37:27 +01:00
|
|
|
if err != nil {
|
2019-04-24 11:15:46 +01:00
|
|
|
return err
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
|
|
|
|
entries <- s
|
2019-04-24 11:15:46 +01:00
|
|
|
return nil
|
|
|
|
})
|
2018-10-08 20:37:27 +01:00
|
|
|
}
|
2019-04-16 19:14:09 +01:00
|
|
|
|
2019-04-24 11:15:46 +01:00
|
|
|
require.Empty(t, remove.Wait(), "unexpected queue.Select/Delete errors")
|
|
|
|
close(entries)
|
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
|
|
|
}
|