queue_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "fmt"
  9. "testing"
  10. "github.com/d4l3k/messagediff"
  11. )
  12. func TestJobQueue(t *testing.T) {
  13. // Some random actions
  14. q := newJobQueue()
  15. q.Push("f1", 0, 0)
  16. q.Push("f2", 0, 0)
  17. q.Push("f3", 0, 0)
  18. q.Push("f4", 0, 0)
  19. progress, queued := q.Jobs()
  20. if len(progress) != 0 || len(queued) != 4 {
  21. t.Fatal("Wrong length")
  22. }
  23. for i := 1; i < 5; i++ {
  24. n, ok := q.Pop()
  25. if !ok || n != fmt.Sprintf("f%d", i) {
  26. t.Fatal("Wrong element")
  27. }
  28. progress, queued = q.Jobs()
  29. if len(progress) != 1 || len(queued) != 3 {
  30. t.Log(progress)
  31. t.Log(queued)
  32. t.Fatal("Wrong length")
  33. }
  34. q.Done(n)
  35. progress, queued = q.Jobs()
  36. if len(progress) != 0 || len(queued) != 3 {
  37. t.Fatal("Wrong length", len(progress), len(queued))
  38. }
  39. q.Push(n, 0, 0)
  40. progress, queued = q.Jobs()
  41. if len(progress) != 0 || len(queued) != 4 {
  42. t.Fatal("Wrong length")
  43. }
  44. q.Done("f5") // Does not exist
  45. progress, queued = q.Jobs()
  46. if len(progress) != 0 || len(queued) != 4 {
  47. t.Fatal("Wrong length")
  48. }
  49. }
  50. if len(q.progress) > 0 || len(q.queued) != 4 {
  51. t.Fatal("Wrong length")
  52. }
  53. for i := 4; i > 0; i-- {
  54. progress, queued = q.Jobs()
  55. if len(progress) != 4-i || len(queued) != i {
  56. t.Fatal("Wrong length")
  57. }
  58. s := fmt.Sprintf("f%d", i)
  59. q.BringToFront(s)
  60. progress, queued = q.Jobs()
  61. if len(progress) != 4-i || len(queued) != i {
  62. t.Fatal("Wrong length")
  63. }
  64. n, ok := q.Pop()
  65. if !ok || n != s {
  66. t.Fatal("Wrong element")
  67. }
  68. progress, queued = q.Jobs()
  69. if len(progress) != 5-i || len(queued) != i-1 {
  70. t.Fatal("Wrong length")
  71. }
  72. q.Done("f5") // Does not exist
  73. progress, queued = q.Jobs()
  74. if len(progress) != 5-i || len(queued) != i-1 {
  75. t.Fatal("Wrong length")
  76. }
  77. }
  78. _, ok := q.Pop()
  79. if len(q.progress) != 4 || ok {
  80. t.Fatal("Wrong length")
  81. }
  82. q.Done("f1")
  83. q.Done("f2")
  84. q.Done("f3")
  85. q.Done("f4")
  86. q.Done("f5") // Does not exist
  87. _, ok = q.Pop()
  88. if len(q.progress) != 0 || ok {
  89. t.Fatal("Wrong length")
  90. }
  91. progress, queued = q.Jobs()
  92. if len(progress) != 0 || len(queued) != 0 {
  93. t.Fatal("Wrong length")
  94. }
  95. q.BringToFront("")
  96. q.Done("f5") // Does not exist
  97. progress, queued = q.Jobs()
  98. if len(progress) != 0 || len(queued) != 0 {
  99. t.Fatal("Wrong length")
  100. }
  101. }
  102. func TestBringToFront(t *testing.T) {
  103. q := newJobQueue()
  104. q.Push("f1", 0, 0)
  105. q.Push("f2", 0, 0)
  106. q.Push("f3", 0, 0)
  107. q.Push("f4", 0, 0)
  108. _, queued := q.Jobs()
  109. if diff, equal := messagediff.PrettyDiff(queued, []string{"f1", "f2", "f3", "f4"}); !equal {
  110. t.Errorf("Order does not match. Diff:\n%s", diff)
  111. }
  112. q.BringToFront("f1") // corner case: does nothing
  113. _, queued = q.Jobs()
  114. if diff, equal := messagediff.PrettyDiff(queued, []string{"f1", "f2", "f3", "f4"}); !equal {
  115. t.Errorf("Order does not match. Diff:\n%s", diff)
  116. }
  117. q.BringToFront("f3")
  118. _, queued = q.Jobs()
  119. if diff, equal := messagediff.PrettyDiff(queued, []string{"f3", "f1", "f2", "f4"}); !equal {
  120. t.Errorf("Order does not match. Diff:\n%s", diff)
  121. }
  122. q.BringToFront("f2")
  123. _, queued = q.Jobs()
  124. if diff, equal := messagediff.PrettyDiff(queued, []string{"f2", "f3", "f1", "f4"}); !equal {
  125. t.Errorf("Order does not match. Diff:\n%s", diff)
  126. }
  127. q.BringToFront("f4") // corner case: last element
  128. _, queued = q.Jobs()
  129. if diff, equal := messagediff.PrettyDiff(queued, []string{"f4", "f2", "f3", "f1"}); !equal {
  130. t.Errorf("Order does not match. Diff:\n%s", diff)
  131. }
  132. }
  133. func TestShuffle(t *testing.T) {
  134. q := newJobQueue()
  135. q.Push("f1", 0, 0)
  136. q.Push("f2", 0, 0)
  137. q.Push("f3", 0, 0)
  138. q.Push("f4", 0, 0)
  139. // This test will fail once in eight million times (1 / (4!)^5) :)
  140. for i := 0; i < 5; i++ {
  141. q.Shuffle()
  142. _, queued := q.Jobs()
  143. if l := len(queued); l != 4 {
  144. t.Fatalf("Weird length %d returned from Jobs()", l)
  145. }
  146. t.Logf("%v", queued)
  147. if _, equal := messagediff.PrettyDiff(queued, []string{"f1", "f2", "f3", "f4"}); !equal {
  148. // The queue was shuffled
  149. return
  150. }
  151. }
  152. t.Error("Queue was not shuffled after five attempts.")
  153. }
  154. func TestSortBySize(t *testing.T) {
  155. q := newJobQueue()
  156. q.Push("f1", 20, 0)
  157. q.Push("f2", 40, 0)
  158. q.Push("f3", 30, 0)
  159. q.Push("f4", 10, 0)
  160. q.SortSmallestFirst()
  161. _, actual := q.Jobs()
  162. if l := len(actual); l != 4 {
  163. t.Fatalf("Weird length %d returned from Jobs()", l)
  164. }
  165. expected := []string{"f4", "f1", "f3", "f2"}
  166. if diff, equal := messagediff.PrettyDiff(actual, expected); !equal {
  167. t.Errorf("SortSmallestFirst() diff:\n%s", diff)
  168. }
  169. q.SortLargestFirst()
  170. _, actual = q.Jobs()
  171. if l := len(actual); l != 4 {
  172. t.Fatalf("Weird length %d returned from Jobs()", l)
  173. }
  174. expected = []string{"f2", "f3", "f1", "f4"}
  175. if diff, equal := messagediff.PrettyDiff(actual, expected); !equal {
  176. t.Errorf("SortLargestFirst() diff:\n%s", diff)
  177. }
  178. }
  179. func TestSortByAge(t *testing.T) {
  180. q := newJobQueue()
  181. q.Push("f1", 0, 20)
  182. q.Push("f2", 0, 40)
  183. q.Push("f3", 0, 30)
  184. q.Push("f4", 0, 10)
  185. q.SortOldestFirst()
  186. _, actual := q.Jobs()
  187. if l := len(actual); l != 4 {
  188. t.Fatalf("Weird length %d returned from Jobs()", l)
  189. }
  190. expected := []string{"f4", "f1", "f3", "f2"}
  191. if diff, equal := messagediff.PrettyDiff(actual, expected); !equal {
  192. t.Errorf("SortOldestFirst() diff:\n%s", diff)
  193. }
  194. q.SortNewestFirst()
  195. _, actual = q.Jobs()
  196. if l := len(actual); l != 4 {
  197. t.Fatalf("Weird length %d returned from Jobs()", l)
  198. }
  199. expected = []string{"f2", "f3", "f1", "f4"}
  200. if diff, equal := messagediff.PrettyDiff(actual, expected); !equal {
  201. t.Errorf("SortNewestFirst() diff:\n%s", diff)
  202. }
  203. }
  204. func BenchmarkJobQueueBump(b *testing.B) {
  205. files := genFiles(b.N)
  206. q := newJobQueue()
  207. for _, f := range files {
  208. q.Push(f.Name, 0, 0)
  209. }
  210. b.ResetTimer()
  211. for i := 0; i < b.N; i++ {
  212. q.BringToFront(files[i].Name)
  213. }
  214. }
  215. func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
  216. files := genFiles(10000)
  217. b.ResetTimer()
  218. for i := 0; i < b.N; i++ {
  219. q := newJobQueue()
  220. for _, f := range files {
  221. q.Push(f.Name, 0, 0)
  222. }
  223. for _ = range files {
  224. n, _ := q.Pop()
  225. q.Done(n)
  226. }
  227. }
  228. }