Merge pull request #125908 from aojea/race_pforward

fix race on integration test for portforward
This commit is contained in:
Kubernetes Prow Robot 2024-07-08 17:25:10 -07:00 committed by GitHub
commit cf33bef284
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -113,7 +113,15 @@ func TestPortforward(t *testing.T) {
// local port missing asks os to find random open port.
// Example: ":8000" (local = random, remote = 8000)
localRemotePort := fmt.Sprintf(":%s", remotePort)
streams, _, out, errOut := genericiooptions.NewTestIOStreams()
out := &mBuffer{
buffer: bytes.Buffer{},
}
errOut := &bytes.Buffer{}
streams := genericiooptions.IOStreams{
In: &bytes.Buffer{},
Out: out,
ErrOut: errOut,
}
portForwardOptions := portforward.NewDefaultPortForwardOptions(streams)
portForwardOptions.Namespace = "default"
portForwardOptions.PodName = "mypod"
@ -226,3 +234,20 @@ func (d *dummyPortForwarder) PortForward(ctx context.Context, name string, uid t
resp.Write(stream) //nolint:errcheck
return stream.Close()
}
type mBuffer struct {
mu sync.Mutex
buffer bytes.Buffer
}
func (s *mBuffer) Write(p []byte) (n int, err error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.buffer.Write(p)
}
func (s *mBuffer) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.buffer.String()
}