-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure_test.go
144 lines (115 loc) · 3.24 KB
/
closure_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package shutdown
import (
"context"
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type pkgCloser struct {
mu sync.Mutex
isClose bool
err error
}
func (mc *pkgCloser) Close() error {
mc.mu.Lock()
defer mc.mu.Unlock()
mc.isClose = true
return mc.err
}
func TestAppendAndClose(t *testing.T) {
SetPackageClosure(&Lifo{})
once = sync.Once{}
mCloser := &pkgCloser{}
Append(mCloser)
if err := Close(); err != nil || !mCloser.isClose {
t.Fatalf("Expected closer to be closed without errors, got: %v", err)
}
}
func TestAppendAndCloseWithError(t *testing.T) {
SetPackageClosure(&Fifo{})
once = sync.Once{}
expectedErr := errors.New("close error")
mCloser := &pkgCloser{err: expectedErr}
Append(mCloser)
if err := Close(); err == nil || err.Error() != expectedErr.Error() {
t.Fatalf("Expected error: %v, got: %v", expectedErr, err)
}
}
type mockLogger struct {
messages []string
mu sync.Mutex
}
func (ml *mockLogger) Msgf(format string, args ...interface{}) {
ml.mu.Lock()
defer ml.mu.Unlock()
ml.messages = append(ml.messages, fmt.Sprintf(format, args...))
}
func TestWaitForSignals(t *testing.T) {
ml := &mockLogger{}
signals := []os.Signal{os.Interrupt}
go func() {
// Simulate a signal after a short delay
time.Sleep(100 * time.Millisecond)
process, _ := os.FindProcess(os.Getpid())
_ = process.Signal(os.Interrupt)
}()
WaitForSignals(ml, signals...)
if len(ml.messages) == 0 || ml.messages[0] != "Received signal: interrupt" {
t.Errorf("Expected log message about received signal, got: %v", ml.messages)
}
}
func TestWaitForSignalsContext(t *testing.T) {
ml := &mockLogger{}
signals := []os.Signal{os.Interrupt}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
WaitForSignalsContext(ctx, ml, signals...)
if len(ml.messages) == 0 || ml.messages[0] != "Received signal: context deadline exceeded" {
t.Errorf("Expected log message about context deadline, got: %v", ml.messages)
}
}
func TestFn_Close(t *testing.T) {
c := Fn(func() error {
return nil
})
if err := c.Close(); err != nil {
t.Errorf("Expected no error, got: %v", err)
}
}
func getLastLoggedMessage(ml *mockLogger) string {
ml.mu.Lock()
defer ml.mu.Unlock()
if len(ml.messages) == 0 {
return ""
}
return ml.messages[len(ml.messages)-1]
}
func TestCloseOnSignal(t *testing.T) {
logger := &mockLogger{}
go func() {
// Simulate a signal after a short delay
time.Sleep(100 * time.Millisecond)
process, _ := os.FindProcess(os.Getpid())
_ = process.Signal(os.Interrupt)
}()
// Normally, you'd want to simulate the actual os.Signal here, but this example
// just checks if the logger logs the message.
err := CloseOnSignal(logger, os.Interrupt)
assert.NoError(t, err)
assert.Equal(t, "Received signal: interrupt", getLastLoggedMessage(logger))
}
func TestCloseOnSignalContextCancelled(t *testing.T) {
logger := &mockLogger{}
ctx, cancel := context.WithCancel(context.Background())
// Simulate context completion without waiting for the actual os.Signal.
go func() {
cancel()
}()
err := CloseOnSignalContext(ctx, logger, os.Interrupt)
assert.NoError(t, err)
assert.Equal(t, "Received signal: context canceled", getLastLoggedMessage(logger))
}