Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes70277.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"runtime/pprof"
    10  	"time"
    11  )
    12  
    13  func init() {
    14  	register("Kubernetes70277", Kubernetes70277)
    15  }
    16  
    17  type WaitFunc_kubernetes70277 func(done <-chan struct{}) <-chan struct{}
    18  
    19  type ConditionFunc_kubernetes70277 func() (done bool, err error)
    20  
    21  func WaitFor_kubernetes70277(wait WaitFunc_kubernetes70277, fn ConditionFunc_kubernetes70277, done <-chan struct{}) error {
    22  	c := wait(done)
    23  	for {
    24  		_, open := <-c
    25  		ok, err := fn()
    26  		if err != nil {
    27  			return err
    28  		}
    29  		if ok {
    30  			return nil
    31  		}
    32  		if !open {
    33  			break
    34  		}
    35  	}
    36  	return nil
    37  }
    38  
    39  func poller_kubernetes70277(interval, timeout time.Duration) WaitFunc_kubernetes70277 {
    40  	return WaitFunc_kubernetes70277(func(done <-chan struct{}) <-chan struct{} {
    41  		ch := make(chan struct{})
    42  		go func() {
    43  			defer close(ch)
    44  
    45  			tick := time.NewTicker(interval)
    46  			defer tick.Stop()
    47  
    48  			var after <-chan time.Time
    49  			if timeout != 0 {
    50  				timer := time.NewTimer(timeout)
    51  				after = timer.C
    52  				defer timer.Stop()
    53  			}
    54  			for {
    55  				select {
    56  				case <-tick.C:
    57  					select {
    58  					case ch <- struct{}{}:
    59  					default:
    60  					}
    61  				case <-after:
    62  					return
    63  				case <-done:
    64  					return
    65  				}
    66  			}
    67  		}()
    68  
    69  		return ch
    70  	})
    71  }
    72  
    73  func Kubernetes70277() {
    74  	prof := pprof.Lookup("goroutineleak")
    75  	defer func() {
    76  		time.Sleep(100 * time.Millisecond)
    77  		prof.WriteTo(os.Stdout, 2)
    78  	}()
    79  	for i := 0; i < 1000; i++ {
    80  		go func() {
    81  			stopCh := make(chan struct{})
    82  			defer close(stopCh)
    83  			waitFunc := poller_kubernetes70277(time.Millisecond, 80*time.Millisecond)
    84  			var doneCh <-chan struct{}
    85  
    86  			WaitFor_kubernetes70277(func(done <-chan struct{}) <-chan struct{} {
    87  				doneCh = done
    88  				return waitFunc(done)
    89  			}, func() (bool, error) {
    90  				time.Sleep(10 * time.Millisecond)
    91  				return true, nil
    92  			}, stopCh)
    93  
    94  			<-doneCh // block here
    95  		}()
    96  	}
    97  }
    98  

View as plain text