cache_test.go 3.2 KB

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