Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Addison <lukeaddison785@gmail.com>
  • Loading branch information
dippynark committed Dec 2, 2023
1 parent 9501679 commit bf963c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
13 changes: 8 additions & 5 deletions pkg/manager/signals/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import (
"time"
)

var onlyOneSignalHandler = make(chan struct{})
var (
onlyOneSignalHandler = make(chan struct{})
// Define global signal channel for testing
signalCh = make(chan os.Signal, 2)
)

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

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

c := make(chan os.Signal, 2)
signal.Notify(c, shutdownSignals...)
signal.Notify(signalCh, shutdownSignals...)
go func() {
<-c
<-signalCh
// Cancel the context after delaying for the specified duration but
// avoid blocking if a second signal is caught
go func() {
<-time.After(delay)
cancel()
}()
<-c
<-signalCh
os.Exit(1) // second signal. Exit directly.
}()

Expand Down
61 changes: 14 additions & 47 deletions pkg/manager/signals/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ limitations under the License.
package signals

import (
"fmt"
"os"
"os/signal"
"sync"
"time"

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

Context("SignalHandler Test", func() {

It("test signal handler", func() {
ctx := SetupSignalHandler()
task := &Task{
ticker: time.NewTicker(time.Second * 2),
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
task.wg.Add(1)
go func(c chan os.Signal) {
defer task.wg.Done()
task.Run(c)
}(c)
It("test signal handler with delay", func() {
delay := time.Second
ctx := SetupSignalHandlerWithDelay(delay)

select {
case sig := <-c:
fmt.Printf("Got %s signal. Aborting...\n", sig)
case _, ok := <-ctx.Done():
Expect(ok).To(BeFalse())
}
// Save time before sending signal
beforeSendingSignal := time.Now()

// Send signal
signalCh <- os.Interrupt

_, ok := <-ctx.Done()
// Verify that the channel was closed
Expect(ok).To(BeFalse())
// Verify that our delay was respected
Expect(time.Since(beforeSendingSignal)).To(BeNumerically(">=", delay))
})

})

})

type Task struct {
wg sync.WaitGroup
ticker *time.Ticker
}

func (t *Task) Run(c chan os.Signal) {
for {
go sendSignal(c)
handle()
}
}

func handle() {
for i := 0; i < 5; i++ {
fmt.Print("#")
time.Sleep(time.Millisecond * 100)
}
fmt.Println()
}

func sendSignal(stopChan chan os.Signal) {
fmt.Printf("...")
time.Sleep(1 * time.Second)
stopChan <- os.Interrupt
}

0 comments on commit bf963c8

Please sign in to comment.