-
Notifications
You must be signed in to change notification settings - Fork 82
/
actors_test.go
67 lines (57 loc) · 1.63 KB
/
actors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
}
}
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())
}