cache_test.go 3.6 KB

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