mirror of
https://github.com/status-im/status-go.git
synced 2025-01-10 14:47:06 +00:00
83ad76637a
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package profiling
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestProfilingCPU(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
err := StartCPUProfile(dir)
|
|
require.NoError(t, err)
|
|
|
|
// Block for a bit to collect some metrics.
|
|
time.Sleep(time.Second)
|
|
|
|
err = StopCPUProfile()
|
|
require.NoError(t, err)
|
|
|
|
// Verify that the file has some content.
|
|
file, err := os.Open(filepath.Join(dir, CPUFilename))
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
err := file.Close()
|
|
require.NoError(t, err)
|
|
}()
|
|
|
|
t.Logf("CPU profile saved in %s for %s", filepath.Join(dir, CPUFilename), os.Args[0])
|
|
|
|
info, err := file.Stat()
|
|
require.NoError(t, err)
|
|
require.True(t, info.Size() > 0, "a file with CPU profile is empty")
|
|
}
|
|
|
|
func TestProfilingMem(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
err := WriteHeapFile(dir)
|
|
require.NoError(t, err)
|
|
|
|
// Verify that the file has some content.
|
|
file, err := os.Open(filepath.Join(dir, MemFilename))
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
err := file.Close()
|
|
require.NoError(t, err)
|
|
}()
|
|
|
|
t.Logf("Memory profile saved in %s for %s", filepath.Join(dir, MemFilename), os.Args[0])
|
|
|
|
info, err := file.Stat()
|
|
require.NoError(t, err)
|
|
require.True(t, info.Size() > 0, "a file with memory profile is empty")
|
|
}
|