syncthing/internal/model/queue_test.go

192 lines
3.8 KiB
Go
Raw Normal View History

2014-12-01 22:33:40 +01:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 21:36:35 +01: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 http://mozilla.org/MPL/2.0/.
2014-12-01 22:33:40 +01:00
package model
import (
"fmt"
2014-12-30 09:35:21 +01:00
"reflect"
2014-12-01 22:33:40 +01:00
"testing"
)
func TestJobQueue(t *testing.T) {
// Some random actions
2014-12-30 09:35:21 +01:00
q := newJobQueue()
q.Push("f1")
q.Push("f2")
q.Push("f3")
q.Push("f4")
2014-12-01 22:33:40 +01:00
progress, queued := q.Jobs()
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length")
}
for i := 1; i < 5; i++ {
n, ok := q.Pop()
if !ok || n != fmt.Sprintf("f%d", i) {
2014-12-01 22:33:40 +01:00
t.Fatal("Wrong element")
}
progress, queued = q.Jobs()
if len(progress) != 1 || len(queued) != 3 {
2014-12-30 09:35:21 +01:00
t.Log(progress)
t.Log(queued)
2014-12-01 22:33:40 +01: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))
}
q.Push(n)
progress, queued = q.Jobs()
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length")
}
q.Done("f5") // Does not exist
2014-12-01 22:33:40 +01:00
progress, queued = q.Jobs()
if len(progress) != 0 || len(queued) != 4 {
t.Fatal("Wrong length")
}
}
if len(q.progress) > 0 || len(q.queued) != 4 {
2014-12-01 22:33:40 +01: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 09:35:21 +01:00
q.BringToFront(s)
2014-12-01 22:33:40 +01:00
progress, queued = q.Jobs()
if len(progress) != 4-i || len(queued) != i {
t.Fatal("Wrong length")
}
n, ok := q.Pop()
if !ok || n != s {
2014-12-01 22:33:40 +01:00
t.Fatal("Wrong element")
}
progress, queued = q.Jobs()
if len(progress) != 5-i || len(queued) != i-1 {
t.Fatal("Wrong length")
}
q.Done("f5") // Does not exist
2014-12-01 22:33:40 +01:00
progress, queued = q.Jobs()
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 22:33:40 +01: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 22:33:40 +01:00
_, ok = q.Pop()
if len(q.progress) != 0 || ok {
2014-12-01 22:33:40 +01:00
t.Fatal("Wrong length")
}
progress, queued = q.Jobs()
if len(progress) != 0 || len(queued) != 0 {
t.Fatal("Wrong length")
}
2014-12-30 09:35:21 +01:00
q.BringToFront("")
q.Done("f5") // Does not exist
2014-12-01 22:33:40 +01:00
progress, queued = q.Jobs()
if len(progress) != 0 || len(queued) != 0 {
t.Fatal("Wrong length")
}
}
2014-12-30 09:07:49 +01:00
2014-12-30 09:35:21 +01:00
func TestBringToFront(t *testing.T) {
q := newJobQueue()
q.Push("f1")
q.Push("f2")
q.Push("f3")
q.Push("f4")
2014-12-30 09:07:49 +01:00
2014-12-30 09:35:21 +01:00
_, queued := q.Jobs()
if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
t.Errorf("Incorrect order %v at start", queued)
2014-12-30 09:07:49 +01:00
}
2014-12-30 09:35:21 +01:00
q.BringToFront("f1") // corner case: does nothing
2014-12-30 09:07:49 +01:00
2014-12-30 09:35:21 +01:00
_, queued = q.Jobs()
if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
t.Errorf("Incorrect order %v", queued)
2014-12-30 09:07:49 +01:00
}
2014-12-30 09:35:21 +01:00
q.BringToFront("f3")
_, queued = q.Jobs()
if !reflect.DeepEqual(queued, []string{"f3", "f1", "f2", "f4"}) {
t.Errorf("Incorrect order %v", queued)
2014-12-30 09:07:49 +01:00
}
2014-12-30 09:35:21 +01:00
q.BringToFront("f2")
2014-12-30 09:07:49 +01:00
2014-12-30 09:35:21 +01:00
_, queued = q.Jobs()
if !reflect.DeepEqual(queued, []string{"f2", "f3", "f1", "f4"}) {
t.Errorf("Incorrect order %v", queued)
2014-12-30 09:07:49 +01:00
}
2014-12-30 09:35:21 +01:00
q.BringToFront("f4") // corner case: last element
_, queued = q.Jobs()
if !reflect.DeepEqual(queued, []string{"f4", "f2", "f3", "f1"}) {
t.Errorf("Incorrect order %v", queued)
2014-12-30 09:07:49 +01:00
}
}
func BenchmarkJobQueueBump(b *testing.B) {
files := genFiles(b.N)
2014-12-30 09:35:21 +01:00
q := newJobQueue()
for _, f := range files {
q.Push(f.Name)
2014-12-30 09:07:49 +01:00
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2014-12-30 09:35:21 +01:00
q.BringToFront(files[i].Name)
2014-12-30 09:07:49 +01:00
}
}
func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
files := genFiles(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
2014-12-30 09:35:21 +01:00
q := newJobQueue()
for _, f := range files {
q.Push(f.Name)
2014-12-30 09:07:49 +01:00
}
for _ = range files {
n, _ := q.Pop()
2014-12-30 09:07:49 +01:00
q.Done(n)
}
}
}