sigs.k8s.io/controller-runtime@v0.18.2/pkg/manager/signals/signal_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package signals 18 19 import ( 20 "fmt" 21 "os" 22 "os/signal" 23 "sync" 24 "time" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 ) 29 30 var _ = Describe("runtime signal", func() { 31 32 Context("SignalHandler Test", func() { 33 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) 46 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 } 53 }) 54 55 }) 56 57 }) 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 }