devicedownloadstate_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.UpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int32{0, 1, 2}}
  16. f1v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v1, BlockIndexes: []int32{3, 4, 5}}
  17. f1v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, Name: "f1", Version: v1, BlockIndexes: nil}
  18. f1v2p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int32{10, 11, 12}}
  19. f1v2p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f1", Version: v2, BlockIndexes: []int32{13, 14, 15}}
  20. f1v2del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, Name: "f1", Version: v2, BlockIndexes: nil}
  21. f2v1p1 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int32{20, 21, 22}}
  22. f2v1p2 := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeAppend, Name: "f2", Version: v1, BlockIndexes: []int32{23, 24, 25}}
  23. f2v1del := protocol.FileDownloadProgressUpdate{UpdateType: protocol.UpdateTypeForget, 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. { //4
  44. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2del},
  45. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2},
  46. []protocol.FileDownloadProgressUpdate{f1v2p1, f1v2p2},
  47. },
  48. { //5
  49. // v2 replaces old v1 data
  50. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
  51. []protocol.FileDownloadProgressUpdate{f1v2p1},
  52. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
  53. },
  54. { //6
  55. // v1 delete on v2 data does nothing
  56. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v1del},
  57. []protocol.FileDownloadProgressUpdate{f1v2p1},
  58. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p2},
  59. },
  60. { //7
  61. // v2 replacees v1, v2 gets deleted, and v2 part 2 gets added.
  62. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1, f1v2del, f1v2p2},
  63. []protocol.FileDownloadProgressUpdate{f1v2p2},
  64. []protocol.FileDownloadProgressUpdate{f1v1p1, f1v1p2, f1v2p1},
  65. },
  66. // Multiple files in one go
  67. { //8
  68. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  69. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  70. []protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
  71. },
  72. { //9
  73. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1, f2v1del},
  74. []protocol.FileDownloadProgressUpdate{f1v1p1},
  75. []protocol.FileDownloadProgressUpdate{f2v1p1, f2v1p1},
  76. },
  77. { //10
  78. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1del, f2v1p1},
  79. []protocol.FileDownloadProgressUpdate{f1v1p1, f2v1p1},
  80. []protocol.FileDownloadProgressUpdate{f1v1p2, f2v1p2},
  81. },
  82. }
  83. for i, test := range tests {
  84. s := newDeviceDownloadState()
  85. s.Update("folder", test.updates)
  86. for _, expected := range test.shouldHaveIndexesFrom {
  87. for _, n := range expected.BlockIndexes {
  88. if !s.Has("folder", expected.Name, expected.Version, n) {
  89. t.Error("Test", i+1, "error:", expected.Name, expected.Version, "missing", n)
  90. }
  91. }
  92. }
  93. for _, unexpected := range test.shouldNotHaveIndexesFrom {
  94. for _, n := range unexpected.BlockIndexes {
  95. if s.Has("folder", unexpected.Name, unexpected.Version, n) {
  96. t.Error("Test", i+1, "error:", unexpected.Name, unexpected.Version, "has extra", n)
  97. }
  98. }
  99. }
  100. }
  101. }