Mining/pkg/mining/supervisor_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

58 lines
1.3 KiB
Go

package mining
import (
"context"
"sync/atomic"
"testing"
"time"
)
func TestSupervisor_RegisterTask_Good(t *testing.T) {
supervisor := NewTaskSupervisor()
defer supervisor.Stop()
var executed atomic.Bool
supervisor.RegisterTask("test-task", func(ctx context.Context) {
executed.Store(true)
<-ctx.Done()
}, time.Second, 0)
supervisor.Start()
time.Sleep(50 * time.Millisecond)
if !executed.Load() {
t.Fatal("expected task to have executed")
}
}
func TestSupervisor_RegisterTask_Bad(t *testing.T) {
supervisor := NewTaskSupervisor()
defer supervisor.Stop()
// Start with no tasks — should not panic
supervisor.Start()
_, _, found := supervisor.GetTaskStatus("nonexistent")
if found {
t.Fatal("expected task not found")
}
}
func TestSupervisor_RegisterTask_Ugly(t *testing.T) {
supervisor := NewTaskSupervisor()
var panicCount atomic.Int32
supervisor.RegisterTask("panicking-task", func(ctx context.Context) {
panicCount.Add(1)
panic("intentional panic for testing")
}, 10*time.Millisecond, 2)
supervisor.Start()
time.Sleep(100 * time.Millisecond)
supervisor.Stop()
// Task should have been restarted after panic
if panicCount.Load() < 2 {
t.Fatalf("expected at least 2 executions after panic recovery, got %d", panicCount.Load())
}
}