-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown_test.go
85 lines (83 loc) · 2.08 KB
/
shutdown_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package graceful
import (
"context"
"errors"
"testing"
"time"
)
func TestHandleHandleSignalsWithContext(t *testing.T) {
t.Run("should return nil if shutdown on signal", func(t *testing.T) {
initGrace()
tested := false
_, done := NewShutdownObserver()
go func() {
err := HandleSignalsWithContext(context.Background(), 0)
tested = true
if err != nil {
t.Errorf("expected nil, got %v", err)
}
}()
time.Sleep(100 * time.Millisecond)
err := Shutdown()
if err != nil {
t.Errorf("expected nil, got %v", err)
}
time.Sleep(100 * time.Millisecond)
done()
time.Sleep(100 * time.Millisecond)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
if !tested {
t.Error("expected to complete HandleSignalsWithContext")
}
})
t.Run("should return err if shutdown on context", func(t *testing.T) {
initGrace()
tested := false
_, done := NewShutdownObserver()
ctx, cancel := context.WithCancel(context.Background())
go func() {
err := HandleSignalsWithContext(ctx, 0)
tested = true
if err == nil {
t.Error("expected err, got nil")
}
if !errors.Is(err, context.Canceled) {
t.Errorf("expected '%v' to be in error tree, got '%v'", context.Canceled, err)
}
}()
time.Sleep(100 * time.Millisecond)
cancel()
time.Sleep(100 * time.Millisecond)
done()
time.Sleep(100 * time.Millisecond)
if !tested {
t.Error("expected to complete HandleSignalsWithContext")
}
})
t.Run("should return err if shutdown on timeout", func(t *testing.T) {
initGrace()
tested := false
NewShutdownObserver()
go func() {
err := HandleSignalsWithContext(context.Background(), 50*time.Millisecond)
tested = true
if err == nil {
t.Error("expected err, got nil")
}
if !errors.Is(err, ErrTimeout) {
t.Errorf("expected '%v' to be in error tree, got '%v'", context.Canceled, err)
}
}()
time.Sleep(20 * time.Millisecond)
err := Shutdown()
if err != nil {
t.Errorf("expected nil, got %v", err)
}
time.Sleep(100 * time.Millisecond)
if !tested {
t.Error("expected to complete HandleSignalsWithContext")
}
})
}