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()) } }