MCPcopy
hub / github.com/syncthing/syncthing / TestCacheUnique

Function TestCacheUnique

lib/discover/cache_test.go:30–76  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28}
29
30func TestCacheUnique(t *testing.T) {
31 addresses0 := []string{"tcp://192.0.2.44:22000", "tcp://192.0.2.42:22000"}
32 addresses1 := []string{"tcp://192.0.2.43:22000", "tcp://192.0.2.42:22000"}
33
34 // what we expect from just addresses0
35 addresses0Sorted := []string{"tcp://192.0.2.42:22000", "tcp://192.0.2.44:22000"}
36
37 // what we expect from addresses0+addresses1
38 totalSorted := []string{
39 "tcp://192.0.2.42:22000",
40 // no duplicate .42
41 "tcp://192.0.2.43:22000",
42 "tcp://192.0.2.44:22000",
43 }
44
45 c := setupCache()
46
47 // Add a fake discovery service and verify we get its answers through the
48 // cache.
49
50 f1 := &fakeDiscovery{addresses0}
51 c.addLocked("f1", f1, time.Minute, 0)
52
53 ctx := context.Background()
54
55 addr, err := c.Lookup(ctx, protocol.LocalDeviceID)
56 if err != nil {
57 t.Fatal(err)
58 }
59 if !reflect.DeepEqual(addr, addresses0Sorted) {
60 t.Errorf("Incorrect addresses; %+v != %+v", addr, addresses0Sorted)
61 }
62
63 // Add one more that answers in the same way and check that we don't
64 // duplicate or otherwise mess up the responses now.
65
66 f2 := &fakeDiscovery{addresses1}
67 c.addLocked("f2", f2, time.Minute, 0)
68
69 addr, err = c.Lookup(ctx, protocol.LocalDeviceID)
70 if err != nil {
71 t.Fatal(err)
72 }
73 if !reflect.DeepEqual(addr, totalSorted) {
74 t.Errorf("Incorrect addresses; %+v != %+v", addr, totalSorted)
75 }
76}
77
78type fakeDiscovery struct {
79 addresses []string

Callers

nothing calls this directly

Calls 4

setupCacheFunction · 0.85
addLockedMethod · 0.80
FatalMethod · 0.80
LookupMethod · 0.65

Tested by

no test coverage detected