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