cache_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 https://mozilla.org/MPL/2.0/.
  6. package discover
  7. import (
  8. "reflect"
  9. "testing"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. func TestCacheUnique(t *testing.T) {
  14. addresses0 := []string{"tcp://192.0.2.44:22000", "tcp://192.0.2.42:22000"}
  15. addresses1 := []string{"tcp://192.0.2.43:22000", "tcp://192.0.2.42:22000"}
  16. // what we expect from just addresses0
  17. addresses0Sorted := []string{"tcp://192.0.2.42:22000", "tcp://192.0.2.44:22000"}
  18. // what we expect from addresses0+addresses1
  19. totalSorted := []string{
  20. "tcp://192.0.2.42:22000",
  21. // no duplicate .42
  22. "tcp://192.0.2.43:22000",
  23. "tcp://192.0.2.44:22000",
  24. }
  25. c := NewCachingMux()
  26. c.(*cachingMux).ServeBackground()
  27. defer c.Stop()
  28. // Add a fake discovery service and verify we get its answers through the
  29. // cache.
  30. f1 := &fakeDiscovery{addresses0}
  31. c.Add(f1, time.Minute, 0)
  32. addr, err := c.Lookup(protocol.LocalDeviceID)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if !reflect.DeepEqual(addr, addresses0Sorted) {
  37. t.Errorf("Incorrect addresses; %+v != %+v", addr, addresses0Sorted)
  38. }
  39. // Add one more that answers in the same way and check that we don't
  40. // duplicate or otherwise mess up the responses now.
  41. f2 := &fakeDiscovery{addresses1}
  42. c.Add(f2, time.Minute, 0)
  43. addr, err = c.Lookup(protocol.LocalDeviceID)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. if !reflect.DeepEqual(addr, totalSorted) {
  48. t.Errorf("Incorrect addresses; %+v != %+v", addr, totalSorted)
  49. }
  50. }
  51. type fakeDiscovery struct {
  52. addresses []string
  53. }
  54. func (f *fakeDiscovery) Lookup(deviceID protocol.DeviceID) (addresses []string, err error) {
  55. return f.addresses, nil
  56. }
  57. func (f *fakeDiscovery) Error() error {
  58. return nil
  59. }
  60. func (f *fakeDiscovery) String() string {
  61. return "fake"
  62. }
  63. func (f *fakeDiscovery) Cache() map[protocol.DeviceID]CacheEntry {
  64. return nil
  65. }
  66. func TestCacheSlowLookup(t *testing.T) {
  67. c := NewCachingMux()
  68. c.(*cachingMux).ServeBackground()
  69. defer c.Stop()
  70. // Add a slow discovery service.
  71. started := make(chan struct{})
  72. f1 := &slowDiscovery{time.Second, started}
  73. c.Add(f1, time.Minute, 0)
  74. // Start a lookup, which will take at least a second
  75. t0 := time.Now()
  76. go c.Lookup(protocol.LocalDeviceID)
  77. <-started // The slow lookup method has been called so we're inside the lock
  78. // It should be possible to get ChildErrors while it's running
  79. c.ChildErrors()
  80. // Only a small amount of time should have passed, not the full second
  81. diff := time.Since(t0)
  82. if diff > 500*time.Millisecond {
  83. t.Error("ChildErrors was blocked for", diff)
  84. }
  85. }
  86. type slowDiscovery struct {
  87. delay time.Duration
  88. started chan struct{}
  89. }
  90. func (f *slowDiscovery) Lookup(deviceID protocol.DeviceID) (addresses []string, err error) {
  91. close(f.started)
  92. time.Sleep(f.delay)
  93. return nil, nil
  94. }
  95. func (f *slowDiscovery) Error() error {
  96. return nil
  97. }
  98. func (f *slowDiscovery) String() string {
  99. return "fake"
  100. }
  101. func (f *slowDiscovery) Cache() map[protocol.DeviceID]CacheEntry {
  102. return nil
  103. }