summaryrefslogtreecommitdiff
path: root/internal/mapr/globalgroupset_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/globalgroupset_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/globalgroupset_test.go')
-rw-r--r--internal/mapr/globalgroupset_test.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/internal/mapr/globalgroupset_test.go b/internal/mapr/globalgroupset_test.go
new file mode 100644
index 0000000..3e2a9e5
--- /dev/null
+++ b/internal/mapr/globalgroupset_test.go
@@ -0,0 +1,95 @@
+package mapr
+
+import (
+ "testing"
+ "time"
+)
+
+// TestMergeNoblockSemaphoreReleasedOnPanic verifies that MergeNoblock releases
+// the semaphore even when g.merge panics (e.g. due to a nil GroupSet).
+// Without the fix (using defer), the semaphore would be leaked and subsequent
+// calls like NumSets would deadlock forever.
+func TestMergeNoblockSemaphoreReleasedOnPanic(t *testing.T) {
+ g := NewGlobalGroupSet()
+
+ // Calling MergeNoblock with a nil *GroupSet causes a nil-pointer dereference
+ // inside g.merge when it iterates over group.sets. We catch the panic in a
+ // goroutine and verify that the GlobalGroupSet is still usable afterwards.
+ done := make(chan struct{})
+ go func() {
+ defer func() {
+ // Recover the expected panic so the goroutine exits cleanly.
+ if r := recover(); r == nil {
+ t.Errorf("expected a panic from MergeNoblock with nil GroupSet, got none")
+ }
+ close(done)
+ }()
+ // This must panic internally; with the bug the semaphore is never released.
+ //nolint:staticcheck // intentional nil dereference to exercise the panic path
+ g.MergeNoblock(nil, nil) //nolint:errcheck
+ }()
+
+ // Wait for the goroutine to finish (panic recovered).
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("timed out waiting for MergeNoblock panic to be recovered")
+ }
+
+ // After the panic the semaphore must have been released by the deferred
+ // release in MergeNoblock. If the bug is present NumSets acquires the same
+ // 1-slot semaphore and blocks forever, causing the test to time out.
+ result := make(chan int, 1)
+ go func() {
+ result <- g.NumSets()
+ }()
+
+ select {
+ case n := <-result:
+ if n != 0 {
+ t.Errorf("expected 0 sets in empty GlobalGroupSet, got %d", n)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("NumSets deadlocked: semaphore was not released after MergeNoblock panic (bug reproduced)")
+ }
+}
+
+// TestMergeNoblockNormalOperation verifies the non-panic happy path still works
+// correctly: a successful merge returns (true, nil) and NumSets reflects the
+// merged data.
+func TestMergeNoblockNormalOperation(t *testing.T) {
+ g := NewGlobalGroupSet()
+ group := NewGroupSet()
+
+ // Populate the group set with one entry so there is something to merge.
+ set := NewAggregateSet()
+ set.FValues["count"] = 1
+ group.sets["key1"] = set
+
+ // A minimal query is enough; the merge loop only needs query.Select which
+ // can be empty for this structural test (no select conditions to iterate).
+ query := &Query{}
+
+ merged, err := g.MergeNoblock(query, group)
+ if err != nil {
+ t.Errorf("unexpected error from MergeNoblock: %v", err)
+ }
+ if !merged {
+ t.Error("expected MergeNoblock to return merged=true when semaphore is free")
+ }
+
+ // After merging, NumSets must return 1 and must not deadlock.
+ result := make(chan int, 1)
+ go func() {
+ result <- g.NumSets()
+ }()
+
+ select {
+ case n := <-result:
+ if n != 1 {
+ t.Errorf("expected 1 set after merge, got %d", n)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("NumSets deadlocked after normal MergeNoblock (unexpected)")
+ }
+}