File: tempfile_test.go

package info (click to toggle)
golang-github-google-pprof 0.0~git20211008.947d60d-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 5,036 kB
  • sloc: sh: 70; ansic: 11; makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package driver

import (
	"os"
	"sync"
	"testing"
)

func TestNewTempFile(t *testing.T) {
	const n = 100
	// Line up ready to execute goroutines with a read-write lock.
	var mu sync.RWMutex
	mu.Lock()
	var wg sync.WaitGroup
	errc := make(chan error, n)
	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() {
			mu.RLock()
			defer mu.RUnlock()
			defer wg.Done()
			f, err := newTempFile(os.TempDir(), "profile", ".tmp")
			errc <- err
			deferDeleteTempFile(f.Name())
			f.Close()
		}()
	}
	// Start the file creation race.
	mu.Unlock()
	// Wait for the goroutines to finish.
	wg.Wait()

	for i := 0; i < n; i++ {
		if err := <-errc; err != nil {
			t.Fatalf("newTempFile(): got %v, want no error", err)
		}
	}
	if len(tempFiles) != n {
		t.Errorf("len(tempFiles): got %d, want %d", len(tempFiles), n)
	}
	names := map[string]bool{}
	for _, name := range tempFiles {
		if names[name] {
			t.Errorf("got temp file %s created multiple times", name)
			break
		}
		names[name] = true
	}
	if err := cleanupTempFiles(); err != nil {
		t.Errorf("cleanupTempFiles(): got error %v, want no error", err)
	}
	if len(tempFiles) != 0 {
		t.Errorf("len(tempFiles) after the cleanup: got %d, want 0", len(tempFiles))
	}
}