2014-12-01 14:33:40 -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-12-01 14:33:40 -07:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2016-08-06 06:05:59 -07:00
|
|
|
"time"
|
2016-03-06 13:32:10 -07:00
|
|
|
|
|
|
|
"github.com/d4l3k/messagediff"
|
2014-12-01 14:33:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJobQueue(t *testing.T) {
|
|
|
|
// Some random actions
|
2014-12-30 01:35:21 -07:00
|
|
|
q := newJobQueue()
|
2016-08-06 06:05:59 -07:00
|
|
|
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 14:33:40 -07:00
|
|
|
|
|
|
|
progress, queued := q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 4 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < 5; i++ {
|
2014-12-30 01:31:34 -07:00
|
|
|
n, ok := q.Pop()
|
|
|
|
if !ok || n != fmt.Sprintf("f%d", i) {
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong element")
|
|
|
|
}
|
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 1 || len(queued) != 3 {
|
2014-12-30 01:35:21 -07:00
|
|
|
t.Log(progress)
|
|
|
|
t.Log(queued)
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
q.Done(n)
|
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 3 {
|
|
|
|
t.Fatal("Wrong length", len(progress), len(queued))
|
|
|
|
}
|
|
|
|
|
2016-08-06 06:05:59 -07:00
|
|
|
q.Push(n, 0, time.Time{})
|
2014-12-01 14:33:40 -07:00
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 4 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
q.Done("f5") // Does not exist
|
2014-12-01 14:33:40 -07:00
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 4 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:07:58 -07:00
|
|
|
if len(q.progress) > 0 || len(q.queued) != 4 {
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 4; i > 0; i-- {
|
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 4-i || len(queued) != i {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("f%d", i)
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront(s)
|
2014-12-01 14:33:40 -07:00
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 4-i || len(queued) != i {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
n, ok := q.Pop()
|
|
|
|
if !ok || n != s {
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong element")
|
|
|
|
}
|
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 5-i || len(queued) != i-1 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
q.Done("f5") // Does not exist
|
2014-12-01 14:33:40 -07:00
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 5-i || len(queued) != i-1 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
_, ok := q.Pop()
|
|
|
|
if len(q.progress) != 4 || ok {
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
q.Done("f1")
|
|
|
|
q.Done("f2")
|
|
|
|
q.Done("f3")
|
|
|
|
q.Done("f4")
|
|
|
|
q.Done("f5") // Does not exist
|
2014-12-01 14:33:40 -07:00
|
|
|
|
2014-12-30 01:31:34 -07:00
|
|
|
_, ok = q.Pop()
|
|
|
|
if len(q.progress) != 0 || ok {
|
2014-12-01 14:33:40 -07:00
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
|
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 0 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront("")
|
2014-12-30 01:31:34 -07:00
|
|
|
q.Done("f5") // Does not exist
|
2014-12-01 14:33:40 -07:00
|
|
|
progress, queued = q.Jobs()
|
|
|
|
if len(progress) != 0 || len(queued) != 0 {
|
|
|
|
t.Fatal("Wrong length")
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 01:07:49 -07:00
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
func TestBringToFront(t *testing.T) {
|
|
|
|
q := newJobQueue()
|
2016-08-06 06:05:59 -07:00
|
|
|
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 01:07:49 -07:00
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
_, queued := q.Jobs()
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("Order does not match. Diff:\n%s", diff)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront("f1") // corner case: does nothing
|
2014-12-30 01:07:49 -07:00
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
_, queued = q.Jobs()
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("Order does not match. Diff:\n%s", diff)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront("f3")
|
|
|
|
|
|
|
|
_, queued = q.Jobs()
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff([]string{"f3", "f1", "f2", "f4"}, queued); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("Order does not match. Diff:\n%s", diff)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront("f2")
|
2014-12-30 01:07:49 -07:00
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
_, queued = q.Jobs()
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff([]string{"f2", "f3", "f1", "f4"}, queued); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("Order does not match. Diff:\n%s", diff)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront("f4") // corner case: last element
|
|
|
|
|
|
|
|
_, queued = q.Jobs()
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff([]string{"f4", "f2", "f3", "f1"}, queued); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("Order does not match. Diff:\n%s", diff)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:13:53 -07:00
|
|
|
func TestShuffle(t *testing.T) {
|
|
|
|
q := newJobQueue()
|
2016-08-06 06:05:59 -07:00
|
|
|
q.Push("f1", 0, time.Time{})
|
|
|
|
q.Push("f2", 0, time.Time{})
|
|
|
|
q.Push("f3", 0, time.Time{})
|
|
|
|
q.Push("f4", 0, time.Time{})
|
2015-04-24 22:13:53 -07:00
|
|
|
|
|
|
|
// This test will fail once in eight million times (1 / (4!)^5) :)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
q.Shuffle()
|
|
|
|
_, queued := q.Jobs()
|
|
|
|
if l := len(queued); l != 4 {
|
|
|
|
t.Fatalf("Weird length %d returned from Jobs()", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("%v", queued)
|
2016-03-17 00:03:29 -07:00
|
|
|
if _, equal := messagediff.PrettyDiff([]string{"f1", "f2", "f3", "f4"}, queued); !equal {
|
2015-04-24 22:13:53 -07:00
|
|
|
// The queue was shuffled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("Queue was not shuffled after five attempts.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortBySize(t *testing.T) {
|
|
|
|
q := newJobQueue()
|
2016-08-06 06:05:59 -07:00
|
|
|
q.Push("f1", 20, time.Time{})
|
|
|
|
q.Push("f2", 40, time.Time{})
|
|
|
|
q.Push("f3", 30, time.Time{})
|
|
|
|
q.Push("f4", 10, time.Time{})
|
2015-04-24 22:13:53 -07:00
|
|
|
|
|
|
|
q.SortSmallestFirst()
|
|
|
|
|
|
|
|
_, actual := q.Jobs()
|
|
|
|
if l := len(actual); l != 4 {
|
|
|
|
t.Fatalf("Weird length %d returned from Jobs()", l)
|
|
|
|
}
|
|
|
|
expected := []string{"f4", "f1", "f3", "f2"}
|
|
|
|
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("SortSmallestFirst() diff:\n%s", diff)
|
2015-04-24 22:13:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
q.SortLargestFirst()
|
|
|
|
|
|
|
|
_, actual = q.Jobs()
|
|
|
|
if l := len(actual); l != 4 {
|
|
|
|
t.Fatalf("Weird length %d returned from Jobs()", l)
|
|
|
|
}
|
|
|
|
expected = []string{"f2", "f3", "f1", "f4"}
|
|
|
|
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("SortLargestFirst() diff:\n%s", diff)
|
2015-04-24 22:13:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortByAge(t *testing.T) {
|
|
|
|
q := newJobQueue()
|
2016-08-06 06:05:59 -07:00
|
|
|
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))
|
2015-04-24 22:13:53 -07:00
|
|
|
|
|
|
|
q.SortOldestFirst()
|
|
|
|
|
|
|
|
_, actual := q.Jobs()
|
|
|
|
if l := len(actual); l != 4 {
|
|
|
|
t.Fatalf("Weird length %d returned from Jobs()", l)
|
|
|
|
}
|
|
|
|
expected := []string{"f4", "f1", "f3", "f2"}
|
|
|
|
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("SortOldestFirst() diff:\n%s", diff)
|
2015-04-24 22:13:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
q.SortNewestFirst()
|
|
|
|
|
|
|
|
_, actual = q.Jobs()
|
|
|
|
if l := len(actual); l != 4 {
|
|
|
|
t.Fatalf("Weird length %d returned from Jobs()", l)
|
|
|
|
}
|
|
|
|
expected = []string{"f2", "f3", "f1", "f4"}
|
|
|
|
|
2016-03-17 00:03:29 -07:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 13:32:10 -07:00
|
|
|
t.Errorf("SortNewestFirst() diff:\n%s", diff)
|
2015-04-24 22:13:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:07:49 -07:00
|
|
|
func BenchmarkJobQueueBump(b *testing.B) {
|
|
|
|
files := genFiles(b.N)
|
|
|
|
|
2014-12-30 01:35:21 -07:00
|
|
|
q := newJobQueue()
|
2014-12-30 01:31:34 -07:00
|
|
|
for _, f := range files {
|
2016-08-06 06:05:59 -07:00
|
|
|
q.Push(f.Name, 0, time.Time{})
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2014-12-30 01:35:21 -07:00
|
|
|
q.BringToFront(files[i].Name)
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
|
|
|
|
files := genFiles(10000)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2014-12-30 01:35:21 -07:00
|
|
|
q := newJobQueue()
|
2014-12-30 01:31:34 -07:00
|
|
|
for _, f := range files {
|
2016-08-06 06:05:59 -07:00
|
|
|
q.Push(f.Name, 0, time.Time{})
|
2014-12-30 01:07:49 -07:00
|
|
|
}
|
2016-08-14 23:37:32 -07:00
|
|
|
for range files {
|
2014-12-30 01:31:34 -07:00
|
|
|
n, _ := q.Pop()
|
2014-12-30 01:07:49 -07:00
|
|
|
q.Done(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|