queue_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "reflect"
  10. "testing"
  11. )
  12. func TestJobQueue(t *testing.T) {
  13. // Some random actions
  14. q := newJobQueue()
  15. q.Push("f1")
  16. q.Push("f2")
  17. q.Push("f3")
  18. q.Push("f4")
  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)
  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")
  105. q.Push("f2")
  106. q.Push("f3")
  107. q.Push("f4")
  108. _, queued := q.Jobs()
  109. if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
  110. t.Errorf("Incorrect order %v at start", queued)
  111. }
  112. q.BringToFront("f1") // corner case: does nothing
  113. _, queued = q.Jobs()
  114. if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
  115. t.Errorf("Incorrect order %v", queued)
  116. }
  117. q.BringToFront("f3")
  118. _, queued = q.Jobs()
  119. if !reflect.DeepEqual(queued, []string{"f3", "f1", "f2", "f4"}) {
  120. t.Errorf("Incorrect order %v", queued)
  121. }
  122. q.BringToFront("f2")
  123. _, queued = q.Jobs()
  124. if !reflect.DeepEqual(queued, []string{"f2", "f3", "f1", "f4"}) {
  125. t.Errorf("Incorrect order %v", queued)
  126. }
  127. q.BringToFront("f4") // corner case: last element
  128. _, queued = q.Jobs()
  129. if !reflect.DeepEqual(queued, []string{"f4", "f2", "f3", "f1"}) {
  130. t.Errorf("Incorrect order %v", queued)
  131. }
  132. }
  133. func BenchmarkJobQueueBump(b *testing.B) {
  134. files := genFiles(b.N)
  135. q := newJobQueue()
  136. for _, f := range files {
  137. q.Push(f.Name)
  138. }
  139. b.ResetTimer()
  140. for i := 0; i < b.N; i++ {
  141. q.BringToFront(files[i].Name)
  142. }
  143. }
  144. func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
  145. files := genFiles(10000)
  146. b.ResetTimer()
  147. for i := 0; i < b.N; i++ {
  148. q := newJobQueue()
  149. for _, f := range files {
  150. q.Push(f.Name)
  151. }
  152. for _ = range files {
  153. n, _ := q.Pop()
  154. q.Done(n)
  155. }
  156. }
  157. }