cache_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 ignore
  7. import (
  8. "testing"
  9. "time"
  10. )
  11. func TestCache(t *testing.T) {
  12. c := newCache(nil)
  13. res, ok := c.get("nonexistent")
  14. if res.IsIgnored() || res.IsDeletable() || ok != false {
  15. t.Errorf("res %v, ok %v for nonexistent item", res, ok)
  16. }
  17. // Set and check some items
  18. c.set("true", resultInclude|resultDeletable)
  19. c.set("false", 0)
  20. res, ok = c.get("true")
  21. if !res.IsIgnored() || !res.IsDeletable() || ok != true {
  22. t.Errorf("res %v, ok %v for true item", res, ok)
  23. }
  24. res, ok = c.get("false")
  25. if res.IsIgnored() || res.IsDeletable() || ok != true {
  26. t.Errorf("res %v, ok %v for false item", res, ok)
  27. }
  28. // Don't clean anything
  29. c.clean(time.Second)
  30. // Same values should exist
  31. res, ok = c.get("true")
  32. if !res.IsIgnored() || !res.IsDeletable() || ok != true {
  33. t.Errorf("res %v, ok %v for true item", res, ok)
  34. }
  35. res, ok = c.get("false")
  36. if res.IsIgnored() || res.IsDeletable() || ok != true {
  37. t.Errorf("res %v, ok %v for false item", res, ok)
  38. }
  39. // Sleep and access, to get some data for clean
  40. time.Sleep(500 * time.Millisecond)
  41. c.get("true")
  42. time.Sleep(100 * time.Millisecond)
  43. // "false" was accessed ~600 ms ago, "true" was accessed ~100 ms ago.
  44. // This should clean out "false" but not "true"
  45. c.clean(300 * time.Millisecond)
  46. // Same values should exist
  47. _, ok = c.get("true")
  48. if !ok {
  49. t.Error("item should still exist")
  50. }
  51. _, ok = c.get("false")
  52. if ok {
  53. t.Errorf("item should have been cleaned")
  54. }
  55. }