Source file src/runtime/testdata/testgoroutineleakprofile/goker/kubernetes38669.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"
    10  	"runtime/pprof"
    11  	"sync"
    12  )
    13  
    14  func init() {
    15  	register("Kubernetes38669", Kubernetes38669)
    16  }
    17  
    18  type Event_kubernetes38669 int
    19  type watchCacheEvent_kubernetes38669 int
    20  
    21  type cacheWatcher_kubernetes38669 struct {
    22  	sync.Mutex
    23  	input   chan watchCacheEvent_kubernetes38669
    24  	result  chan Event_kubernetes38669
    25  	stopped bool
    26  }
    27  
    28  func (c *cacheWatcher_kubernetes38669) process(initEvents []watchCacheEvent_kubernetes38669) {
    29  	for _, event := range initEvents {
    30  		c.sendWatchCacheEvent(&event)
    31  	}
    32  	defer close(c.result)
    33  	defer c.Stop()
    34  	for {
    35  		_, ok := <-c.input
    36  		if !ok {
    37  			return
    38  		}
    39  	}
    40  }
    41  
    42  func (c *cacheWatcher_kubernetes38669) sendWatchCacheEvent(event *watchCacheEvent_kubernetes38669) {
    43  	c.result <- Event_kubernetes38669(*event)
    44  }
    45  
    46  func (c *cacheWatcher_kubernetes38669) Stop() {
    47  	c.stop()
    48  }
    49  
    50  func (c *cacheWatcher_kubernetes38669) stop() {
    51  	c.Lock()
    52  	defer c.Unlock()
    53  	if !c.stopped {
    54  		c.stopped = true
    55  		close(c.input)
    56  	}
    57  }
    58  
    59  func newCacheWatcher_kubernetes38669(chanSize int, initEvents []watchCacheEvent_kubernetes38669) *cacheWatcher_kubernetes38669 {
    60  	watcher := &cacheWatcher_kubernetes38669{
    61  		input:   make(chan watchCacheEvent_kubernetes38669, chanSize),
    62  		result:  make(chan Event_kubernetes38669, chanSize),
    63  		stopped: false,
    64  	}
    65  	go watcher.process(initEvents)
    66  	return watcher
    67  }
    68  
    69  func Kubernetes38669() {
    70  	prof := pprof.Lookup("goroutineleak")
    71  	defer func() {
    72  		// Yield several times to allow the child goroutine to run.
    73  		for i := 0; i < yieldCount; i++ {
    74  			runtime.Gosched()
    75  		}
    76  		prof.WriteTo(os.Stdout, 2)
    77  	}()
    78  	go func() {
    79  		initEvents := []watchCacheEvent_kubernetes38669{1, 2}
    80  		w := newCacheWatcher_kubernetes38669(0, initEvents)
    81  		w.Stop()
    82  	}()
    83  }
    84  

View as plain text