github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/config_manager/signal_test.go (about) 1 //go:build linux || darwin 2 3 /* 4 Copyright (C) 2022-2023 ApeCloud Co., Ltd 5 6 This file is part of KubeBlocks project 7 8 This program is free software: you can redistribute it and/or modify 9 it under the terms of the GNU Affero General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU Affero General Public License for more details. 17 18 You should have received a copy of the GNU Affero General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 package configmanager 23 24 import ( 25 "context" 26 "os" 27 "os/signal" 28 "syscall" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestSendSignal(t *testing.T) { 36 ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGUSR1) 37 defer stop() 38 39 // for not expect signal 40 { 41 err := sendSignal(PID(os.Getpid()), syscall.SIGUSR2) 42 if err != nil { 43 logger.Error(err, "failed to send signal") 44 } 45 select { 46 case <-time.After(time.Second): 47 // walk here 48 logger.Info("missed signal.") 49 require.True(t, true) 50 case <-ctx.Done(): 51 require.True(t, false) 52 } 53 } 54 55 // for expect signal 56 { 57 err := sendSignal(PID(os.Getpid()), syscall.SIGUSR1) 58 if err != nil { 59 logger.Error(err, "failed to send signal") 60 } 61 62 select { 63 case <-time.After(time.Second): 64 // not walk here 65 logger.Info("missed signal.") 66 require.True(t, false) 67 case <-ctx.Done(): 68 require.True(t, true) 69 // prints "context canceled" 70 logger.Info(ctx.Err().Error()) 71 // stop receiving signal notifications as soon as possible. 72 stop() 73 } 74 75 } 76 }