1// This is a binary used to run automated tests on. It's compiled when the tests 2// run and executed/stopped by the test. For more information check 3// killer_test.go 4package main 5 6import ( 7 "context" 8 "fmt" 9 "os" 10 "os/signal" 11 "syscall" 12 "time" 13) 14 15const SkipTerminateOption = "skip-terminate-signals" 16 17func main() { 18 if len(os.Args) < 2 { 19 fmt.Printf("Usage: %s duration [%s]\n", os.Args[0], SkipTerminateOption) 20 os.Exit(1) 21 } 22 23 duration, err := time.ParseDuration(os.Args[1]) 24 if err != nil { 25 panic(fmt.Sprintf("Couldn't parse duration argument: %v", err)) 26 } 27 28 skipTermination := len(os.Args) > 2 && os.Args[2] == SkipTerminateOption 29 30 ctx, cancel := context.WithCancel(context.Background()) 31 32 signalCh := make(chan os.Signal) 33 signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT) 34 35 go func() { 36 fmt.Println("Waiting for signals (SIGTERM, SIGINT)...") 37 sig := <-signalCh 38 39 fmt.Printf("Received signal: %v\n", sig) 40 41 if skipTermination { 42 fmt.Printf("but ignoring it due to %q option used\n", SkipTerminateOption) 43 return 44 } 45 46 fmt.Println("forcing termination...") 47 cancel() 48 }() 49 50 fmt.Printf("Sleeping for %s (PID=%d)\n", duration, os.Getpid()) 51 52 select { 53 case <-time.After(duration): 54 fmt.Println("Sleep duration achieved") 55 case <-ctx.Done(): 56 fmt.Println("Forced to quit by signal; terminating") 57 os.Exit(1) 58 } 59} 60