Skip to content

Commit bf963c8

Browse files
committed
Add unit test
Signed-off-by: Luke Addison <lukeaddison785@gmail.com>
1 parent 9501679 commit bf963c8

File tree

2 files changed

+22
-52
lines changed

2 files changed

+22
-52
lines changed

pkg/manager/signals/signal.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import (
2323
"time"
2424
)
2525

26-
var onlyOneSignalHandler = make(chan struct{})
26+
var (
27+
onlyOneSignalHandler = make(chan struct{})
28+
// Define global signal channel for testing
29+
signalCh = make(chan os.Signal, 2)
30+
)
2731

2832
// SetupSignalHandlerWithDelay registers for SIGTERM and SIGINT. A context is
2933
// returned which is canceled on one of these signals after waiting for the
@@ -37,17 +41,16 @@ func SetupSignalHandlerWithDelay(delay time.Duration) context.Context {
3741

3842
ctx, cancel := context.WithCancel(context.Background())
3943

40-
c := make(chan os.Signal, 2)
41-
signal.Notify(c, shutdownSignals...)
44+
signal.Notify(signalCh, shutdownSignals...)
4245
go func() {
43-
<-c
46+
<-signalCh
4447
// Cancel the context after delaying for the specified duration but
4548
// avoid blocking if a second signal is caught
4649
go func() {
4750
<-time.After(delay)
4851
cancel()
4952
}()
50-
<-c
53+
<-signalCh
5154
os.Exit(1) // second signal. Exit directly.
5255
}()
5356

pkg/manager/signals/signal_test.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ limitations under the License.
1717
package signals
1818

1919
import (
20-
"fmt"
2120
"os"
22-
"os/signal"
23-
"sync"
2421
"time"
2522

2623
. "github.com/onsi/ginkgo/v2"
@@ -31,53 +28,23 @@ var _ = Describe("runtime signal", func() {
3128

3229
Context("SignalHandler Test", func() {
3330

34-
It("test signal handler", func() {
35-
ctx := SetupSignalHandler()
36-
task := &Task{
37-
ticker: time.NewTicker(time.Second * 2),
38-
}
39-
c := make(chan os.Signal, 1)
40-
signal.Notify(c, os.Interrupt)
41-
task.wg.Add(1)
42-
go func(c chan os.Signal) {
43-
defer task.wg.Done()
44-
task.Run(c)
45-
}(c)
31+
It("test signal handler with delay", func() {
32+
delay := time.Second
33+
ctx := SetupSignalHandlerWithDelay(delay)
4634

47-
select {
48-
case sig := <-c:
49-
fmt.Printf("Got %s signal. Aborting...\n", sig)
50-
case _, ok := <-ctx.Done():
51-
Expect(ok).To(BeFalse())
52-
}
35+
// Save time before sending signal
36+
beforeSendingSignal := time.Now()
37+
38+
// Send signal
39+
signalCh <- os.Interrupt
40+
41+
_, ok := <-ctx.Done()
42+
// Verify that the channel was closed
43+
Expect(ok).To(BeFalse())
44+
// Verify that our delay was respected
45+
Expect(time.Since(beforeSendingSignal)).To(BeNumerically(">=", delay))
5346
})
5447

5548
})
5649

5750
})
58-
59-
type Task struct {
60-
wg sync.WaitGroup
61-
ticker *time.Ticker
62-
}
63-
64-
func (t *Task) Run(c chan os.Signal) {
65-
for {
66-
go sendSignal(c)
67-
handle()
68-
}
69-
}
70-
71-
func handle() {
72-
for i := 0; i < 5; i++ {
73-
fmt.Print("#")
74-
time.Sleep(time.Millisecond * 100)
75-
}
76-
fmt.Println()
77-
}
78-
79-
func sendSignal(stopChan chan os.Signal) {
80-
fmt.Printf("...")
81-
time.Sleep(1 * time.Second)
82-
stopChan <- os.Interrupt
83-
}

0 commit comments

Comments
 (0)