...

Text file src/cmd/go/testdata/script/test_finished_subtest_goroutines.txt

Documentation: cmd/go/testdata/script

     1# Regression test for https://golang.org/issue/45127:
     2# Goroutines for completed parallel subtests should exit immediately,
     3# not block until earlier subtests have finished.
     4
     5[short] skip
     6
     7! go test .
     8stdout 'panic: slow failure'
     9! stdout '\[chan send'
    10
    11-- go.mod --
    12module golang.org/issue45127
    13
    14go 1.16
    15-- issue45127_test.go --
    16package main
    17
    18import (
    19	"fmt"
    20	"runtime"
    21	"runtime/debug"
    22	"sync"
    23	"testing"
    24)
    25
    26func TestTestingGoroutineLeak(t *testing.T) {
    27	debug.SetTraceback("all")
    28
    29	var wg sync.WaitGroup
    30	const nFast = 10
    31
    32	t.Run("slow", func(t *testing.T) {
    33		t.Parallel()
    34		wg.Wait()
    35		for i := 0; i < nFast; i++ {
    36			// If the subtest goroutines are going to park on the channel
    37			// send, allow them to park now. If they're not going to park,
    38			// make sure they have had a chance to run to completion so
    39			// that they aren't spuriously parked when we panic.
    40			runtime.Gosched()
    41		}
    42		panic("slow failure")
    43	})
    44
    45	wg.Add(nFast)
    46	for i := 0; i < nFast; i++ {
    47		t.Run(fmt.Sprintf("leaky%d", i), func(t *testing.T) {
    48			t.Parallel()
    49			wg.Done()
    50		})
    51	}
    52}

View as plain text