Files
oklog-run/actors_test.go
T
dualfroz ae63f3a6f0 fix: assign target in SignalError.As instead of only checking its type
errors.As calls SignalError.As when it cannot match the concrete type
directly, e.g. for `var target SignalError; errors.As(err, &target)`.
The previous implementation matched target's type and returned true
but never wrote to it, so callers got a successful errors.As result
with a zero-valued SignalError and no indication the signal was lost.

Add a regression test asserting the target is actually populated.
2026-09-04 06:21:22 +02:00

93 lines
2.6 KiB
Go

package run
import (
"context"
"errors"
"os"
"testing"
"time"
)
func TestContextHandler(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var rg Group
rg.Add(ContextHandler(ctx))
errc := make(chan error, 1)
go func() { errc <- rg.Run() }()
cancel()
select {
case err := <-errc:
if want, have := context.Canceled, err; !errors.Is(have, want) {
t.Errorf("error: want %v, have %v", want, have)
}
case <-time.After(time.Second):
t.Errorf("timeout waiting for error after cancel")
}
}
func TestSignalError(t *testing.T) {
testc := make(chan os.Signal, 1)
ctx := putTestSigChan(context.Background(), testc)
var rg Group
rg.Add(SignalHandler(ctx, os.Interrupt))
testc <- os.Interrupt
err := rg.Run()
var sigerr *SignalError
if want, have := true, errors.As(err, &sigerr); want != have {
t.Errorf("errors.As(err, &sigerr): want %v, have %v", want, have)
}
if sigerr != nil {
if want, have := os.Interrupt, sigerr.Signal; want != have {
t.Errorf("sigerr.Signal: want %v, have %v", want, have)
}
}
if sigerr := &(SignalError{}); !errors.As(err, &sigerr) {
t.Errorf("errors.As(err, <inline sigerr>): failed")
}
if want, have := true, errors.As(err, &(SignalError{})); want != have {
t.Errorf("errors.As(err, &(SignalError{})): want %v, have %v", want, have)
}
if want, have := true, errors.Is(err, ErrSignal); want != have {
t.Errorf("errors.Is(err, ErrSignal): want %v, have %v", want, have)
}
}
// TestSignalErrorAsAssignsTarget verifies that errors.As does not merely
// report a match for a value-typed SignalError target, but actually
// populates it, per the errors.As contract ("sets target to that error
// value"). A prior fix for SignalError's As method (#27) made errors.As
// return true for such a target without ever assigning to it, silently
// discarding the signal.
func TestSignalErrorAsAssignsTarget(t *testing.T) {
testc := make(chan os.Signal, 1)
ctx := putTestSigChan(context.Background(), testc)
var rg Group
rg.Add(SignalHandler(ctx, os.Interrupt))
testc <- os.Interrupt
err := rg.Run()
var target SignalError
if want, have := true, errors.As(err, &target); want != have {
t.Fatalf("errors.As(err, &target): want %v, have %v", want, have)
}
if want, have := os.Interrupt, target.Signal; want != have {
t.Errorf("target.Signal: want %v, have %v (errors.As reported success but did not assign the target)", want, have)
}
}
func TestSignalHandlerNil(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var rg Group
rg.Add(SignalHandler(ctx, os.Interrupt))
cancel()
t.Logf("%v", rg.Run())
}