syncthing/lib/model/queue_test.go

408 lines
11 KiB
Go
Raw Permalink Normal View History

2014-12-01 21:33:40 +00:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 20:36:35 +00: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,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2014-12-01 21:33:40 +00:00
package model
import (
"fmt"
"math/rand"
2024-02-10 20:02:42 +00:00
"slices"
2014-12-01 21:33:40 +00:00
"testing"
"time"
2016-03-06 20:32:10 +00:00
"github.com/d4l3k/messagediff"
2014-12-01 21:33:40 +00:00
)
func TestJobQueue(t *testing.T) {
// Some random actions
2014-12-30 08:35:21 +00:00
q := newJobQueue()
q.Push("f1", 0, time.Time{})
q.Push("f2", 0, time.Time{})
q.Push("f3", 0, time.Time{})
q.Push("f4", 0, time.Time{})
2014-12-01 21:33:40 +00:00
progress, queued, _ := q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length", len(progress), len(queued))
2014-12-01 21:33:40 +00:00
}
for i := 1; i < 5; i++ {
n, ok := q.Pop()
if !ok || n != fmt.Sprintf("f%d", i) {
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong element")
}
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 1 || len(queued) != 3 {
2014-12-30 08:35:21 +00:00
t.Log(progress)
t.Log(queued)
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong length")
}
q.Done(n)
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 3 {
t.Fatal("Wrong length", len(progress), len(queued))
}
q.Push(n, 0, time.Time{})
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length")
}
q.Done("f5") // Does not exist
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length")
}
}
if len(q.progress) > 0 || len(q.queued) != 4 {
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong length")
}
for i := 4; i > 0; i-- {
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 4-i || len(queued) != i {
t.Fatal("Wrong length")
}
s := fmt.Sprintf("f%d", i)
2014-12-30 08:35:21 +00:00
q.BringToFront(s)
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 4-i || len(queued) != i {
t.Fatal("Wrong length")
}
n, ok := q.Pop()
if !ok || n != s {
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong element")
}
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 5-i || len(queued) != i-1 {
t.Fatal("Wrong length")
}
q.Done("f5") // Does not exist
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 5-i || len(queued) != i-1 {
t.Fatal("Wrong length")
}
}
_, ok := q.Pop()
if len(q.progress) != 4 || ok {
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong length")
}
q.Done("f1")
q.Done("f2")
q.Done("f3")
q.Done("f4")
q.Done("f5") // Does not exist
2014-12-01 21:33:40 +00:00
_, ok = q.Pop()
if len(q.progress) != 0 || ok {
2014-12-01 21:33:40 +00:00
t.Fatal("Wrong length")
}
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 0 {
t.Fatal("Wrong length")
}
2014-12-30 08:35:21 +00:00
q.BringToFront("")
q.Done("f5") // Does not exist
progress, queued, _ = q.Jobs(1, 100)
2014-12-01 21:33:40 +00:00
if len(progress) != 0 || len(queued) != 0 {
t.Fatal("Wrong length")
}
}
2014-12-30 08:07:49 +00:00
2014-12-30 08:35:21 +00:00
func TestBringToFront(t *testing.T) {
q := newJobQueue()
q.Push("f1", 0, time.Time{})
q.Push("f2", 0, time.Time{})
q.Push("f3", 0, time.Time{})
q.Push("f4", 0, time.Time{})
2014-12-30 08:07:49 +00:00
_, queued, _ := q.Jobs(1, 100)
if diff, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("Order does not match. Diff:\n%s", diff)
2014-12-30 08:07:49 +00:00
}
2014-12-30 08:35:21 +00:00
q.BringToFront("f1") // corner case: does nothing
2014-12-30 08:07:49 +00:00
_, queued, _ = q.Jobs(1, 100)
if diff, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("Order does not match. Diff:\n%s", diff)
2014-12-30 08:07:49 +00:00
}
2014-12-30 08:35:21 +00:00
q.BringToFront("f3")
_, queued, _ = q.Jobs(1, 100)
if diff, equal := messagediff.PrettyDiff([]string{"f3", "f1", "f2", "f4"}, queued); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("Order does not match. Diff:\n%s", diff)
2014-12-30 08:07:49 +00:00
}
2014-12-30 08:35:21 +00:00
q.BringToFront("f2")
2014-12-30 08:07:49 +00:00
_, queued, _ = q.Jobs(1, 100)
if diff, equal := messagediff.PrettyDiff([]string{"f2", "f3", "f1", "f4"}, queued); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("Order does not match. Diff:\n%s", diff)
2014-12-30 08:07:49 +00:00
}
2014-12-30 08:35:21 +00:00
q.BringToFront("f4") // corner case: last element
_, queued, _ = q.Jobs(1, 100)
if diff, equal := messagediff.PrettyDiff([]string{"f4", "f2", "f3", "f1"}, queued); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("Order does not match. Diff:\n%s", diff)
2014-12-30 08:07:49 +00:00
}
}
func TestShuffle(t *testing.T) {
q := newJobQueue()
q.Push("f1", 0, time.Time{})
q.Push("f2", 0, time.Time{})
q.Push("f3", 0, time.Time{})
q.Push("f4", 0, time.Time{})
// This test will fail once in eight million times (1 / (4!)^5) :)
for i := 0; i < 5; i++ {
q.Shuffle()
_, queued, _ := q.Jobs(1, 100)
if l := len(queued); l != 4 {
t.Fatalf("Weird length %d returned from jobs(1, 100)", l)
}
t.Logf("%v", queued)
if _, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
// The queue was shuffled
return
}
}
t.Error("Queue was not shuffled after five attempts.")
}
func TestSortBySize(t *testing.T) {
q := newJobQueue()
q.Push("f1", 20, time.Time{})
q.Push("f2", 40, time.Time{})
q.Push("f3", 30, time.Time{})
q.Push("f4", 10, time.Time{})
q.SortSmallestFirst()
_, actual, _ := q.Jobs(1, 100)
if l := len(actual); l != 4 {
t.Fatalf("Weird length %d returned from jobs(1, 100)", l)
}
expected := []string{"f4", "f1", "f3", "f2"}
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("SortSmallestFirst() diff:\n%s", diff)
}
q.SortLargestFirst()
_, actual, _ = q.Jobs(1, 100)
if l := len(actual); l != 4 {
t.Fatalf("Weird length %d returned from jobs(1, 100)", l)
}
expected = []string{"f2", "f3", "f1", "f4"}
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("SortLargestFirst() diff:\n%s", diff)
}
}
func TestSortByAge(t *testing.T) {
q := newJobQueue()
q.Push("f1", 0, time.Unix(20, 0))
q.Push("f2", 0, time.Unix(40, 0))
q.Push("f3", 0, time.Unix(30, 0))
q.Push("f4", 0, time.Unix(10, 0))
q.SortOldestFirst()
_, actual, _ := q.Jobs(1, 100)
if l := len(actual); l != 4 {
t.Fatalf("Weird length %d returned from jobs(1, 100)", l)
}
expected := []string{"f4", "f1", "f3", "f2"}
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("SortOldestFirst() diff:\n%s", diff)
}
q.SortNewestFirst()
_, actual, _ = q.Jobs(1, 100)
if l := len(actual); l != 4 {
t.Fatalf("Weird length %d returned from jobs(1, 100)", l)
}
expected = []string{"f2", "f3", "f1", "f4"}
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
2016-03-06 20:32:10 +00:00
t.Errorf("SortNewestFirst() diff:\n%s", diff)
}
}
2014-12-30 08:07:49 +00:00
func BenchmarkJobQueueBump(b *testing.B) {
files := genFiles(10000)
2014-12-30 08:07:49 +00:00
2014-12-30 08:35:21 +00:00
q := newJobQueue()
for _, f := range files {
q.Push(f.Name, 0, time.Time{})
2014-12-30 08:07:49 +00:00
}
rng := rand.New(rand.NewSource(int64(b.N)))
2014-12-30 08:07:49 +00:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := rng.Intn(len(files))
q.BringToFront(files[r].Name)
2014-12-30 08:07:49 +00:00
}
}
func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
files := genFiles(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
2014-12-30 08:35:21 +00:00
q := newJobQueue()
for _, f := range files {
q.Push(f.Name, 0, time.Time{})
2014-12-30 08:07:49 +00:00
}
for range files {
n, _ := q.Pop()
2014-12-30 08:07:49 +00:00
q.Done(n)
}
}
}
func TestQueuePagination(t *testing.T) {
q := newJobQueue()
// Ten random actions
names := make([]string, 10)
for i := 0; i < 10; i++ {
names[i] = fmt.Sprint("f", i)
q.Push(names[i], 0, time.Time{})
}
progress, queued, skip := q.Jobs(1, 100)
if len(progress) != 0 || len(queued) != 10 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
progress, queued, skip = q.Jobs(1, 5)
if len(progress) != 0 || len(queued) != 5 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(queued, names[:5]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[:5])
}
progress, queued, skip = q.Jobs(2, 5)
if len(progress) != 0 || len(queued) != 5 || skip != 5 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(queued, names[5:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[5:])
}
progress, queued, skip = q.Jobs(2, 7)
if len(progress) != 0 || len(queued) != 3 || skip != 7 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(queued, names[7:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[7:])
}
progress, queued, skip = q.Jobs(3, 5)
if len(progress) != 0 || len(queued) != 0 || skip != 10 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
n, ok := q.Pop()
if !ok || n != names[0] {
t.Fatal("Wrong element")
}
progress, queued, skip = q.Jobs(1, 100)
if len(progress) != 1 || len(queued) != 9 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
progress, queued, skip = q.Jobs(1, 5)
if len(progress) != 1 || len(queued) != 4 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(progress, names[:1]) {
t.Errorf("Wrong elements in progress, got %v, expected %v", progress, names[:1])
} else if !slices.Equal(queued, names[1:5]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[1:5])
}
progress, queued, skip = q.Jobs(2, 5)
if len(progress) != 0 || len(queued) != 5 || skip != 5 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(queued, names[5:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[5:])
}
progress, queued, skip = q.Jobs(2, 7)
if len(progress) != 0 || len(queued) != 3 || skip != 7 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(queued, names[7:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[7:])
}
progress, queued, skip = q.Jobs(3, 5)
if len(progress) != 0 || len(queued) != 0 || skip != 10 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
for i := 1; i < 8; i++ {
n, ok := q.Pop()
if !ok || n != names[i] {
t.Fatal("Wrong element")
}
}
progress, queued, skip = q.Jobs(1, 100)
if len(progress) != 8 || len(queued) != 2 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
progress, queued, skip = q.Jobs(1, 5)
if len(progress) != 5 || len(queued) != 0 || skip != 0 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(progress, names[:5]) {
t.Errorf("Wrong elements in progress, got %v, expected %v", progress, names[:5])
}
progress, queued, skip = q.Jobs(2, 5)
if len(progress) != 3 || len(queued) != 2 || skip != 5 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(progress, names[5:8]) {
t.Errorf("Wrong elements in progress, got %v, expected %v", progress, names[5:8])
} else if !slices.Equal(queued, names[8:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[8:])
}
progress, queued, skip = q.Jobs(2, 7)
if len(progress) != 1 || len(queued) != 2 || skip != 7 {
t.Error("Wrong length", len(progress), len(queued), 0)
} else if !slices.Equal(progress, names[7:8]) {
t.Errorf("Wrong elements in progress, got %v, expected %v", progress, names[7:8])
} else if !slices.Equal(queued, names[8:]) {
t.Errorf("Wrong elements in queued, got %v, expected %v", queued, names[8:])
}
progress, queued, skip = q.Jobs(3, 5)
if len(progress) != 0 || len(queued) != 0 || skip != 10 {
t.Error("Wrong length", len(progress), len(queued), 0)
}
}