...

Source file src/github.com/google/pprof/internal/driver/tempfile_test.go

Documentation: github.com/google/pprof/internal/driver

     1  package driver
     2  
     3  import (
     4  	"os"
     5  	"sync"
     6  	"testing"
     7  )
     8  
     9  func TestNewTempFile(t *testing.T) {
    10  	const n = 100
    11  	// Line up ready to execute goroutines with a read-write lock.
    12  	var mu sync.RWMutex
    13  	mu.Lock()
    14  	var wg sync.WaitGroup
    15  	errc := make(chan error, n)
    16  	for i := 0; i < n; i++ {
    17  		wg.Add(1)
    18  		go func() {
    19  			mu.RLock()
    20  			defer mu.RUnlock()
    21  			defer wg.Done()
    22  			f, err := newTempFile(os.TempDir(), "profile", ".tmp")
    23  			errc <- err
    24  			deferDeleteTempFile(f.Name())
    25  			f.Close()
    26  		}()
    27  	}
    28  	// Start the file creation race.
    29  	mu.Unlock()
    30  	// Wait for the goroutines to finish.
    31  	wg.Wait()
    32  
    33  	for i := 0; i < n; i++ {
    34  		if err := <-errc; err != nil {
    35  			t.Fatalf("newTempFile(): got %v, want no error", err)
    36  		}
    37  	}
    38  	if len(tempFiles) != n {
    39  		t.Errorf("len(tempFiles): got %d, want %d", len(tempFiles), n)
    40  	}
    41  	names := map[string]bool{}
    42  	for _, name := range tempFiles {
    43  		if names[name] {
    44  			t.Errorf("got temp file %s created multiple times", name)
    45  			break
    46  		}
    47  		names[name] = true
    48  	}
    49  	if err := cleanupTempFiles(); err != nil {
    50  		t.Errorf("cleanupTempFiles(): got error %v, want no error", err)
    51  	}
    52  	if len(tempFiles) != 0 {
    53  		t.Errorf("len(tempFiles) after the cleanup: got %d, want 0", len(tempFiles))
    54  	}
    55  }
    56  

View as plain text