staggered_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 https://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "sort"
  9. "strconv"
  10. "testing"
  11. "time"
  12. "github.com/d4l3k/messagediff"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. func TestStaggeredVersioningVersionCount(t *testing.T) {
  16. /* Default settings:
  17. {30, 3600}, // first hour -> 30 sec between versions
  18. {3600, 86400}, // next day -> 1 h between versions
  19. {86400, 592000}, // next 30 days -> 1 day between versions
  20. {604800, maxAge}, // next year -> 1 week between versions
  21. */
  22. now := parseTime("20160415-140000")
  23. versionsWithMtime := []string{
  24. // 14:00:00 is "now"
  25. "test~20160415-140000", // 0 seconds ago
  26. "test~20160415-135959", // 1 second ago
  27. "test~20160415-135931", // 29 seconds ago
  28. "test~20160415-135930", // 30 seconds ago
  29. "test~20160415-130059", // 59 minutes 01 seconds ago
  30. "test~20160415-130030", // 59 minutes 30 seconds ago
  31. "test~20160415-130000", // 1 hour ago
  32. "test~20160415-120001", // 1 hour 59:59 ago
  33. "test~20160414-155959", // 22 hours 1 second ago
  34. "test~20160414-150001", // 22 hours 59 seconds ago
  35. "test~20160414-150000", // 23 hours ago
  36. "test~20160414-140000", // 1 day ago
  37. "test~20160414-130001", // 1 days 59:59 second ago
  38. "test~20160409-135959", // 6 days 1 second ago
  39. "test~20160408-140001", // 6 days 23:59:59 second ago
  40. "test~20160408-140000", // 7 days ago
  41. "test~20160408-135959", // 7 days 1 second ago
  42. "test~20160407-140001", // 7 days 23:59:59 ago
  43. "test~20160407-140000", // 8 days ago
  44. "test~20160317-140000", // 29 days ago
  45. "test~20160317-135959", // 29 days 1 second ago
  46. "test~20160316-140000", // 30 days ago
  47. "test~20160308-135959", // 37 days 1 second ago
  48. "test~20160301-140000", // 44 days ago
  49. "test~20160223-140000", // 51 days ago
  50. "test~20150423-140000", // 358 days ago (!!! 2016 was a leap year !!!)
  51. "test~20150417-140000", // 364 days ago
  52. "test~20150416-140000", // 365 days ago
  53. // exceeds maxAge
  54. "test~20150416-135959", // 365 days 1 second ago
  55. "test~20150416-135958", // 365 days 2 seconds ago
  56. "test~20150414-140000", // 367 days ago
  57. }
  58. delete := []string{
  59. "test~20160415-135959", // 1 second ago
  60. "test~20160415-135931", // 29 seconds ago
  61. "test~20160415-130059", // 59 minutes 01 seconds ago
  62. "test~20160415-130000", // 1 hour ago
  63. "test~20160414-155959", // 22 hours 1 second ago
  64. "test~20160414-150001", // 22 hours 59 seconds ago
  65. "test~20160414-140000", // 1 day ago
  66. "test~20160409-135959", // 6 days 1 second ago
  67. "test~20160408-140001", // 6 days 23:59:59 second ago
  68. "test~20160408-135959", // 7 days 1 second ago
  69. "test~20160407-140001", // 7 days 23:59:59 ago
  70. "test~20160317-135959", // 29 days 1 second ago
  71. "test~20160308-135959", // 37 days 1 second ago
  72. "test~20150417-140000", // 364 days ago
  73. "test~20150416-135959", // 365 days 1 second ago
  74. "test~20150416-135958", // 365 days 2 seconds ago
  75. "test~20150414-140000", // 367 days ago
  76. }
  77. sort.Strings(delete)
  78. v := newStaggered(fs.NewFilesystem(fs.FilesystemTypeFake, "testdata"), map[string]string{
  79. "maxAge": strconv.Itoa(365 * 86400),
  80. }).(*staggered)
  81. rem := v.toRemove(versionsWithMtime, now)
  82. sort.Strings(rem)
  83. if diff, equal := messagediff.PrettyDiff(delete, rem); !equal {
  84. t.Errorf("Incorrect deleted files; got %v, expected %v\n%v", rem, delete, diff)
  85. }
  86. }
  87. func parseTime(in string) time.Time {
  88. t, err := time.ParseInLocation(TimeFormat, in, time.Local)
  89. if err != nil {
  90. panic(err.Error())
  91. }
  92. return t
  93. }