(t *testing.T)
| 28 | } |
| 29 | |
| 30 | func 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 | |
| 78 | type fakeDiscovery struct { |
| 79 | addresses []string |
nothing calls this directly
no test coverage detected