vitess.io/vitess@v0.16.2/go/vt/servenv/pprof_test.go (about) 1 //go:build !race 2 3 // Disabling race detector because it doesn't like TestPProfInitWithWaitSig and TestPProfInitWithoutWaitSig, 4 // but the profileStarted variable is updated in response to signals invoked in the tests and works as intended. 5 6 package servenv 7 8 import ( 9 "os/signal" 10 "reflect" 11 "strings" 12 "syscall" 13 "testing" 14 "time" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestParseProfileFlag(t *testing.T) { 20 tests := []struct { 21 arg string 22 want *profile 23 wantErr bool 24 }{ 25 {"", nil, false}, 26 {"mem", &profile{mode: profileMemHeap, rate: 4096}, false}, 27 {"mem,rate=1234", &profile{mode: profileMemHeap, rate: 1234}, false}, 28 {"mem,rate", nil, true}, 29 {"mem,rate=foobar", nil, true}, 30 {"mem=allocs", &profile{mode: profileMemAllocs, rate: 4096}, false}, 31 {"mem=allocs,rate=420", &profile{mode: profileMemAllocs, rate: 420}, false}, 32 {"block", &profile{mode: profileBlock, rate: 1}, false}, 33 {"block,rate=4", &profile{mode: profileBlock, rate: 4}, false}, 34 {"cpu", &profile{mode: profileCPU}, false}, 35 {"cpu,quiet", &profile{mode: profileCPU, quiet: true}, false}, 36 {"cpu,quiet=true", &profile{mode: profileCPU, quiet: true}, false}, 37 {"cpu,quiet=false", &profile{mode: profileCPU, quiet: false}, false}, 38 {"cpu,quiet=foobar", nil, true}, 39 {"cpu,path=", &profile{mode: profileCPU, path: ""}, false}, 40 {"cpu,path", nil, true}, 41 {"cpu,path=a", &profile{mode: profileCPU, path: "a"}, false}, 42 {"cpu,path=a/b/c/d", &profile{mode: profileCPU, path: "a/b/c/d"}, false}, 43 {"cpu,waitSig", &profile{mode: profileCPU, waitSig: true}, false}, 44 {"cpu,path=a/b,waitSig", &profile{mode: profileCPU, waitSig: true, path: "a/b"}, false}, 45 } 46 for _, tt := range tests { 47 t.Run(tt.arg, func(t *testing.T) { 48 var profileFlag []string 49 if tt.arg != "" { 50 profileFlag = strings.Split(tt.arg, ",") 51 } 52 got, err := parseProfileFlag(profileFlag) 53 if (err != nil) != tt.wantErr { 54 t.Errorf("parseProfileFlag() error = %v, wantErr %v", err, tt.wantErr) 55 return 56 } 57 if !reflect.DeepEqual(got, tt.want) { 58 t.Errorf("parseProfileFlag() got = %v, want %v", got, tt.want) 59 } 60 }) 61 } 62 } 63 64 // with waitSig, we should start with profiling off and toggle on-off-on-off 65 func TestPProfInitWithWaitSig(t *testing.T) { 66 signal.Reset(syscall.SIGUSR1) 67 pprofFlag = strings.Split("cpu,waitSig", ",") 68 69 pprofInit() 70 time.Sleep(1 * time.Second) 71 assert.Equal(t, uint32(0), profileStarted) 72 73 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 74 time.Sleep(1 * time.Second) 75 assert.Equal(t, uint32(1), profileStarted) 76 77 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 78 time.Sleep(1 * time.Second) 79 assert.Equal(t, uint32(0), profileStarted) 80 81 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 82 time.Sleep(1 * time.Second) 83 assert.Equal(t, uint32(1), profileStarted) 84 85 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 86 time.Sleep(1 * time.Second) 87 assert.Equal(t, uint32(0), profileStarted) 88 } 89 90 // without waitSig, we should start with profiling on and toggle off-on-off 91 func TestPProfInitWithoutWaitSig(t *testing.T) { 92 signal.Reset(syscall.SIGUSR1) 93 pprofFlag = strings.Split("cpu", ",") 94 95 pprofInit() 96 time.Sleep(1 * time.Second) 97 assert.Equal(t, uint32(1), profileStarted) 98 99 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 100 time.Sleep(1 * time.Second) 101 assert.Equal(t, uint32(0), profileStarted) 102 103 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 104 time.Sleep(1 * time.Second) 105 assert.Equal(t, uint32(1), profileStarted) 106 107 syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) 108 time.Sleep(1 * time.Second) 109 assert.Equal(t, uint32(0), profileStarted) 110 }