cache_test.go 3.5 KB

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