virtualmtime_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2015 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 db
  7. import (
  8. "testing"
  9. "time"
  10. )
  11. func TestVirtualMtimeRepo(t *testing.T) {
  12. ldb := OpenMemory()
  13. // A few repos so we can ensure they don't pollute each other
  14. repo1 := NewVirtualMtimeRepo(ldb, "folder1")
  15. repo2 := NewVirtualMtimeRepo(ldb, "folder2")
  16. // Since GetMtime() returns its argument if the key isn't found or is outdated, we need a dummy to test with.
  17. dummyTime := time.Date(2001, time.February, 3, 4, 5, 6, 0, time.UTC)
  18. // Some times to test with
  19. time1 := time.Date(2001, time.February, 3, 4, 5, 7, 0, time.UTC)
  20. time2 := time.Date(2010, time.February, 3, 4, 5, 6, 0, time.UTC)
  21. file1 := "file1.txt"
  22. // Files are not present at the start
  23. if v := repo1.GetMtime(file1, dummyTime); !v.Equal(dummyTime) {
  24. t.Errorf("Mtime should be missing (%v) from repo 1 but it's %v", dummyTime, v)
  25. }
  26. if v := repo2.GetMtime(file1, dummyTime); !v.Equal(dummyTime) {
  27. t.Errorf("Mtime should be missing (%v) from repo 2 but it's %v", dummyTime, v)
  28. }
  29. repo1.UpdateMtime(file1, time1, time2)
  30. // Now it should return time2 only when time1 is passed as the argument
  31. if v := repo1.GetMtime(file1, time1); !v.Equal(time2) {
  32. t.Errorf("Mtime should be %v for disk time %v but we got %v", time2, time1, v)
  33. }
  34. if v := repo1.GetMtime(file1, dummyTime); !v.Equal(dummyTime) {
  35. t.Errorf("Mtime should be %v for disk time %v but we got %v", dummyTime, dummyTime, v)
  36. }
  37. // repo2 shouldn't know about this file
  38. if v := repo2.GetMtime(file1, time1); !v.Equal(time1) {
  39. t.Errorf("Mtime should be %v for disk time %v in repo 2 but we got %v", time1, time1, v)
  40. }
  41. repo1.DeleteMtime(file1)
  42. // Now it should be gone
  43. if v := repo1.GetMtime(file1, time1); !v.Equal(time1) {
  44. t.Errorf("Mtime should be %v for disk time %v but we got %v", time1, time1, v)
  45. }
  46. // Try again but with Drop()
  47. repo1.UpdateMtime(file1, time1, time2)
  48. repo1.Drop()
  49. if v := repo1.GetMtime(file1, time1); !v.Equal(time1) {
  50. t.Errorf("Mtime should be %v for disk time %v but we got %v", time1, time1, v)
  51. }
  52. }