cache_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package ignore
  16. import (
  17. "testing"
  18. "time"
  19. )
  20. func TestCache(t *testing.T) {
  21. c := newCache(nil)
  22. res, ok := c.get("nonexistent")
  23. if res != false || ok != false {
  24. t.Error("res %v, ok %v for nonexistent item", res, ok)
  25. }
  26. // Set and check some items
  27. c.set("true", true)
  28. c.set("false", false)
  29. res, ok = c.get("true")
  30. if res != true || ok != true {
  31. t.Errorf("res %v, ok %v for true item", res, ok)
  32. }
  33. res, ok = c.get("false")
  34. if res != false || ok != true {
  35. t.Errorf("res %v, ok %v for false item", res, ok)
  36. }
  37. // Don't clean anything
  38. c.clean(time.Second)
  39. // Same values should exist
  40. res, ok = c.get("true")
  41. if res != true || ok != true {
  42. t.Errorf("res %v, ok %v for true item", res, ok)
  43. }
  44. res, ok = c.get("false")
  45. if res != false || ok != true {
  46. t.Errorf("res %v, ok %v for false item", res, ok)
  47. }
  48. // Sleep and access, to get some data for clean
  49. time.Sleep(100 * time.Millisecond)
  50. c.get("true")
  51. time.Sleep(100 * time.Millisecond)
  52. // "false" was accessed 200 ms ago, "true" was accessed 100 ms ago.
  53. // This should clean out "false" but not "true"
  54. c.clean(150 * time.Millisecond)
  55. // Same values should exist
  56. _, ok = c.get("true")
  57. if !ok {
  58. t.Error("item should still exist")
  59. }
  60. _, ok = c.get("false")
  61. if ok {
  62. t.Errorf("item should have been cleaned")
  63. }
  64. }