queue_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "testing"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. )
  21. var (
  22. f1 = &protocol.FileInfo{Name: "f1"}
  23. f2 = &protocol.FileInfo{Name: "f2"}
  24. f3 = &protocol.FileInfo{Name: "f3"}
  25. f4 = &protocol.FileInfo{Name: "f4"}
  26. f5 = &protocol.FileInfo{Name: "f5"}
  27. )
  28. func TestJobQueue(t *testing.T) {
  29. // Some random actions
  30. q := NewJobQueue()
  31. q.Push(f1)
  32. q.Push(f2)
  33. q.Push(f3)
  34. q.Push(f4)
  35. progress, queued := q.Jobs()
  36. if len(progress) != 0 || len(queued) != 4 {
  37. t.Fatal("Wrong length")
  38. }
  39. for i := 1; i < 5; i++ {
  40. n := q.Pop()
  41. if n == nil || n.Name != fmt.Sprintf("f%d", i) {
  42. t.Fatal("Wrong element")
  43. }
  44. progress, queued = q.Jobs()
  45. if len(progress) != 1 || len(queued) != 3 {
  46. t.Fatal("Wrong length")
  47. }
  48. q.Done(n)
  49. progress, queued = q.Jobs()
  50. if len(progress) != 0 || len(queued) != 3 {
  51. t.Fatal("Wrong length", len(progress), len(queued))
  52. }
  53. q.Push(n)
  54. progress, queued = q.Jobs()
  55. if len(progress) != 0 || len(queued) != 4 {
  56. t.Fatal("Wrong length")
  57. }
  58. q.Done(f5) // Does not exist
  59. progress, queued = q.Jobs()
  60. if len(progress) != 0 || len(queued) != 4 {
  61. t.Fatal("Wrong length")
  62. }
  63. }
  64. if len(q.progress) > 0 || len(q.lookup) != 4 || q.queued.Len() != 4 {
  65. t.Fatal("Wrong length")
  66. }
  67. for i := 4; i > 0; i-- {
  68. progress, queued = q.Jobs()
  69. if len(progress) != 4-i || len(queued) != i {
  70. t.Fatal("Wrong length")
  71. }
  72. s := fmt.Sprintf("f%d", i)
  73. q.Bump(s)
  74. progress, queued = q.Jobs()
  75. if len(progress) != 4-i || len(queued) != i {
  76. t.Fatal("Wrong length")
  77. }
  78. n := q.Pop()
  79. if n == nil || n.Name != s {
  80. t.Fatal("Wrong element")
  81. }
  82. progress, queued = q.Jobs()
  83. if len(progress) != 5-i || len(queued) != i-1 {
  84. t.Fatal("Wrong length")
  85. }
  86. q.Done(f5) // Does not exist
  87. progress, queued = q.Jobs()
  88. if len(progress) != 5-i || len(queued) != i-1 {
  89. t.Fatal("Wrong length")
  90. }
  91. }
  92. if len(q.progress) != 4 || q.Pop() != nil || len(q.lookup) != 0 {
  93. t.Fatal("Wrong length")
  94. }
  95. q.Done(f1)
  96. q.Done(f2)
  97. q.Done(f3)
  98. q.Done(f4)
  99. q.Done(f5) // Does not exist
  100. if len(q.progress) != 0 || q.Pop() != nil || len(q.lookup) != 0 {
  101. t.Fatal("Wrong length")
  102. }
  103. progress, queued = q.Jobs()
  104. if len(progress) != 0 || len(queued) != 0 {
  105. t.Fatal("Wrong length")
  106. }
  107. q.Bump("")
  108. q.Done(f5) // Does not exist
  109. progress, queued = q.Jobs()
  110. if len(progress) != 0 || len(queued) != 0 {
  111. t.Fatal("Wrong length")
  112. }
  113. }