github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/gazelle/profiler_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 ) 8 9 func TestEmptyProfiler(t *testing.T) { 10 dir := t.TempDir() 11 tests := []struct { 12 name string 13 cpuProfile string 14 memProfile string 15 }{ 16 { 17 name: "cpuProfile", 18 cpuProfile: filepath.Join(dir, "cpu.prof"), 19 }, 20 { 21 name: "memProfile", 22 memProfile: filepath.Join(dir, "mem.prof"), 23 }, 24 { 25 name: "empty", 26 }, 27 } 28 29 for _, test := range tests { 30 t.Run("", func(t *testing.T) { 31 p, err := newProfiler(test.cpuProfile, test.memProfile) 32 if err != nil { 33 t.Fatalf("newProfiler failed: %v", err) 34 } 35 if err := p.stop(); err != nil { 36 t.Fatalf("stop failed: %v", err) 37 } 38 }) 39 } 40 } 41 42 func TestProfiler(t *testing.T) { 43 dir := t.TempDir() 44 cpuProfileName := filepath.Join(dir, "cpu.prof") 45 memProfileName := filepath.Join(dir, "mem.prof") 46 t.Cleanup(func() { 47 os.Remove(cpuProfileName) 48 os.Remove(memProfileName) 49 }) 50 51 p, err := newProfiler(cpuProfileName, memProfileName) 52 if err != nil { 53 t.Fatalf("newProfiler failed: %v", err) 54 } 55 if p.cpuProfile == nil { 56 t.Fatal("Expected cpuProfile to be non-nil") 57 } 58 if p.memProfile != memProfileName { 59 t.Fatalf("Expected memProfile to be %s, got %s", memProfileName, p.memProfile) 60 } 61 62 if err := p.stop(); err != nil { 63 t.Fatalf("stop failed: %v", err) 64 } 65 66 if _, err := os.Stat(cpuProfileName); os.IsNotExist(err) { 67 t.Fatalf("CPU profile file %s was not created", cpuProfileName) 68 } 69 70 if _, err := os.Stat(memProfileName); os.IsNotExist(err) { 71 t.Fatalf("Memory profile file %s was not created", memProfileName) 72 } 73 }