-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraceful_test.go
More file actions
76 lines (60 loc) · 1.33 KB
/
graceful_test.go
File metadata and controls
76 lines (60 loc) · 1.33 KB
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
package graceful
import (
"context"
"sync"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func TestContextSignal(t *testing.T) {
ctx := NewContext(context.Background(), WithLogger(zaptest.NewLogger(t)))
mu := sync.Mutex{}
expected := 3
closed := 0
counter := func() {
time.Sleep(time.Second)
mu.Lock()
closed++
mu.Unlock()
}
go ctx.Closer(counter)
go ctx.Closer(counter)
go ctx.Closer(counter)
go func() {
time.Sleep(time.Second)
err := syscall.Kill(syscall.Getpid(), syscall.SIGINT)
require.NoError(t, err)
}()
ctx.AwaitKillSignal()
mu.Lock()
require.Equal(t, expected, closed)
mu.Unlock()
}
func TestForceContextSignal(t *testing.T) {
ctx := NewContext(context.Background(), WithLogger(zaptest.NewLogger(t)))
mu := sync.Mutex{}
expected := 0
closed := 0
counter := func() {
time.Sleep(10 * time.Second)
mu.Lock()
closed++
mu.Unlock()
}
go ctx.Closer(counter)
go ctx.Closer(counter)
go ctx.Closer(counter)
go func() {
time.Sleep(time.Second)
// NOTE: the second signal represents the force kill
syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint:errcheck
time.Sleep(time.Second)
syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint:errcheck
}()
ctx.AwaitKillSignal()
mu.Lock()
require.Equal(t, expected, closed)
mu.Unlock()
}