summaryrefslogtreecommitdiff
path: root/internal/mapr/safe_aggregateset_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/safe_aggregateset_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/safe_aggregateset_test.go')
-rw-r--r--internal/mapr/safe_aggregateset_test.go147
1 files changed, 147 insertions, 0 deletions
diff --git a/internal/mapr/safe_aggregateset_test.go b/internal/mapr/safe_aggregateset_test.go
new file mode 100644
index 0000000..327987a
--- /dev/null
+++ b/internal/mapr/safe_aggregateset_test.go
@@ -0,0 +1,147 @@
+package mapr
+
+import (
+ "sync"
+ "testing"
+)
+
+// TestSafeAggregateSetConcurrency tests that SafeAggregateSet handles concurrent operations correctly.
+func TestSafeAggregateSetConcurrency(t *testing.T) {
+ safeSet := NewSafeAggregateSet()
+
+ // Number of concurrent goroutines
+ numGoroutines := 100
+ // Number of operations per goroutine
+ opsPerGoroutine := 1000
+
+ var wg sync.WaitGroup
+ wg.Add(numGoroutines)
+
+ // Launch concurrent goroutines
+ for i := 0; i < numGoroutines; i++ {
+ go func(id int) {
+ defer wg.Done()
+
+ for j := 0; j < opsPerGoroutine; j++ {
+ // Test Count operation
+ err := safeSet.Aggregate("count", Count, "1", false)
+ if err != nil {
+ t.Errorf("Error in Count aggregation: %v", err)
+ }
+
+ // Test Sum operation
+ err = safeSet.Aggregate("sum", Sum, "10.5", false)
+ if err != nil {
+ t.Errorf("Error in Sum aggregation: %v", err)
+ }
+
+ // Test Last operation
+ err = safeSet.Aggregate("last", Last, "value", false)
+ if err != nil {
+ t.Errorf("Error in Last aggregation: %v", err)
+ }
+
+ // Test Min operation
+ err = safeSet.Aggregate("min", Min, "5.0", false)
+ if err != nil {
+ t.Errorf("Error in Min aggregation: %v", err)
+ }
+
+ // Test Max operation
+ err = safeSet.Aggregate("max", Max, "15.0", false)
+ if err != nil {
+ t.Errorf("Error in Max aggregation: %v", err)
+ }
+
+ // Increment samples
+ safeSet.IncrementSamples()
+ }
+ }(i)
+ }
+
+ // Wait for all goroutines to complete
+ wg.Wait()
+
+ // Verify results
+ clone := safeSet.Clone()
+
+ // Check Count
+ expectedCount := float64(numGoroutines * opsPerGoroutine)
+ if clone.FValues["count"] != expectedCount {
+ t.Errorf("Expected count %f, got %f", expectedCount, clone.FValues["count"])
+ }
+
+ // Check Sum
+ expectedSum := float64(numGoroutines * opsPerGoroutine) * 10.5
+ if clone.FValues["sum"] != expectedSum {
+ t.Errorf("Expected sum %f, got %f", expectedSum, clone.FValues["sum"])
+ }
+
+ // Check Min
+ if clone.FValues["min"] != 5.0 {
+ t.Errorf("Expected min 5.0, got %f", clone.FValues["min"])
+ }
+
+ // Check Max
+ if clone.FValues["max"] != 15.0 {
+ t.Errorf("Expected max 15.0, got %f", clone.FValues["max"])
+ }
+
+ // Check Samples
+ expectedSamples := numGoroutines * opsPerGoroutine
+ if clone.Samples != expectedSamples {
+ t.Errorf("Expected samples %d, got %d", expectedSamples, clone.Samples)
+ }
+
+ // Check Last (should be "value")
+ if clone.SValues["last"] != "value" {
+ t.Errorf("Expected last 'value', got '%s'", clone.SValues["last"])
+ }
+}
+
+// TestSafeAggregateSetClone tests that cloning creates an independent copy.
+func TestSafeAggregateSetClone(t *testing.T) {
+ original := NewSafeAggregateSet()
+
+ // Add some data
+ original.Aggregate("count", Count, "1", false)
+ original.Aggregate("sum", Sum, "100", false)
+ original.Aggregate("last", Last, "original", false)
+ original.IncrementSamples()
+
+ // Clone the set
+ clone := original.Clone()
+
+ // Modify the original
+ original.Aggregate("count", Count, "1", false)
+ original.Aggregate("sum", Sum, "50", false)
+ original.Aggregate("last", Last, "modified", false)
+ original.IncrementSamples()
+
+ // Verify clone is unchanged
+ if clone.FValues["count"] != 1 {
+ t.Errorf("Clone count should be 1, got %f", clone.FValues["count"])
+ }
+
+ if clone.FValues["sum"] != 100 {
+ t.Errorf("Clone sum should be 100, got %f", clone.FValues["sum"])
+ }
+
+ if clone.SValues["last"] != "original" {
+ t.Errorf("Clone last should be 'original', got '%s'", clone.SValues["last"])
+ }
+
+ if clone.Samples != 1 {
+ t.Errorf("Clone samples should be 1, got %d", clone.Samples)
+ }
+
+ // Verify original has changed
+ originalClone := original.Clone()
+ if originalClone.FValues["count"] != 2 {
+ t.Errorf("Original count should be 2, got %f", originalClone.FValues["count"])
+ }
+
+ if originalClone.FValues["sum"] != 150 {
+ t.Errorf("Original sum should be 150, got %f", originalClone.FValues["sum"])
+ }
+} \ No newline at end of file