edge: path hop fallback test proves the suspect box is skipped (#186)
TestBackhaulRouteToFallsBackOnRoute now counts the sender's dials to each destination address and asserts betty is dialled once on the first hop and not again on the second. Without it, a backhaulTarget keyed on the wire host would pass: the second hop would redial betty and reach carol through the connect fallback with the same answer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+24
−1
internal/edge/pathhop_test.go+24 −1
diff --git a/internal/edge/pathhop_test.go b/internal/edge/pathhop_test.go| index 3a6cad5a5..2bdc7807b 100644 |
| --- a/internal/edge/pathhop_test.go |
| +++ b/internal/edge/pathhop_test.go |
| @@ -15,6 +15,7 @@ import ( |
| "net/http" |
| "net/http/httptest" |
| "sync" |
| + "sync/atomic" |
| "testing" |
| "time" |
| @@ -40,6 +41,9 @@ type pathHopScene struct { |
| bucket string |
| sender *backhaulEdge |
| edges []*backhaulEdge |
| + // dials counts the sender's dial attempts per destination address |
| + // (dialAt), refused connections included. |
| + dials sync.Map // ip → *atomic.Int64 |
| } |
| func newPathHopScene(t *testing.T) *pathHopScene { |
| @@ -71,17 +75,29 @@ func (s *pathHopScene) file(t *testing.T, box, ip, region string, stamp int) { |
| box, ip, region, time.Now().UTC().Format(time.RFC3339Nano), hosts)) |
| } |
| -// dialAt points the sender's backhaul transport for ip at addr. |
| +// dialAt points the sender's backhaul transport for ip at addr, counting |
| +// every dial (dialsTo). |
| func (s *pathHopScene) dialAt(t *testing.T, ip, addr string) { |
| t.Helper() |
| + n, _ := s.dials.LoadOrStore(ip, new(atomic.Int64)) |
| tr := s.sender.edge.backhaulTransport(ip) |
| tr.DialContext = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| + n.(*atomic.Int64).Add(1) |
| var d net.Dialer |
| return d.DialContext(ctx, network, addr) |
| } |
| t.Cleanup(tr.CloseIdleConnections) |
| } |
| +// dialsTo is how many times the sender has dialled ip. |
| +func (s *pathHopScene) dialsTo(ip string) int64 { |
| + n, ok := s.dials.Load(ip) |
| + if !ok { |
| + return 0 |
| + } |
| + return n.(*atomic.Int64).Load() |
| +} |
| + |
| // edge adds a real receiving edge whose registry serves routes. |
| func (s *pathHopScene) edge(t *testing.T, box, ip string, stamp int, routes map[string]string) *backhaulEdge { |
| t.Helper() |
| @@ -307,11 +323,18 @@ func TestBackhaulRouteToFallsBackOnRoute(t *testing.T) { |
| }) |
| s.refresh(t) |
| betty.srv.Close() |
| + // The second hop must not dial betty at all: a reach to carol through a |
| + // second connect fallback would answer the same, so the dial count is |
| + // what proves backhaulTarget took the route's alternative. |
| + wantBettyDials := []int64{1, 1} |
| for i := range 2 { |
| w := s.send(t, pathHopRequest()) |
| if w.Code != http.StatusOK || w.Body.String() != "carol|"+pathHopWire { |
| t.Fatalf("hop %d: %d %q", i, w.Code, w.Body.String()) |
| } |
| + if n := s.dialsTo("127.0.0.2"); n != wantBettyDials[i] { |
| + t.Fatalf("after hop %d betty was dialled %d times, want %d", i, n, wantBettyDials[i]) |
| + } |
| } |
| if hosts, _ := carol.seen(); len(hosts) != 2 { |
| t.Fatalf("carol calls=%v, want 2", hosts) |