2014-11-16 16:18:59 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-11-16 16:18:59 -07:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2019-11-25 03:07:36 -07:00
|
|
|
"context"
|
2016-04-15 03:59:41 -07:00
|
|
|
"fmt"
|
2019-02-13 11:54:04 -07:00
|
|
|
"os"
|
2016-04-15 03:59:41 -07:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2014-11-16 16:18:59 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-04-15 03:59:41 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-11-16 16:18:59 -07:00
|
|
|
)
|
|
|
|
|
2016-04-15 03:59:41 -07:00
|
|
|
var timeout = 100 * time.Millisecond
|
|
|
|
|
|
|
|
func caller(skip int) string {
|
|
|
|
_, file, line, ok := runtime.Caller(skip + 1)
|
|
|
|
if !ok {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", filepath.Base(file), line)
|
|
|
|
}
|
2014-11-16 16:18:59 -07:00
|
|
|
|
2019-08-15 07:29:37 -07:00
|
|
|
func expectEvent(w events.Subscription, t *testing.T, size int) {
|
2014-11-16 16:18:59 -07:00
|
|
|
event, err := w.Poll(timeout)
|
|
|
|
if err != nil {
|
2016-04-15 03:59:41 -07:00
|
|
|
t.Fatal("Unexpected error:", err, "at", caller(1))
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
if event.Type != events.DownloadProgress {
|
2016-04-15 03:59:41 -07:00
|
|
|
t.Fatal("Unexpected event:", event, "at", caller(1))
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
data := event.Data.(map[string]map[string]*pullerProgress)
|
|
|
|
if len(data) != size {
|
2016-04-15 03:59:41 -07:00
|
|
|
t.Fatal("Unexpected event data size:", data, "at", caller(1))
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:29:37 -07:00
|
|
|
func expectTimeout(w events.Subscription, t *testing.T) {
|
2014-11-16 16:18:59 -07:00
|
|
|
_, err := w.Poll(timeout)
|
|
|
|
if err != events.ErrTimeout {
|
2016-04-15 03:59:41 -07:00
|
|
|
t.Fatal("Unexpected non-Timeout error:", err, "at", caller(1))
|
2014-11-16 16:18:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProgressEmitter(t *testing.T) {
|
2019-08-15 07:29:37 -07:00
|
|
|
evLogger := events.NewLogger()
|
|
|
|
go evLogger.Serve()
|
|
|
|
defer evLogger.Stop()
|
|
|
|
|
|
|
|
w := evLogger.Subscribe(events.DownloadProgress)
|
2014-11-16 16:18:59 -07:00
|
|
|
|
2018-08-16 03:11:48 -07:00
|
|
|
c := createTmpWrapper(config.Configuration{})
|
2019-02-13 11:54:04 -07:00
|
|
|
defer os.Remove(c.ConfigPath())
|
2014-11-16 16:18:59 -07:00
|
|
|
c.SetOptions(config.OptionsConfiguration{
|
2020-01-20 13:14:29 -07:00
|
|
|
ProgressUpdateIntervalS: 60, // irrelevant, but must be positive
|
2014-11-16 16:18:59 -07:00
|
|
|
})
|
|
|
|
|
2019-08-15 07:29:37 -07:00
|
|
|
p := NewProgressEmitter(c, evLogger)
|
2014-11-16 16:18:59 -07:00
|
|
|
go p.Serve()
|
2019-11-14 15:08:40 -07:00
|
|
|
defer p.Stop()
|
2016-08-16 11:22:01 -07:00
|
|
|
p.interval = 0
|
2014-11-16 16:18:59 -07:00
|
|
|
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
2015-04-22 15:54:31 -07:00
|
|
|
s := sharedPullerState{
|
2016-04-15 03:59:41 -07:00
|
|
|
updated: time.Now(),
|
|
|
|
mut: sync.NewRWMutex(),
|
2015-04-22 15:54:31 -07:00
|
|
|
}
|
2014-11-16 16:18:59 -07:00
|
|
|
p.Register(&s)
|
|
|
|
|
|
|
|
expectEvent(w, t, 1)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
2016-04-15 03:59:41 -07:00
|
|
|
s.copyDone(protocol.BlockInfo{})
|
2014-11-16 16:18:59 -07:00
|
|
|
|
|
|
|
expectEvent(w, t, 1)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
|
|
|
s.copiedFromOrigin()
|
|
|
|
|
|
|
|
expectEvent(w, t, 1)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
|
|
|
s.pullStarted()
|
2014-12-01 03:26:54 -07:00
|
|
|
|
2014-11-16 16:18:59 -07:00
|
|
|
expectEvent(w, t, 1)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
2016-04-15 03:59:41 -07:00
|
|
|
s.pullDone(protocol.BlockInfo{})
|
2014-11-16 16:18:59 -07:00
|
|
|
|
|
|
|
expectEvent(w, t, 1)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
|
|
|
p.Deregister(&s)
|
|
|
|
|
|
|
|
expectEvent(w, t, 0)
|
|
|
|
expectTimeout(w, t)
|
|
|
|
|
|
|
|
}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
func TestSendDownloadProgressMessages(t *testing.T) {
|
2018-08-16 03:11:48 -07:00
|
|
|
c := createTmpWrapper(config.Configuration{})
|
2019-02-13 11:54:04 -07:00
|
|
|
defer os.Remove(c.ConfigPath())
|
2016-04-15 03:59:41 -07:00
|
|
|
c.SetOptions(config.OptionsConfiguration{
|
2020-01-20 13:14:29 -07:00
|
|
|
ProgressUpdateIntervalS: 60, // irrelevant, but must be positive
|
2016-04-15 03:59:41 -07:00
|
|
|
TempIndexMinBlocks: 10,
|
|
|
|
})
|
|
|
|
|
2016-11-30 00:54:20 -07:00
|
|
|
fc := &fakeConnection{}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-08-15 07:29:37 -07:00
|
|
|
evLogger := events.NewLogger()
|
|
|
|
go evLogger.Serve()
|
|
|
|
defer evLogger.Stop()
|
|
|
|
|
|
|
|
p := NewProgressEmitter(c, evLogger)
|
2016-04-15 03:59:41 -07:00
|
|
|
p.temporaryIndexSubscribe(fc, []string{"folder", "folder2"})
|
2019-04-13 05:20:51 -07:00
|
|
|
p.registry["folder"] = make(map[string]*sharedPullerState)
|
|
|
|
p.registry["folder2"] = make(map[string]*sharedPullerState)
|
|
|
|
p.registry["folderXXX"] = make(map[string]*sharedPullerState)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect := func(updateIdx int, state *sharedPullerState, updateType protocol.FileDownloadProgressUpdateType, version protocol.Vector, blocks []int, remove bool) {
|
2016-04-15 03:59:41 -07:00
|
|
|
messageIdx := -1
|
|
|
|
for i, msg := range fc.downloadProgressMessages {
|
|
|
|
if msg.folder == state.folder {
|
|
|
|
messageIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if messageIdx < 0 {
|
|
|
|
t.Errorf("Message for folder %s does not exist at %s", state.folder, caller(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fc.downloadProgressMessages[messageIdx]
|
|
|
|
|
|
|
|
// Don't know the index (it's random due to iterating maps)
|
|
|
|
if updateIdx == -1 {
|
|
|
|
for i, upd := range msg.updates {
|
|
|
|
if upd.Name == state.file.Name {
|
|
|
|
updateIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateIdx == -1 {
|
|
|
|
t.Errorf("Could not find update for %s at %s", state.file.Name, caller(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateIdx > len(msg.updates)-1 {
|
|
|
|
t.Errorf("Update at index %d does not exist at %s", updateIdx, caller(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
update := msg.updates[updateIdx]
|
|
|
|
|
|
|
|
if update.UpdateType != updateType {
|
|
|
|
t.Errorf("Wrong update type at %s", caller(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !update.Version.Equal(version) {
|
|
|
|
t.Errorf("Wrong version at %s", caller(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(update.BlockIndexes) != len(blocks) {
|
|
|
|
t.Errorf("Wrong indexes. Have %d expect %d at %s", len(update.BlockIndexes), len(blocks), caller(1))
|
|
|
|
}
|
|
|
|
for i := range update.BlockIndexes {
|
|
|
|
if update.BlockIndexes[i] != blocks[i] {
|
|
|
|
t.Errorf("Index %d incorrect at %s", i, caller(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if remove {
|
|
|
|
fc.downloadProgressMessages = append(fc.downloadProgressMessages[:messageIdx], fc.downloadProgressMessages[messageIdx+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectEmpty := func() {
|
|
|
|
if len(fc.downloadProgressMessages) > 0 {
|
|
|
|
t.Errorf("Still have something at %s: %#v", caller(1), fc.downloadProgressMessages)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
tick := func() time.Time {
|
|
|
|
now = now.Add(time.Second)
|
|
|
|
return now
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fc.downloadProgressMessages) != 0 {
|
|
|
|
t.Error("Expected no requests")
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 := (protocol.Vector{}).Update(0)
|
|
|
|
v2 := (protocol.Vector{}).Update(1)
|
|
|
|
|
|
|
|
// Requires more than 10 blocks to work.
|
2019-02-02 04:09:07 -07:00
|
|
|
blocks := make([]protocol.BlockInfo, 11)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
state1 := &sharedPullerState{
|
|
|
|
folder: "folder",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state1",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
2019-04-13 05:20:51 -07:00
|
|
|
p.registry["folder"]["1"] = state1
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
// Has no blocks, hence no message is sent
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Returns update for puller with new extra blocks
|
2020-10-01 23:07:05 -07:00
|
|
|
state1.available = []int{1}
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(0, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1}, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Does nothing if nothing changes
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Does nothing if timestamp updated, but no new blocks (should never happen)
|
|
|
|
state1.availableUpdated = tick()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Does not return an update if date blocks change but date does not (should never happen)
|
2020-10-01 23:07:05 -07:00
|
|
|
state1.available = []int{1, 2}
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// If the date and blocks changes, returns only the diff
|
|
|
|
state1.availableUpdated = tick()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(0, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{2}, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Returns forget and update if puller version has changed
|
|
|
|
state1.file.Version = v2
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(0, state1, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, false)
|
|
|
|
expect(1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v2, []int{1, 2}, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
2016-05-22 03:16:09 -07:00
|
|
|
// Returns forget and append if sharedPullerState creation timer changes.
|
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
state1.available = []int{1}
|
2016-05-22 03:16:09 -07:00
|
|
|
state1.availableUpdated = tick()
|
|
|
|
state1.created = tick()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-05-22 03:16:09 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(0, state1, protocol.FileDownloadProgressUpdateTypeForget, v2, nil, false)
|
|
|
|
expect(1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v2, []int{1}, true)
|
2016-05-22 03:16:09 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
2016-04-15 03:59:41 -07:00
|
|
|
// Sends an empty update if new file exists, but does not have any blocks yet. (To indicate that the old blocks are no longer available)
|
|
|
|
state1.file.Version = v1
|
|
|
|
state1.available = nil
|
|
|
|
state1.availableUpdated = tick()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(0, state1, protocol.FileDownloadProgressUpdateTypeForget, v2, nil, false)
|
|
|
|
expect(1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, nil, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Updates for multiple files and folders can be combined
|
2020-10-01 23:07:05 -07:00
|
|
|
state1.available = []int{1, 2, 3}
|
2016-04-15 03:59:41 -07:00
|
|
|
state1.availableUpdated = tick()
|
|
|
|
|
|
|
|
state2 := &sharedPullerState{
|
|
|
|
folder: "folder2",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state2",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
|
|
|
state3 := &sharedPullerState{
|
|
|
|
folder: "folder",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state3",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
|
|
|
state4 := &sharedPullerState{
|
|
|
|
folder: "folder2",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state4",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
2019-04-13 05:20:51 -07:00
|
|
|
p.registry["folder2"]["2"] = state2
|
|
|
|
p.registry["folder"]["3"] = state3
|
|
|
|
p.registry["folder2"]["4"] = state4
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(-1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, false)
|
|
|
|
expect(-1, state3, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, true)
|
|
|
|
expect(-1, state2, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, false)
|
|
|
|
expect(-1, state4, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Returns forget if puller no longer exists, as well as updates if it has been updated.
|
2020-10-01 23:07:05 -07:00
|
|
|
state1.available = []int{1, 2, 3, 4, 5}
|
2016-04-15 03:59:41 -07:00
|
|
|
state1.availableUpdated = tick()
|
2020-10-01 23:07:05 -07:00
|
|
|
state2.available = []int{1, 2, 3, 4, 5}
|
2016-04-15 03:59:41 -07:00
|
|
|
state2.availableUpdated = tick()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
delete(p.registry["folder"], "3")
|
|
|
|
delete(p.registry["folder2"], "4")
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(-1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{4, 5}, false)
|
|
|
|
expect(-1, state3, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, true)
|
|
|
|
expect(-1, state2, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{4, 5}, false)
|
|
|
|
expect(-1, state4, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Deletions are sent only once (actual bug I found writing the tests)
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Not sent for "inactive" (symlinks, dirs, or wrong folder) pullers
|
|
|
|
// Directory
|
|
|
|
state5 := &sharedPullerState{
|
|
|
|
folder: "folder",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state5",
|
|
|
|
Version: v1,
|
2016-07-04 03:40:29 -07:00
|
|
|
Type: protocol.FileInfoTypeDirectory,
|
2016-04-15 03:59:41 -07:00
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
|
|
|
// Symlink
|
|
|
|
state6 := &sharedPullerState{
|
|
|
|
folder: "folder",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state6",
|
|
|
|
Version: v1,
|
2017-02-07 01:34:24 -07:00
|
|
|
Type: protocol.FileInfoTypeSymlink,
|
2016-04-15 03:59:41 -07:00
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
|
|
|
// Some other directory
|
|
|
|
state7 := &sharedPullerState{
|
|
|
|
folder: "folderXXX",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state7",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks,
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
|
|
|
// Less than 10 blocks
|
|
|
|
state8 := &sharedPullerState{
|
|
|
|
folder: "folder",
|
|
|
|
file: protocol.FileInfo{
|
|
|
|
Name: "state8",
|
|
|
|
Version: v1,
|
|
|
|
Blocks: blocks[:3],
|
|
|
|
},
|
|
|
|
mut: sync.NewRWMutex(),
|
2020-10-01 23:07:05 -07:00
|
|
|
available: []int{1, 2, 3},
|
2016-04-15 03:59:41 -07:00
|
|
|
availableUpdated: time.Now(),
|
|
|
|
}
|
2019-04-13 05:20:51 -07:00
|
|
|
p.registry["folder"]["5"] = state5
|
|
|
|
p.registry["folder"]["6"] = state6
|
|
|
|
p.registry["folderXXX"]["7"] = state7
|
|
|
|
p.registry["folder"]["8"] = state8
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Device is no longer subscribed to a particular folder
|
2019-04-13 05:20:51 -07:00
|
|
|
delete(p.registry["folder"], "1") // Clean up first
|
|
|
|
delete(p.registry["folder2"], "2") // Clean up first
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(-1, state1, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, true)
|
|
|
|
expect(-1, state2, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
expectEmpty()
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
p.registry["folder"]["1"] = state1
|
|
|
|
p.registry["folder2"]["2"] = state2
|
|
|
|
p.registry["folder"]["3"] = state3
|
|
|
|
p.registry["folder2"]["4"] = state4
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
2020-10-01 23:07:05 -07:00
|
|
|
expect(-1, state1, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3, 4, 5}, false)
|
|
|
|
expect(-1, state3, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, true)
|
|
|
|
expect(-1, state2, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3, 4, 5}, false)
|
|
|
|
expect(-1, state4, protocol.FileDownloadProgressUpdateTypeAppend, v1, []int{1, 2, 3}, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
p.temporaryIndexUnsubscribe(fc)
|
|
|
|
p.temporaryIndexSubscribe(fc, []string{"folder"})
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
// See progressemitter.go for explanation why this is commented out.
|
|
|
|
// Search for state.cleanup
|
2020-10-01 23:07:05 -07:00
|
|
|
//expect(-1, state2, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, false)
|
|
|
|
//expect(-1, state4, protocol.FileDownloadProgressUpdateTypeForget, v1, nil, true)
|
2016-04-15 03:59:41 -07:00
|
|
|
|
|
|
|
expectEmpty()
|
|
|
|
|
|
|
|
// Cleanup when device no longer exists
|
|
|
|
p.temporaryIndexUnsubscribe(fc)
|
|
|
|
|
2019-04-13 05:20:51 -07:00
|
|
|
sendMsgs(p)
|
2016-04-15 03:59:41 -07:00
|
|
|
_, ok := p.sentDownloadStates[fc.ID()]
|
|
|
|
if ok {
|
|
|
|
t.Error("Should not be there")
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 05:20:51 -07:00
|
|
|
|
|
|
|
func sendMsgs(p *ProgressEmitter) {
|
|
|
|
p.mut.Lock()
|
2020-05-01 00:54:15 -07:00
|
|
|
updates := p.computeProgressUpdates()
|
|
|
|
p.mut.Unlock()
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, update := range updates {
|
|
|
|
update.send(ctx)
|
|
|
|
}
|
2019-04-13 05:20:51 -07:00
|
|
|
}
|