Skip to content
Myr

options_pprof_test.go

This file

Path

src/options_pprof_test.go

89 lines · 2.0 KiB · Go

Last changed by

Not known.

History of this file

Take it

1//go:build pprof2// +build pprof34package fzf56import (7	"bytes"8	"flag"9	"os"10	"os/exec"11	"path/filepath"12	"testing"1314	"github.com/junegunn/fzf/src/util"15)1617// runInitProfileTests is an internal flag used TestInitProfiling18var runInitProfileTests = flag.Bool("test-init-profile", false, "run init profile tests")1920func TestInitProfiling(t *testing.T) {21	if testing.Short() {22		t.Skip("short test")23	}2425	// Run this test in a separate process since it interferes with26	// profiling and modifies the global atexit state. Without this27	// running `go test -bench . -cpuprofile cpu.out` will fail.28	if !*runInitProfileTests {29		t.Parallel()3031		// Make sure we are not the child process.32		if os.Getenv("_FZF_CHILD_PROC") != "" {33			t.Fatal("already running as child process!")34		}3536		cmd := exec.Command(os.Args[0],37			"-test.timeout", "30s",38			"-test.run", "^"+t.Name()+"$",39			"-test-init-profile",40		)41		cmd.Env = append(os.Environ(), "_FZF_CHILD_PROC=1")4243		out, err := cmd.CombinedOutput()44		out = bytes.TrimSpace(out)45		if err != nil {46			t.Fatalf("Child test process failed: %v:\n%s", err, out)47		}48		// Make sure the test actually ran49		if bytes.Contains(out, []byte("no tests to run")) {50			t.Fatalf("Failed to run test %q:\n%s", t.Name(), out)51		}52		return53	}5455	// Child process5657	tempdir := t.TempDir()58	t.Cleanup(util.RunAtExitFuncs)5960	o := Options{61		CPUProfile:   filepath.Join(tempdir, "cpu.prof"),62		MEMProfile:   filepath.Join(tempdir, "mem.prof"),63		BlockProfile: filepath.Join(tempdir, "block.prof"),64		MutexProfile: filepath.Join(tempdir, "mutex.prof"),65	}66	if err := o.initProfiling(); err != nil {67		t.Fatal(err)68	}6970	profiles := []string{71		o.CPUProfile,72		o.MEMProfile,73		o.BlockProfile,74		o.MutexProfile,75	}76	for _, name := range profiles {77		if _, err := os.Stat(name); err != nil {78			t.Errorf("Failed to create profile %s: %v", filepath.Base(name), err)79		}80	}8182	util.RunAtExitFuncs()8384	for _, name := range profiles {85		if _, err := os.Stat(name); err != nil {86			t.Errorf("Failed to write profile %s: %v", filepath.Base(name), err)87		}88	}89}