2020-06-22 17:31:08 +02:00
|
|
|
package writeback
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2020-06-19 16:37:13 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-06-20 16:34:26 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-06-19 16:37:13 +02:00
|
|
|
)
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func newTestWriteBack(t *testing.T) (wb *WriteBack, cancel func()) {
|
2020-06-19 16:37:13 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2024-07-03 12:34:29 +02:00
|
|
|
opt := vfscommon.Opt
|
2024-07-02 18:31:51 +02:00
|
|
|
opt.WriteBack = fs.Duration(100 * time.Millisecond)
|
2020-06-22 17:31:08 +02:00
|
|
|
wb = New(ctx, &opt)
|
2020-06-19 16:37:13 +02:00
|
|
|
return wb, cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
// string for debugging - make a copy and pop the items out in order
|
2020-06-22 17:31:08 +02:00
|
|
|
func (wb *WriteBack) string(t *testing.T) string {
|
2020-06-21 11:53:35 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
ws := wb.items
|
|
|
|
|
2020-06-19 16:37:13 +02:00
|
|
|
// check indexes OK first
|
|
|
|
for i := range ws {
|
|
|
|
assert.Equal(t, i, ws[i].index, ws[i].name)
|
|
|
|
}
|
|
|
|
wsCopy := make(writeBackItems, len(ws))
|
|
|
|
// deep copy the elements
|
|
|
|
for i := range wsCopy {
|
|
|
|
item := *ws[i]
|
|
|
|
wsCopy[i] = &item
|
|
|
|
}
|
|
|
|
// print them
|
|
|
|
var out []string
|
|
|
|
for wsCopy.Len() > 0 {
|
|
|
|
out = append(out, heap.Pop(&wsCopy).(*writeBackItem).name)
|
|
|
|
}
|
|
|
|
return strings.Join(out, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBackItems(t *testing.T) {
|
|
|
|
// Test the items heap behaves properly
|
|
|
|
now := time.Now()
|
|
|
|
wbItem1 := writeBackItem{name: "one", expiry: now.Add(1 * time.Second)}
|
|
|
|
wbItem2 := writeBackItem{name: "two", expiry: now.Add(2 * time.Second)}
|
|
|
|
wbItem3 := writeBackItem{name: "three", expiry: now.Add(4 * time.Second)}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
wb := &WriteBack{
|
2020-06-21 11:53:35 +02:00
|
|
|
items: writeBackItems{},
|
|
|
|
}
|
2020-06-19 16:37:13 +02:00
|
|
|
|
2020-06-21 11:53:35 +02:00
|
|
|
heap.Init(&wb.items)
|
|
|
|
assert.Equal(t, "", wb.string(t))
|
|
|
|
heap.Push(&wb.items, &wbItem2)
|
|
|
|
assert.Equal(t, "two", wb.string(t))
|
|
|
|
heap.Push(&wb.items, &wbItem3)
|
|
|
|
assert.Equal(t, "two,three", wb.string(t))
|
|
|
|
heap.Push(&wb.items, &wbItem1)
|
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
2020-06-21 11:53:35 +02:00
|
|
|
wb.items._update(&wbItem1, now.Add(3*time.Second))
|
|
|
|
assert.Equal(t, "two,one,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
2020-06-21 11:53:35 +02:00
|
|
|
wb.items._update(&wbItem1, now.Add(5*time.Second))
|
|
|
|
assert.Equal(t, "two,three,one", wb.string(t))
|
2020-06-21 12:45:15 +02:00
|
|
|
|
|
|
|
// Set all times the same - should sort in insertion order
|
|
|
|
wb.items._update(&wbItem1, now)
|
|
|
|
wb.items._update(&wbItem2, now)
|
|
|
|
wb.items._update(&wbItem3, now)
|
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func checkOnHeap(t *testing.T, wb *WriteBack, wbItem *writeBackItem) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
assert.True(t, wbItem.onHeap)
|
|
|
|
for i := range wb.items {
|
|
|
|
if wb.items[i] == wbItem {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Failf(t, "expecting %q on heap", wbItem.name)
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func checkNotOnHeap(t *testing.T, wb *WriteBack, wbItem *writeBackItem) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
assert.False(t, wbItem.onHeap)
|
|
|
|
for i := range wb.items {
|
|
|
|
if wb.items[i] == wbItem {
|
|
|
|
t.Errorf("not expecting %q on heap", wbItem.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func checkInLookup(t *testing.T, wb *WriteBack, wbItem *writeBackItem) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Equal(t, wbItem, wb.lookup[wbItem.id])
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func checkNotInLookup(t *testing.T, wb *WriteBack, wbItem *writeBackItem) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Nil(t, wb.lookup[wbItem.id])
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBackItemCRUD(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// _peekItem empty
|
|
|
|
assert.Nil(t, wb._peekItem())
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
wbItem1 := wb._newItem(0, "one", 10)
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem1)
|
|
|
|
checkInLookup(t, wb, wbItem1)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
wbItem2 := wb._newItem(0, "two", 10)
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
wbItem3 := wb._newItem(0, "three", 10)
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem3)
|
|
|
|
checkInLookup(t, wb, wbItem3)
|
|
|
|
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// _delItem
|
|
|
|
wb._delItem(wbItem2)
|
|
|
|
checkOnHeap(t, wb, wbItem2)
|
|
|
|
checkNotInLookup(t, wb, wbItem2)
|
|
|
|
|
|
|
|
// _addItem
|
|
|
|
wb._addItem(wbItem2)
|
|
|
|
checkOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
|
|
|
|
|
|
|
// _popItem
|
|
|
|
assert.True(t, wbItem1.onHeap)
|
|
|
|
poppedWbItem := wb._popItem()
|
|
|
|
assert.Equal(t, wbItem1, poppedWbItem)
|
|
|
|
checkNotOnHeap(t, wb, wbItem1)
|
|
|
|
checkInLookup(t, wb, wbItem1)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// _pushItem
|
|
|
|
wb._pushItem(wbItem1)
|
|
|
|
checkOnHeap(t, wb, wbItem1)
|
|
|
|
checkInLookup(t, wb, wbItem1)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
// push twice
|
|
|
|
wb._pushItem(wbItem1)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// _peekItem
|
|
|
|
assert.Equal(t, wbItem1, wb._peekItem())
|
|
|
|
|
|
|
|
// _removeItem
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,two,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
wb._removeItem(wbItem2)
|
|
|
|
checkNotOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
// remove twice
|
|
|
|
wb._removeItem(wbItem2)
|
|
|
|
checkNotOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one,three", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func assertTimerRunning(t *testing.T, wb *WriteBack, running bool) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb.mu.Lock()
|
|
|
|
if running {
|
|
|
|
assert.NotEqual(t, time.Time{}, wb.expiry)
|
|
|
|
assert.NotNil(t, wb.timer)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, time.Time{}, wb.expiry)
|
|
|
|
assert.Nil(t, wb.timer)
|
|
|
|
}
|
|
|
|
wb.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBackResetTimer(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Reset the timer on an empty queue
|
|
|
|
wb._resetTimer()
|
|
|
|
|
|
|
|
// Check timer is stopped
|
|
|
|
assertTimerRunning(t, wb, false)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
_ = wb._newItem(0, "three", 10)
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// Reset the timer on an queue with stuff
|
|
|
|
wb._resetTimer()
|
|
|
|
|
|
|
|
// Check timer is not stopped
|
|
|
|
assertTimerRunning(t, wb, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A "transfer" for testing
|
|
|
|
type putItem struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
mu sync.Mutex
|
|
|
|
t *testing.T
|
|
|
|
started chan struct{}
|
|
|
|
errChan chan error
|
|
|
|
running bool
|
|
|
|
cancelled bool
|
|
|
|
called bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPutItem(t *testing.T) *putItem {
|
|
|
|
return &putItem{
|
|
|
|
t: t,
|
|
|
|
started: make(chan struct{}, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
// put the object as per PutFn interface
|
2020-06-19 16:37:13 +02:00
|
|
|
func (pi *putItem) put(ctx context.Context) (err error) {
|
|
|
|
pi.wg.Add(1)
|
|
|
|
defer pi.wg.Done()
|
|
|
|
|
|
|
|
pi.mu.Lock()
|
|
|
|
pi.called = true
|
|
|
|
if pi.running {
|
|
|
|
assert.Fail(pi.t, "upload already running")
|
|
|
|
}
|
|
|
|
pi.running = true
|
|
|
|
pi.errChan = make(chan error, 1)
|
|
|
|
pi.mu.Unlock()
|
|
|
|
|
|
|
|
pi.started <- struct{}{}
|
|
|
|
|
|
|
|
cancelled := false
|
|
|
|
select {
|
|
|
|
case err = <-pi.errChan:
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = ctx.Err()
|
|
|
|
cancelled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.mu.Lock()
|
|
|
|
pi.running = false
|
|
|
|
pi.cancelled = cancelled
|
|
|
|
pi.mu.Unlock()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish the "transfer" with the error passed in
|
|
|
|
func (pi *putItem) finish(err error) {
|
|
|
|
pi.mu.Lock()
|
|
|
|
if !pi.running {
|
|
|
|
assert.Fail(pi.t, "upload not running")
|
|
|
|
}
|
|
|
|
pi.mu.Unlock()
|
|
|
|
|
|
|
|
pi.errChan <- err
|
|
|
|
pi.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
func waitUntilNoTransfers(t *testing.T, wb *WriteBack) {
|
2020-06-19 16:37:13 +02:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wb.mu.Lock()
|
|
|
|
uploads := wb.uploads
|
|
|
|
wb.mu.Unlock()
|
|
|
|
if uploads == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Errorf("failed to wait for transfer to finish")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the happy path with everything working
|
|
|
|
func TestWriteBackAddOK(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
var inID Handle
|
|
|
|
wb.SetID(&inID)
|
|
|
|
assert.Equal(t, Handle(1), inID)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(inID, "one", 10, true, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Equal(t, inID, id)
|
|
|
|
wbItem := wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
pi.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkNotInLookup(t, wb, wbItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test the upload failing and being retried
|
|
|
|
func TestWriteBackAddFailRetry(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
pi.finish(errors.New("transfer failed BOOM"))
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// check the retry
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
pi.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkNotInLookup(t, wb, wbItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test the upload being cancelled by another upload being added
|
|
|
|
func TestWriteBackAddUpdate(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2024-06-20 16:20:47 +02:00
|
|
|
assert.Equal(t, int64(10), wbItem.size) // check size
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// Now the upload has started add another one
|
|
|
|
|
|
|
|
pi2 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id2 := wb.Add(id, "one", 20, true, pi2.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Equal(t, id, id2)
|
2024-06-20 16:20:47 +02:00
|
|
|
assert.Equal(t, int64(20), wbItem.size) // check size has changed
|
|
|
|
checkOnHeap(t, wb, wbItem) // object awaiting writeback time
|
2020-06-19 16:37:13 +02:00
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// check the previous transfer was cancelled
|
|
|
|
assert.True(t, pi.cancelled)
|
|
|
|
|
|
|
|
// check the retry
|
|
|
|
<-pi2.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
pi2.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkNotInLookup(t, wb, wbItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test the upload being not cancelled by another upload being added
|
|
|
|
func TestWriteBackAddUpdateNotModified(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, false, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// Now the upload has started add another one
|
|
|
|
|
|
|
|
pi2 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id2 := wb.Add(id, "one", 10, false, pi2.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Equal(t, id, id2)
|
2022-08-14 04:56:32 +02:00
|
|
|
checkNotOnHeap(t, wb, wbItem) // object still being transferred
|
2020-06-19 16:37:13 +02:00
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// Because modified was false above this should not cancel the
|
|
|
|
// transfer
|
|
|
|
assert.False(t, pi.cancelled)
|
|
|
|
|
|
|
|
// wait for original transfer to finish
|
|
|
|
pi.finish(nil)
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkNotInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
assert.False(t, pi2.called)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test the upload being not cancelled by another upload being
|
|
|
|
// added because the upload hasn't started yet
|
|
|
|
func TestWriteBackAddUpdateNotStarted(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-21 11:53:35 +02:00
|
|
|
assert.Equal(t, "one", wb.string(t))
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// Immediately add another upload before the first has started
|
|
|
|
|
|
|
|
pi2 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id2 := wb.Add(id, "one", 10, true, pi2.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.Equal(t, id, id2)
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem) // object still awaiting transfer
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// Wait for the upload to start
|
|
|
|
<-pi2.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// Because modified was false above this should not cancel the
|
|
|
|
// transfer
|
|
|
|
assert.False(t, pi.cancelled)
|
|
|
|
|
|
|
|
// wait for new transfer to finish
|
|
|
|
pi2.finish(nil)
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkNotInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
assert.False(t, pi.called)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteBackGetStats(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
wb.Add(0, "one", 10, true, pi.put)
|
2020-06-19 16:37:13 +02:00
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued := wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, queued, 1)
|
|
|
|
assert.Equal(t, inProgress, 0)
|
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued = wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, queued, 0)
|
|
|
|
assert.Equal(t, inProgress, 1)
|
|
|
|
|
|
|
|
pi.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued = wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, queued, 0)
|
|
|
|
assert.Equal(t, inProgress, 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-20 16:34:26 +02:00
|
|
|
func TestWriteBackQueue(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
|
|
|
|
|
|
|
queue := wb.Queue()
|
|
|
|
require.Equal(t, 1, len(queue))
|
|
|
|
assert.Greater(t, queue[0].Expiry, 0.0)
|
|
|
|
assert.Less(t, queue[0].Expiry, 1.0)
|
|
|
|
queue[0].Expiry = 0.0
|
|
|
|
assert.Equal(t, []QueueInfo{
|
|
|
|
{
|
|
|
|
Name: "one",
|
|
|
|
Size: 10,
|
|
|
|
Expiry: 0.0,
|
|
|
|
Tries: 0,
|
|
|
|
Delay: 0.1,
|
|
|
|
Uploading: false,
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
}, queue)
|
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
|
|
|
|
queue = wb.Queue()
|
|
|
|
require.Equal(t, 1, len(queue))
|
|
|
|
assert.Less(t, queue[0].Expiry, 0.0)
|
|
|
|
assert.Greater(t, queue[0].Expiry, -1.0)
|
|
|
|
queue[0].Expiry = 0.0
|
|
|
|
assert.Equal(t, []QueueInfo{
|
|
|
|
{
|
|
|
|
Name: "one",
|
|
|
|
Size: 10,
|
|
|
|
Expiry: 0.0,
|
|
|
|
Tries: 1,
|
|
|
|
Delay: 0.1,
|
|
|
|
Uploading: true,
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
}, queue)
|
|
|
|
|
|
|
|
pi.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
|
|
|
|
queue = wb.Queue()
|
|
|
|
assert.Equal(t, []QueueInfo{}, queue)
|
|
|
|
}
|
|
|
|
|
2024-06-20 18:00:41 +02:00
|
|
|
func TestWriteBackSetExpiry(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err := wb.SetExpiry(123123123, time.Now())
|
|
|
|
assert.Equal(t, ErrorIDNotFound, err)
|
|
|
|
|
|
|
|
pi := newPutItem(t)
|
|
|
|
|
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
|
|
|
wbItem := wb.lookup[id]
|
|
|
|
|
|
|
|
// get the expiry time with locking so we don't cause races
|
|
|
|
getExpiry := func() time.Time {
|
|
|
|
wb.mu.Lock()
|
|
|
|
defer wb.mu.Unlock()
|
|
|
|
return wbItem.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
expiry := time.Until(getExpiry()).Seconds()
|
|
|
|
assert.Greater(t, expiry, 0.0)
|
|
|
|
assert.Less(t, expiry, 1.0)
|
|
|
|
|
|
|
|
newExpiry := time.Now().Add(100 * time.Second)
|
|
|
|
require.NoError(t, wb.SetExpiry(wbItem.id, newExpiry))
|
|
|
|
assert.Equal(t, newExpiry, getExpiry())
|
|
|
|
|
|
|
|
// This starts the transfer
|
|
|
|
newExpiry = time.Now().Add(-100 * time.Second)
|
|
|
|
require.NoError(t, wb.SetExpiry(wbItem.id, newExpiry))
|
|
|
|
assert.Equal(t, newExpiry, getExpiry())
|
|
|
|
|
|
|
|
<-pi.started
|
|
|
|
|
|
|
|
expiry = time.Until(getExpiry()).Seconds()
|
|
|
|
assert.LessOrEqual(t, expiry, -100.0)
|
|
|
|
|
|
|
|
pi.finish(nil) // transfer successful
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
|
|
|
|
expiry = time.Until(getExpiry()).Seconds()
|
|
|
|
assert.LessOrEqual(t, expiry, -100.0)
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:37:13 +02:00
|
|
|
// Test queuing more than fs.Config.Transfers
|
|
|
|
func TestWriteBackMaxQueue(t *testing.T) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2020-06-19 16:37:13 +02:00
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
maxTransfers := ci.Transfers
|
2020-06-19 16:37:13 +02:00
|
|
|
toTransfer := maxTransfers + 2
|
|
|
|
|
|
|
|
// put toTransfer things in the queue
|
|
|
|
pis := []*putItem{}
|
|
|
|
for i := 0; i < toTransfer; i++ {
|
|
|
|
pi := newPutItem(t)
|
|
|
|
pis = append(pis, pi)
|
2024-06-20 16:20:47 +02:00
|
|
|
wb.Add(0, fmt.Sprintf("number%d", 1), 10, true, pi.put)
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued := wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, toTransfer, queued)
|
|
|
|
assert.Equal(t, 0, inProgress)
|
|
|
|
|
|
|
|
// now start the first maxTransfers - this should stop the timer
|
|
|
|
for i := 0; i < maxTransfers; i++ {
|
|
|
|
<-pis[i].started
|
|
|
|
}
|
|
|
|
|
|
|
|
// timer should be stopped now
|
|
|
|
assertTimerRunning(t, wb, false)
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued = wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, toTransfer-maxTransfers, queued)
|
|
|
|
assert.Equal(t, maxTransfers, inProgress)
|
|
|
|
|
2022-08-14 04:56:32 +02:00
|
|
|
// now finish the first maxTransfers
|
2020-06-19 16:37:13 +02:00
|
|
|
for i := 0; i < maxTransfers; i++ {
|
|
|
|
pis[i].finish(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now start and finish the remaining transfers one at a time
|
|
|
|
for i := maxTransfers; i < toTransfer; i++ {
|
|
|
|
<-pis[i].started
|
|
|
|
pis[i].finish(nil)
|
|
|
|
}
|
|
|
|
waitUntilNoTransfers(t, wb)
|
|
|
|
|
2020-06-22 17:31:08 +02:00
|
|
|
inProgress, queued = wb.Stats()
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.Equal(t, queued, 0)
|
|
|
|
assert.Equal(t, inProgress, 0)
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:28 +02:00
|
|
|
func TestWriteBackRename(t *testing.T) {
|
2020-06-19 16:37:13 +02:00
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// cancel when not in writeback
|
2020-07-03 18:16:28 +02:00
|
|
|
wb.Rename(1, "nonExistent")
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// add item
|
|
|
|
pi1 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, true, pi1.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-07-03 18:16:28 +02:00
|
|
|
assert.Equal(t, wbItem.name, "one")
|
2020-06-19 16:37:13 +02:00
|
|
|
|
2020-07-03 18:16:28 +02:00
|
|
|
// rename when not uploading
|
|
|
|
wb.Rename(id, "two")
|
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.False(t, pi1.cancelled)
|
2020-07-03 18:16:28 +02:00
|
|
|
assert.Equal(t, wbItem.name, "two")
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// add item
|
|
|
|
pi2 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id = wb.Add(id, "two", 10, true, pi2.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem = wb.lookup[id]
|
2020-06-19 16:37:13 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-07-03 18:16:28 +02:00
|
|
|
assert.Equal(t, wbItem.name, "two")
|
2020-06-19 16:37:13 +02:00
|
|
|
|
|
|
|
// wait for upload to start
|
|
|
|
<-pi2.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
2020-07-03 18:16:28 +02:00
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// rename when uploading - goes back on heap
|
|
|
|
wb.Rename(id, "three")
|
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
2020-06-19 16:37:13 +02:00
|
|
|
assert.True(t, pi2.cancelled)
|
2020-07-03 18:16:28 +02:00
|
|
|
assert.Equal(t, wbItem.name, "three")
|
2020-06-19 16:37:13 +02:00
|
|
|
}
|
2020-06-21 12:58:56 +02:00
|
|
|
|
2021-07-27 17:38:30 +02:00
|
|
|
// TestWriteBackRenameDuplicates checks that if we rename an entry and
|
|
|
|
// make a duplicate, we remove the duplicate.
|
|
|
|
func TestWriteBackRenameDuplicates(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-06-20 16:20:47 +02:00
|
|
|
// add item "one", 10
|
2021-07-27 17:38:30 +02:00
|
|
|
pi1 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id1 := wb.Add(0, "one", 10, true, pi1.put)
|
2021-07-27 17:38:30 +02:00
|
|
|
wbItem1 := wb.lookup[id1]
|
|
|
|
checkOnHeap(t, wb, wbItem1)
|
|
|
|
checkInLookup(t, wb, wbItem1)
|
|
|
|
assert.Equal(t, wbItem1.name, "one")
|
|
|
|
|
|
|
|
<-pi1.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem1)
|
|
|
|
checkInLookup(t, wb, wbItem1)
|
|
|
|
|
|
|
|
// add item "two"
|
|
|
|
pi2 := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id2 := wb.Add(0, "two", 10, true, pi2.put)
|
2021-07-27 17:38:30 +02:00
|
|
|
wbItem2 := wb.lookup[id2]
|
|
|
|
checkOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
|
|
|
assert.Equal(t, wbItem2.name, "two")
|
|
|
|
|
|
|
|
<-pi2.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
|
|
|
|
|
|
|
// rename "two" to "one"
|
|
|
|
wb.Rename(id2, "one")
|
|
|
|
|
|
|
|
// check "one" is cancelled and removed from heap and lookup
|
|
|
|
checkNotOnHeap(t, wb, wbItem1)
|
|
|
|
checkNotInLookup(t, wb, wbItem1)
|
|
|
|
assert.True(t, pi1.cancelled)
|
|
|
|
assert.Equal(t, wbItem1.name, "one")
|
|
|
|
|
|
|
|
// check "two" (now called "one"!) has been cancelled and will
|
|
|
|
// be retried
|
|
|
|
checkOnHeap(t, wb, wbItem2)
|
|
|
|
checkInLookup(t, wb, wbItem2)
|
|
|
|
assert.True(t, pi2.cancelled)
|
|
|
|
assert.Equal(t, wbItem2.name, "one")
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:58:56 +02:00
|
|
|
func TestWriteBackCancelUpload(t *testing.T) {
|
|
|
|
wb, cancel := newTestWriteBack(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// cancel when not in writeback
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.False(t, wb.cancelUpload(1))
|
2020-06-21 12:58:56 +02:00
|
|
|
|
|
|
|
// add item
|
|
|
|
pi := newPutItem(t)
|
2024-06-20 16:20:47 +02:00
|
|
|
id := wb.Add(0, "one", 10, true, pi.put)
|
2020-06-22 17:31:08 +02:00
|
|
|
wbItem := wb.lookup[id]
|
2020-06-21 12:58:56 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// cancel when not uploading
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.False(t, wb.cancelUpload(id))
|
2020-06-21 12:58:56 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// wait for upload to start
|
|
|
|
<-pi.started
|
|
|
|
checkNotOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
|
|
|
|
// cancel when uploading
|
2020-06-22 17:31:08 +02:00
|
|
|
assert.True(t, wb.cancelUpload(id))
|
2020-06-21 12:58:56 +02:00
|
|
|
checkOnHeap(t, wb, wbItem)
|
|
|
|
checkInLookup(t, wb, wbItem)
|
|
|
|
assert.True(t, pi.cancelled)
|
|
|
|
}
|