diff --git a/internal/speedtest/latency/latency.go b/internal/speedtest/latency/latency.go index 136b3df..dfa7258 100644 --- a/internal/speedtest/latency/latency.go +++ b/internal/speedtest/latency/latency.go @@ -3,7 +3,7 @@ package latency import ( "context" "math" - "sort" + "slices" "sync" "time" ) @@ -131,7 +131,7 @@ func Compute(samples []float64) Stats { } sorted := append([]float64(nil), samples...) - sort.Float64s(sorted) + slices.Sort(sorted) minV := sorted[0] maxV := sorted[n-1] diff --git a/internal/speedtest/runner/runner.go b/internal/speedtest/runner/runner.go index 8b506da..702324e 100644 --- a/internal/speedtest/runner/runner.go +++ b/internal/speedtest/runner/runner.go @@ -2,6 +2,7 @@ package runner import ( "bufio" + "cmp" "context" "encoding/json" "fmt" @@ -9,7 +10,7 @@ import ( "net" "net/http" "os" - "sort" + "slices" "strconv" "strings" "time" @@ -302,16 +303,7 @@ func discoverCandidates(ctx context.Context, cfg *speedconfig.Config, p provider seen[key] = true out.Candidates = append(out.Candidates, buildCandidate(ctx, cfg, p, host, key, "dns")) } - sort.SliceStable(out.Candidates, func(i, j int) bool { - li, lj := out.Candidates[i], out.Candidates[j] - if li.Status == "ok" && lj.Status != "ok" { - return true - } - if li.Status != "ok" && lj.Status == "ok" { - return false - } - return li.RTTMs < lj.RTTMs - }) + sortCandidates(out.Candidates) for _, cand := range out.Candidates { if cand.Status == "ok" { out.Selected = cand @@ -328,6 +320,18 @@ func discoverCandidates(ctx context.Context, cfg *speedconfig.Config, p provider return out, nil } +func sortCandidates(candidates []candidate) { + slices.SortStableFunc(candidates, func(a, b candidate) int { + if a.Status == "ok" && b.Status != "ok" { + return -1 + } + if a.Status != "ok" && b.Status == "ok" { + return 1 + } + return cmp.Compare(a.RTTMs, b.RTTMs) + }) +} + func buildCandidate(ctx context.Context, cfg *speedconfig.Config, p provider.Provider, host, ip, source string) candidate { cand := candidate{IP: ip, Source: source, Status: "degraded"} if !cfg.NoMetadata { diff --git a/internal/speedtest/runner/runner_test.go b/internal/speedtest/runner/runner_test.go index 535ed2a..9a6a7ce 100644 --- a/internal/speedtest/runner/runner_test.go +++ b/internal/speedtest/runner/runner_test.go @@ -19,6 +19,24 @@ import ( "github.com/nxtrace/NTrace-core/internal/speedtest/result" ) +func TestSortCandidatesKeepsHealthyRTTAndStableOrder(t *testing.T) { + candidates := []candidate{ + {IP: "192.0.2.1", Status: "degraded", RTTMs: 20}, + {IP: "192.0.2.2", Status: "ok", RTTMs: 10}, + {IP: "192.0.2.3", Status: "ok", RTTMs: 10}, + {IP: "192.0.2.4", Status: "degraded", RTTMs: 5}, + } + + sortCandidates(candidates) + + want := []string{"192.0.2.2", "192.0.2.3", "192.0.2.4", "192.0.2.1"} + for i, ip := range want { + if candidates[i].IP != ip { + t.Fatalf("Candidates[%d].IP = %q, want %q", i, candidates[i].IP, ip) + } + } +} + func TestResolveLocalBindIPPrefersExplicitSource(t *testing.T) { ip, err := resolveLocalBindIP(net.ParseIP("1.1.1.1"), "127.0.0.1", "missing0") if err != nil { diff --git a/internal/speedtest/transfer/transfer.go b/internal/speedtest/transfer/transfer.go index 229b51f..08bfd18 100644 --- a/internal/speedtest/transfer/transfer.go +++ b/internal/speedtest/transfer/transfer.go @@ -73,9 +73,7 @@ func Run( }() for i := 0; i < threads; i++ { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { var fault bool if dir == Download { _, fault = doDownload(ctx2, client, spec, timeout, &totalBytes) @@ -85,7 +83,7 @@ func Run( if fault { faultCount.Add(1) } - }() + }) } wg.Wait() diff --git a/trace/cache.go b/trace/cache.go index 298d202..e18fdd7 100644 --- a/trace/cache.go +++ b/trace/cache.go @@ -1,8 +1,5 @@ package trace func ClearCaches() { - geoCache.Range(func(key, value any) bool { - geoCache.Delete(key) - return true - }) + geoCache.Clear() } diff --git a/trace/geo_lookup_test.go b/trace/geo_lookup_test.go index 68367ff..5996f8c 100644 --- a/trace/geo_lookup_test.go +++ b/trace/geo_lookup_test.go @@ -10,6 +10,20 @@ import ( "github.com/nxtrace/NTrace-core/ipgeo" ) +func TestClearCachesRemovesAllEntries(t *testing.T) { + geoCache.Store("198.51.100.1", &ipgeo.IPGeoData{IP: "198.51.100.1"}) + geoCache.Store("2001:db8::1", &ipgeo.IPGeoData{IP: "2001:db8::1"}) + + ClearCaches() + + if _, ok := geoCache.Load("198.51.100.1"); ok { + t.Fatal("IPv4 cache entry remained after ClearCaches") + } + if _, ok := geoCache.Load("2001:db8::1"); ok { + t.Fatal("IPv6 cache entry remained after ClearCaches") + } +} + func TestLookupIPGeoCachesResults(t *testing.T) { ClearCaches() t.Cleanup(ClearCaches) diff --git a/util/util.go b/util/util.go index aa082c8..76bc2aa 100644 --- a/util/util.go +++ b/util/util.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "runtime" + "slices" "strconv" "strings" "sync" @@ -589,12 +590,7 @@ func GetPowProvider() string { } func StringInSlice(val string, list []string) bool { - for _, v := range list { - if v == val { - return true - } - } - return false + return slices.Contains(list, val) } func HideIPPart(ip string) string { diff --git a/wshandle/client.go b/wshandle/client.go index 58d3705..7e84054 100644 --- a/wshandle/client.go +++ b/wshandle/client.go @@ -23,10 +23,7 @@ import ( func formatHostPort(addr, port string) string { clean := strings.TrimSpace(addr) clean = strings.Trim(clean, "[]") - if strings.Contains(clean, ":") { - return "[" + clean + "]:" + port - } - return clean + ":" + port + return net.JoinHostPort(clean, port) } type wsWriteJob struct { diff --git a/wshandle/client_test.go b/wshandle/client_test.go index 309d388..26e932f 100644 --- a/wshandle/client_test.go +++ b/wshandle/client_test.go @@ -17,6 +17,29 @@ import ( "github.com/nxtrace/NTrace-core/util" ) +func TestFormatHostPort(t *testing.T) { + tests := []struct { + name string + addr string + port string + want string + }{ + {name: "IPv4", addr: " 192.0.2.1 ", port: "443", want: "192.0.2.1:443"}, + {name: "IPv6", addr: "2001:db8::1", port: "443", want: "[2001:db8::1]:443"}, + {name: "bracketed IPv6", addr: "[2001:db8::1]", port: "443", want: "[2001:db8::1]:443"}, + {name: "IPv6 zone", addr: "fe80::1%en0", port: "443", want: "[fe80::1%en0]:443"}, + {name: "empty host", addr: "", port: "443", want: ":443"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := formatHostPort(tt.addr, tt.port); got != tt.want { + t.Fatalf("formatHostPort(%q, %q) = %q, want %q", tt.addr, tt.port, got, tt.want) + } + }) + } +} + func newStartedTestWsConn() *WsConn { return newStartedTestWsConnWithContext(context.Background()) }