Mining/pkg/mining/metrics_test.go
Claude 63a9107f9c
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
ax(batch): add missing test files and fix pre-existing test compilation errors
Adds test files for 6 source files that had no corresponding test:
bufpool_test.go (mining + node), metrics_test.go, version_test.go,
supervisor_test.go, mining_profile_test.go. Each follows
TestFilename_Function_{Good,Bad,Ugly} convention.

Also fixes 2 pre-existing compilation errors:
- ratelimiter_test.go: rl -> rateLimiter (leftover from AX rename)
- worker_test.go: worker.node -> worker.nodeManager (field was renamed)

Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 18:30:42 +01:00

77 lines
2.1 KiB
Go

package mining
import (
"testing"
"time"
)
func TestMetrics_LatencyHistogram_Good(t *testing.T) {
histogram := NewLatencyHistogram(10)
histogram.Record(42 * time.Millisecond)
histogram.Record(58 * time.Millisecond)
if histogram.Count() != 2 {
t.Fatalf("expected count 2, got %d", histogram.Count())
}
avg := histogram.Average()
if avg != 50*time.Millisecond {
t.Fatalf("expected average 50ms, got %v", avg)
}
}
func TestMetrics_LatencyHistogram_Bad(t *testing.T) {
histogram := NewLatencyHistogram(10)
// No samples recorded — Average should be 0
if histogram.Average() != 0 {
t.Fatalf("expected 0 average for empty histogram, got %v", histogram.Average())
}
if histogram.Count() != 0 {
t.Fatalf("expected 0 count, got %d", histogram.Count())
}
}
func TestMetrics_LatencyHistogram_Ugly(t *testing.T) {
// Ring buffer overflow: maxSize=2, insert 3 samples
histogram := NewLatencyHistogram(2)
histogram.Record(10 * time.Millisecond)
histogram.Record(20 * time.Millisecond)
histogram.Record(30 * time.Millisecond)
if histogram.Count() != 2 {
t.Fatalf("expected count 2 after overflow, got %d", histogram.Count())
}
// Oldest sample (10ms) should have been evicted
avg := histogram.Average()
if avg != 25*time.Millisecond {
t.Fatalf("expected average 25ms (20+30)/2, got %v", avg)
}
}
func TestMetrics_RecordRequest_Good(t *testing.T) {
before := DefaultMetrics.RequestsTotal.Load()
RecordRequest(false, 5*time.Millisecond)
after := DefaultMetrics.RequestsTotal.Load()
if after != before+1 {
t.Fatalf("expected total to increase by 1, got %d -> %d", before, after)
}
}
func TestMetrics_RecordRequest_Bad(t *testing.T) {
beforeErr := DefaultMetrics.RequestsErrored.Load()
RecordRequest(true, 5*time.Millisecond)
afterErr := DefaultMetrics.RequestsErrored.Load()
if afterErr != beforeErr+1 {
t.Fatalf("expected errored to increase by 1, got %d -> %d", beforeErr, afterErr)
}
}
func TestMetrics_RecordRequest_Ugly(t *testing.T) {
snapshot := GetMetricsSnapshot()
if snapshot == nil {
t.Fatal("expected non-nil snapshot")
}
if _, ok := snapshot["requests_total"]; !ok {
t.Fatal("expected requests_total in snapshot")
}
}