Give every API value with a unit as a number (closes #199)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
A value with a unit is now a number, and its field name ends in the unit, as the vendored API_POLICIES.md requires. @meta.execution_time becomes execution_time_ms. In the stats answers, uptime, connection_duration, memory_usage and whois_stats.current_interval become uptime_seconds, connection_duration_seconds, memory_usage_bytes and whois_stats.current_interval_seconds. connection_duration_seconds is null while the feed is not connected. The status page formats the numbers itself. REPO_POLICIES.md is re-vendored from the same sneak/prompts commit. Model: opus-5-5
This commit was merged in pull request #207.
This commit is contained in:
@@ -73,6 +73,7 @@ func emptyTable() *routetable.Table {
|
||||
// Paths several tests request.
|
||||
const (
|
||||
asPagePath = "/as/64500"
|
||||
asJSONPath = "/api/v1/as/64500"
|
||||
prefixLengthPagePath = "/prefixlength/24"
|
||||
statusJSONPath = "/status.json"
|
||||
statsPath = "/api/v1/stats"
|
||||
@@ -295,6 +296,167 @@ func TestStatsAnswerCarriesChurnPerMinute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// fixedIntervalASNFetcher is a WHOIS fetcher that does nothing and reports
|
||||
// interval as its wait between fetches.
|
||||
type fixedIntervalASNFetcher struct {
|
||||
idleASNFetcher
|
||||
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
func (f fixedIntervalASNFetcher) GetStats() ASNFetcherStats {
|
||||
return ASNFetcherStats{CurrentInterval: f.interval}
|
||||
}
|
||||
|
||||
// unitStats holds the figures of a stats answer that carry a unit. Each is a
|
||||
// pointer, so that a figure missing from the answer is nil.
|
||||
//
|
||||
//nolint:tagliatelle // field names are the existing JSON API
|
||||
type unitStats struct {
|
||||
UptimeSeconds *int64 `json:"uptime_seconds"`
|
||||
MemoryUsageBytes *uint64 `json:"memory_usage_bytes"`
|
||||
WHOISStats struct {
|
||||
CurrentIntervalSeconds *float64 `json:"current_interval_seconds"`
|
||||
} `json:"whois_stats"`
|
||||
GC struct {
|
||||
HeapAllocBytes uint64 `json:"heap_alloc_bytes"`
|
||||
} `json:"gc"`
|
||||
}
|
||||
|
||||
// TestStatsAnswersGiveUnitsInFieldNames checks that the figures of both stats
|
||||
// answers that carry a unit are numbers, with the unit at the end of their
|
||||
// names. They used to be strings with the unit in them, such as "1h18m34s" or
|
||||
// "1.1 GB" (https://git.eeqj.de/sneak/routewatch/issues/199). The stream has
|
||||
// never connected, so the uptime is 0. The memory use in /api/v1/stats is the
|
||||
// same number of bytes as its gc.heap_alloc_bytes.
|
||||
func TestStatsAnswersGiveUnitsInFieldNames(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const fetchInterval = 18200 * time.Millisecond
|
||||
|
||||
s := newServerWithTestRoutes(t)
|
||||
s.SetASNFetcher(fixedIntervalASNFetcher{interval: fetchInterval})
|
||||
|
||||
for _, path := range []string{statusJSONPath, statsPath} {
|
||||
answer := get(t, s, path).Body.Bytes()
|
||||
|
||||
var body struct {
|
||||
Data unitStats `json:"data"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(answer, &body)
|
||||
if err != nil {
|
||||
t.Errorf("%s: figures are not numbers: %v; answer %s", path, err, answer)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
got := body.Data
|
||||
if got.UptimeSeconds == nil || *got.UptimeSeconds != 0 {
|
||||
t.Errorf("%s: uptime_seconds is not 0: %s", path, answer)
|
||||
}
|
||||
|
||||
if got.MemoryUsageBytes == nil || *got.MemoryUsageBytes == 0 {
|
||||
t.Errorf("%s: memory_usage_bytes is not a number of bytes: %s",
|
||||
path, answer)
|
||||
}
|
||||
|
||||
interval := got.WHOISStats.CurrentIntervalSeconds
|
||||
if interval == nil || *interval != fetchInterval.Seconds() {
|
||||
t.Errorf("%s: whois_stats.current_interval_seconds is not %v: %s",
|
||||
path, fetchInterval.Seconds(), answer)
|
||||
}
|
||||
|
||||
if path == statsPath && got.MemoryUsageBytes != nil &&
|
||||
*got.MemoryUsageBytes != got.GC.HeapAllocBytes {
|
||||
t.Errorf("%s: memory_usage_bytes %d, want gc.heap_alloc_bytes %d",
|
||||
path, *got.MemoryUsageBytes, got.GC.HeapAllocBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatsAnswerGivesConnectionDurationInSeconds checks that the
|
||||
// connection_duration_seconds of /api/v1/stats is null while the stream is not
|
||||
// connected, and a whole number of seconds once it is. It used to be the
|
||||
// string "disconnected", or a Go duration such as "1h18m34s"
|
||||
// (https://git.eeqj.de/sneak/routewatch/issues/199).
|
||||
func TestStatsAnswerGivesConnectionDurationInSeconds(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := newServerWithTestRoutes(t)
|
||||
|
||||
connectionDuration := func() json.RawMessage {
|
||||
answer := get(t, s, statsPath).Body.Bytes()
|
||||
|
||||
var body struct {
|
||||
Data map[string]json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(answer, &body)
|
||||
if err != nil {
|
||||
t.Fatalf("decoding the answer: %v; answer %s", err, answer)
|
||||
}
|
||||
|
||||
duration, ok := body.Data["connection_duration_seconds"]
|
||||
if !ok {
|
||||
t.Fatalf("no connection_duration_seconds: %s", answer)
|
||||
}
|
||||
|
||||
return duration
|
||||
}
|
||||
|
||||
disconnected := connectionDuration()
|
||||
if string(disconnected) != "null" {
|
||||
t.Errorf("disconnected: connection_duration_seconds = %s, want null",
|
||||
disconnected)
|
||||
}
|
||||
|
||||
s.streamer.GetMetricsTracker().SetConnected(true)
|
||||
|
||||
var seconds int64
|
||||
|
||||
connected := connectionDuration()
|
||||
|
||||
err := json.Unmarshal(connected, &seconds)
|
||||
if err != nil || seconds != 0 {
|
||||
t.Errorf("just connected: connection_duration_seconds = %s, want 0",
|
||||
connected)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamDurationsCountSeconds checks that the uptime and the connection
|
||||
// duration of a stream that connected 90 seconds ago are both 90. A stream
|
||||
// that has never connected has an uptime of 0, and one that is not connected
|
||||
// has no connection duration.
|
||||
func TestStreamDurationsCountSeconds(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const connectedFor = 90 * time.Second
|
||||
|
||||
connectedSince := time.Now().Add(-connectedFor)
|
||||
want := int64(connectedFor / time.Second)
|
||||
|
||||
if got := uptimeSeconds(connectedSince); got != want {
|
||||
t.Errorf("uptimeSeconds = %d, want %d", got, want)
|
||||
}
|
||||
|
||||
if got := uptimeSeconds(time.Time{}); got != 0 {
|
||||
t.Errorf("uptimeSeconds before any connection = %d, want 0", got)
|
||||
}
|
||||
|
||||
switch got := connectionDurationSeconds(true, connectedSince); {
|
||||
case got == nil:
|
||||
t.Errorf("connectionDurationSeconds = nil, want %d", want)
|
||||
case *got != want:
|
||||
t.Errorf("connectionDurationSeconds = %d, want %d", *got, want)
|
||||
}
|
||||
|
||||
if got := connectionDurationSeconds(false, connectedSince); got != nil {
|
||||
t.Errorf("connectionDurationSeconds while not connected = %d, want nil",
|
||||
*got)
|
||||
}
|
||||
}
|
||||
|
||||
// newTestTable returns a routing table that holds a route for testPrefixV4,
|
||||
// with origin AS 64500, and one for testPrefixV6, with origin AS 64501.
|
||||
func newTestTable() *routetable.Table {
|
||||
@@ -766,7 +928,7 @@ func TestRoutesAnswer504WhenDatabaseReadIsCutOff(t *testing.T) {
|
||||
{path: "/ip/" + addrInTestPrefixV4, wantJSONBody: true},
|
||||
{path: statsPath, wantJSONBody: true},
|
||||
{path: "/api/v1/ip/" + addrInTestPrefixV4, wantJSONBody: true},
|
||||
{path: "/api/v1/as/64500", wantJSONBody: true},
|
||||
{path: asJSONPath, wantJSONBody: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -950,7 +1112,7 @@ func TestJSONRoutesDoNotSendErrorDetails(t *testing.T) {
|
||||
{"/.well-known/healthcheck.json", http.StatusServiceUnavailable},
|
||||
{statusJSONPath, http.StatusInternalServerError},
|
||||
{statsPath, http.StatusInternalServerError},
|
||||
{"/api/v1/as/64500", http.StatusInternalServerError},
|
||||
{asJSONPath, http.StatusInternalServerError},
|
||||
{"/api/v1/ip/" + addrInTestPrefixV4, http.StatusInternalServerError},
|
||||
{"/api/v1/prefix/foo/24", http.StatusBadRequest},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user