devicedownloadstate_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. )
  11. func TestDeviceDownloadState(t *testing.T) {
  12. v1 := (protocol.Vector{}).Update(0)
  13. v2 := (protocol.Vector{}).Update(1)
  14. // file 1 version 1 part 1
  15. f1v1p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int{0, 1, 2}}
  16. f1v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int{3, 4, 5}}
  17. f1v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeForget, Name: "f1", Version: v1, BlockIndexes: nil}
  18. f1v2p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int{10, 11, 12}}
  19. f1v2p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int{13, 14, 15}}
  20. f1v2del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeForget, Name: "f1", Version: v2, BlockIndexes: nil}
  21. f2v1p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int{20, 21, 22}}
  22. f2v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int{23, 24, 25}}
  23. f2v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.FileDownloadProgressUpdateTypeForget, Name: "f2", Version: v1, BlockIndexes: nil}
  24. tests := []struct {
  25. updates []protocol.FileDownloadProgressUpdate
  26. shouldHaveIndexesFrom []protocol.FileDownloadProgressUpdate
  27. shouldNotHaveIndexesFrom []protocol.FileDownloadProgressUpdate
  28. }{
  29. { // 1
  30. []protocol.FileDownloadProgressUpdate{f1v1p1},
  31. []protocol.FileDownloadProgressUpdate{f1v1p1},
  32. []protocol.FileDownloadProgressUpdate{f1v1p2, f1v2p1, f1v2p2},
  33. },
  34. { // 2
  35. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
  36. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
  37. []protocol.FileDownloadProgressUpdate{f1v2p1, f1v2p2},
  38. },
  39. { // 3
  40. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v1del},
  41. nil,
  42. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v2p2},
  43. },
  44. { // 4
  45. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2del},
  46. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
  47. []protocol.FileDownloadProgressUpdate{f1v2p1, f1v2p2},
  48. },
  49. { // 5
  50. // v2 replaces old v1 data
  51. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
  52. []protocol.FileDownloadProgressUpdate{f1v2p1},
  53. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
  54. },
  55. { // 6
  56. // v1 delete on v2 data does nothing
  57. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v1del},
  58. []protocol.FileDownloadProgressUpdate{f1v2p1},
  59. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
  60. },
  61. { // 7
  62. // v2 replacees v1, v2 gets deleted, and v2 part 2 gets added.
  63. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v2del, f1v2p2},
  64. []protocol.FileDownloadProgressUpdate{f1v2p2},
  65. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
  66. },
  67. // Multiple files in one go
  68. { // 8
  69. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  70. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  71. []protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
  72. },
  73. { // 9
  74. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1, f2v1del},
  75. []protocol.FileDownloadProgressUpdate{f1v1p1},
  76. []protocol.FileDownloadProgressUpdate{f2v1p1, f2v1p1},
  77. },
  78. { // 10
  79. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1del, f2v1p1},
  80. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  81. []protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
  82. },
  83. }
  84. for i, test := range tests {
  85. s := newDeviceDownloadState()
  86. s.Update("folder", test.updates)
  87. for _, expected := range test.shouldHaveIndexesFrom {
  88. for _, n := range expected.BlockIndexes {
  89. if !s.Has("folder", expected.Name, expected.Version, n) {
  90. t.Error("Test", i+1, "error:", expected.Name, expected.Version, "missing", n)
  91. }
  92. }
  93. }
  94. for _, unexpected := range test.shouldNotHaveIndexesFrom {
  95. for _, n := range unexpected.BlockIndexes {
  96. if s.Has("folder", unexpected.Name, unexpected.Version, n) {
  97. t.Error("Test", i+1, "error:", unexpected.Name, unexpected.Version, "has extra", n)
  98. }
  99. }
  100. }
  101. }
  102. }