summaryrefslogtreecommitdiff
path: root/internal/mapr/groupset_avg_nan_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-07-22 23:51:18 +0300
committerPaul Buetow <paul@buetow.org>2026-07-22 23:51:18 +0300
commit849951be1d1a7ee9f9302006ccb187bf5b4e36f3 (patch)
tree496c924a03a9ea6212e29bb4699e268066ebad81 /internal/mapr/groupset_avg_nan_test.go
parentbf78b3abffee6d49c08ca2980156afc455994969 (diff)
feat: DTail fork — server/client feature development
Squashed development of the snonux/dtail fork's product code (internal/, cmd/) since diverging from mimecast/dtail. Major areas: - Read/output path: the former "turbo" channel-less path is now the single, default server-side read/output path for cat/grep/tail and MapReduce; the old channel-based path and its config/env toggles were removed. - MapReduce: single aggregate implementation (server + serverless) fed directly by a processor pipeline, with input-exhausted finalization via the shutdown coordinator; high-concurrency and data-race fixes. - Journal source reads (journal:unit.service) via journalctl, Linux-gated behind a journal-v1 capability. - Auth-key fast reconnect: in-memory per-user public-key cache with TTL/max-keys, registered over an authenticated session (AUTHKEY), checked before authorized_keys. - Interactive query reload (--interactive-query) with SESSION START/UPDATE generation boundaries and capability negotiation. - Client-side deadlines: --timeout / --shutdownAfter as context deadlines; follow shutdown handling. - Client logging: diagnostics-only daily log by default, opt-in payload tee via --log-payload. - Numerous correctness fixes (buffer-pool double-recycle races, EOF-sentinel leaks, glob-expansion cap, TOCTOU in CSV parsing) with accompanying unit tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/mapr/groupset_avg_nan_test.go')
-rw-r--r--internal/mapr/groupset_avg_nan_test.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/internal/mapr/groupset_avg_nan_test.go b/internal/mapr/groupset_avg_nan_test.go
new file mode 100644
index 0000000..c3d48a4
--- /dev/null
+++ b/internal/mapr/groupset_avg_nan_test.go
@@ -0,0 +1,79 @@
+package mapr
+
+import (
+ "math"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+// TestGroupSetAvgZeroSamplesDoesNotProduceNaN is a negative test that reproduces
+// the bug where an empty aggregate set (Samples==0) causes 0/0 = NaN in the Avg
+// case of resultSelect. This happens when the server creates a group-set entry
+// via GetSet before confirming that any select fields matched, then serialises and
+// sends the empty set to the client. The client-side resultSelect must guard the
+// Avg division so that Samples==0 yields 0 instead of NaN.
+func TestGroupSetAvgZeroSamplesDoesNotProduceNaN(t *testing.T) {
+ t.Parallel()
+
+ query, err := NewQuery("select avg(latency) from stats group by host")
+ if err != nil {
+ t.Fatalf("Unable to parse query: %v", err)
+ }
+
+ groupSet := NewGroupSet()
+
+ // Simulate what the server does when no log line fields match the select
+ // clause: GetSet creates the entry, but Samples stays 0 and FValues is
+ // never populated. This is the bug trigger — previously 0/0 = NaN.
+ _ = groupSet.GetSet("host-a")
+
+ rows, _, err := groupSet.result(query, false)
+ if err != nil {
+ t.Fatalf("result() returned unexpected error: %v", err)
+ }
+ if len(rows) != 1 {
+ t.Fatalf("Expected 1 result row (even for empty set), got %d", len(rows))
+ }
+
+ // Before the fix each floating-point value in the row was the string "NaN".
+ for _, row := range rows {
+ for _, v := range row.values {
+ trimmed := strings.TrimSpace(v)
+ f, parseErr := strconv.ParseFloat(trimmed, 64)
+ if parseErr != nil {
+ // Non-numeric values (e.g. integer count or last-string fields)
+ // are fine; only floating-point results can be NaN.
+ continue
+ }
+ if math.IsNaN(f) {
+ t.Errorf("avg on empty set produced NaN in output %q; expected 0", v)
+ }
+ }
+ }
+}
+
+// TestGroupSetAvgZeroSamplesResultOutputContainsNoNaN verifies that the
+// higher-level Result method (which drives terminal output) also never emits
+// "NaN" strings when aggregate sets have zero samples.
+func TestGroupSetAvgZeroSamplesResultOutputContainsNoNaN(t *testing.T) {
+ t.Parallel()
+
+ query, err := NewQuery("select avg(latency) from stats group by host")
+ if err != nil {
+ t.Fatalf("Unable to parse query: %v", err)
+ }
+
+ groupSet := NewGroupSet()
+ // Empty set — Samples==0, no FValues populated.
+ _ = groupSet.GetSet("host-a")
+
+ output, _, err := groupSet.Result(query, 100, nil)
+ if err != nil {
+ t.Fatalf("Result() returned unexpected error: %v", err)
+ }
+
+ if strings.Contains(output, "NaN") {
+ t.Errorf("Result output must not contain 'NaN', got:\n%s", output)
+ }
+}