queue_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestJobQueue(t *testing.T) {
  22. // Some random actions
  23. q := newJobQueue()
  24. q.Push("f1")
  25. q.Push("f2")
  26. q.Push("f3")
  27. q.Push("f4")
  28. progress, queued := q.Jobs()
  29. if len(progress) != 0 || len(queued) != 4 {
  30. t.Fatal("Wrong length")
  31. }
  32. for i := 1; i < 5; i++ {
  33. n, ok := q.Pop()
  34. if !ok || n != fmt.Sprintf("f%d", i) {
  35. t.Fatal("Wrong element")
  36. }
  37. progress, queued = q.Jobs()
  38. if len(progress) != 1 || len(queued) != 3 {
  39. t.Log(progress)
  40. t.Log(queued)
  41. t.Fatal("Wrong length")
  42. }
  43. q.Done(n)
  44. progress, queued = q.Jobs()
  45. if len(progress) != 0 || len(queued) != 3 {
  46. t.Fatal("Wrong length", len(progress), len(queued))
  47. }
  48. q.Push(n)
  49. progress, queued = q.Jobs()
  50. if len(progress) != 0 || len(queued) != 4 {
  51. t.Fatal("Wrong length")
  52. }
  53. q.Done("f5") // Does not exist
  54. progress, queued = q.Jobs()
  55. if len(progress) != 0 || len(queued) != 4 {
  56. t.Fatal("Wrong length")
  57. }
  58. }
  59. if len(q.progress) > 0 || len(q.queued) != 4 {
  60. t.Fatal("Wrong length")
  61. }
  62. for i := 4; i > 0; i-- {
  63. progress, queued = q.Jobs()
  64. if len(progress) != 4-i || len(queued) != i {
  65. t.Fatal("Wrong length")
  66. }
  67. s := fmt.Sprintf("f%d", i)
  68. q.BringToFront(s)
  69. progress, queued = q.Jobs()
  70. if len(progress) != 4-i || len(queued) != i {
  71. t.Fatal("Wrong length")
  72. }
  73. n, ok := q.Pop()
  74. if !ok || n != s {
  75. t.Fatal("Wrong element")
  76. }
  77. progress, queued = q.Jobs()
  78. if len(progress) != 5-i || len(queued) != i-1 {
  79. t.Fatal("Wrong length")
  80. }
  81. q.Done("f5") // Does not exist
  82. progress, queued = q.Jobs()
  83. if len(progress) != 5-i || len(queued) != i-1 {
  84. t.Fatal("Wrong length")
  85. }
  86. }
  87. _, ok := q.Pop()
  88. if len(q.progress) != 4 || ok {
  89. t.Fatal("Wrong length")
  90. }
  91. q.Done("f1")
  92. q.Done("f2")
  93. q.Done("f3")
  94. q.Done("f4")
  95. q.Done("f5") // Does not exist
  96. _, ok = q.Pop()
  97. if len(q.progress) != 0 || ok {
  98. t.Fatal("Wrong length")
  99. }
  100. progress, queued = q.Jobs()
  101. if len(progress) != 0 || len(queued) != 0 {
  102. t.Fatal("Wrong length")
  103. }
  104. q.BringToFront("")
  105. q.Done("f5") // Does not exist
  106. progress, queued = q.Jobs()
  107. if len(progress) != 0 || len(queued) != 0 {
  108. t.Fatal("Wrong length")
  109. }
  110. }
  111. func TestBringToFront(t *testing.T) {
  112. q := newJobQueue()
  113. q.Push("f1")
  114. q.Push("f2")
  115. q.Push("f3")
  116. q.Push("f4")
  117. _, queued := q.Jobs()
  118. if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
  119. t.Errorf("Incorrect order %v at start", queued)
  120. }
  121. q.BringToFront("f1") // corner case: does nothing
  122. _, queued = q.Jobs()
  123. if !reflect.DeepEqual(queued, []string{"f1", "f2", "f3", "f4"}) {
  124. t.Errorf("Incorrect order %v", queued)
  125. }
  126. q.BringToFront("f3")
  127. _, queued = q.Jobs()
  128. if !reflect.DeepEqual(queued, []string{"f3", "f1", "f2", "f4"}) {
  129. t.Errorf("Incorrect order %v", queued)
  130. }
  131. q.BringToFront("f2")
  132. _, queued = q.Jobs()
  133. if !reflect.DeepEqual(queued, []string{"f2", "f3", "f1", "f4"}) {
  134. t.Errorf("Incorrect order %v", queued)
  135. }
  136. q.BringToFront("f4") // corner case: last element
  137. _, queued = q.Jobs()
  138. if !reflect.DeepEqual(queued, []string{"f4", "f2", "f3", "f1"}) {
  139. t.Errorf("Incorrect order %v", queued)
  140. }
  141. }
  142. func BenchmarkJobQueueBump(b *testing.B) {
  143. files := genFiles(b.N)
  144. q := newJobQueue()
  145. for _, f := range files {
  146. q.Push(f.Name)
  147. }
  148. b.ResetTimer()
  149. for i := 0; i < b.N; i++ {
  150. q.BringToFront(files[i].Name)
  151. }
  152. }
  153. func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
  154. files := genFiles(10000)
  155. b.ResetTimer()
  156. for i := 0; i < b.N; i++ {
  157. q := newJobQueue()
  158. for _, f := range files {
  159. q.Push(f.Name)
  160. }
  161. for _ = range files {
  162. n, _ := q.Pop()
  163. q.Done(n)
  164. }
  165. }
  166. }