virtualmtime_test.go 2.4 KB

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