staggered_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 http://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "sort"
  9. "strconv"
  10. "testing"
  11. "time"
  12. "github.com/d4l3k/messagediff"
  13. )
  14. func TestStaggeredVersioningVersionCount(t *testing.T) {
  15. /* Default settings:
  16. {30, 3600}, // first hour -> 30 sec between versions
  17. {3600, 86400}, // next day -> 1 h between versions
  18. {86400, 592000}, // next 30 days -> 1 day between versions
  19. {604800, maxAge}, // next year -> 1 week between versions
  20. */
  21. loc, _ := time.LoadLocation("Local")
  22. now, _ := time.ParseInLocation(TimeFormat, "20160415-140000", loc)
  23. files := []string{
  24. // 14:00:00 is "now"
  25. "test~20160415-140000", // 0 seconds ago
  26. "test~20160415-135959", // 1 second ago
  27. "test~20160415-135958", // 2 seconds ago
  28. "test~20160415-135900", // 1 minute ago
  29. "test~20160415-135859", // 1 minute 1 second ago
  30. "test~20160415-135830", // 1 minute 30 seconds ago
  31. "test~20160415-135829", // 1 minute 31 seconds ago
  32. "test~20160415-135700", // 3 minutes ago
  33. "test~20160415-135630", // 3 minutes 30 seconds ago
  34. "test~20160415-133000", // 30 minutes ago
  35. "test~20160415-132900", // 31 minutes ago
  36. "test~20160415-132500", // 35 minutes ago
  37. "test~20160415-132000", // 40 minutes ago
  38. "test~20160415-130000", // 60 minutes ago
  39. "test~20160415-124000", // 80 minutes ago
  40. "test~20160415-122000", // 100 minutes ago
  41. "test~20160415-110000", // 120 minutes ago
  42. }
  43. sort.Strings(files)
  44. delete := []string{
  45. "test~20160415-140000", // 0 seconds ago
  46. "test~20160415-135959", // 1 second ago
  47. "test~20160415-135900", // 1 minute ago
  48. "test~20160415-135830", // 1 minute 30 second ago
  49. "test~20160415-130000", // 60 minutes ago
  50. "test~20160415-124000", // 80 minutes ago
  51. }
  52. sort.Strings(delete)
  53. v := NewStaggered("", "testdata", map[string]string{"maxAge": strconv.Itoa(365 * 86400)}).(Staggered)
  54. rem := v.toRemove(files, now)
  55. if diff, equal := messagediff.PrettyDiff(delete, rem); !equal {
  56. t.Errorf("Incorrect deleted files; got %v, expected %v\n%v", rem, delete, diff)
  57. }
  58. }