cache_test.go 3.8 KB

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