From 179ba4c7b21bc5924aecab129b58ff1ac662f98e Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Fri, 10 Feb 2023 13:38:52 -0500 Subject: [PATCH] cmd: fix test flakyiness (#6040) Signed-off-by: Nick Santos --- internal/controllers/core/cmd/execer.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/controllers/core/cmd/execer.go b/internal/controllers/core/cmd/execer.go index 91591c652c..4c396c7f8f 100644 --- a/internal/controllers/core/cmd/execer.go +++ b/internal/controllers/core/cmd/execer.go @@ -27,6 +27,7 @@ type Execer interface { } type fakeExecProcess struct { + closeCh chan bool exitCh chan int workdir string env []string @@ -47,17 +48,23 @@ func NewFakeExecer() *FakeExecer { func (e *FakeExecer) Start(ctx context.Context, cmd model.Cmd, w io.Writer) chan statusAndMetadata { e.mu.Lock() - _, ok := e.processes[cmd.String()] + oldProcess, ok := e.processes[cmd.String()] e.mu.Unlock() if ok { - logger.Get(ctx).Infof("internal error: fake execer only supports one instance of each unique command at a time. tried to start a second instance of %q", cmd.Argv) - return nil + select { + case <-oldProcess.closeCh: + case <-time.After(5 * time.Second): + logger.Get(ctx).Infof("internal error: fake execer only supports one instance of each unique command at a time. tried to start a second instance of %q", cmd.Argv) + return nil + } } exitCh := make(chan int) + closeCh := make(chan bool) e.mu.Lock() e.processes[cmd.String()] = &fakeExecProcess{ + closeCh: closeCh, exitCh: exitCh, workdir: cmd.Dir, startTime: time.Now(), @@ -70,6 +77,7 @@ func (e *FakeExecer) Start(ctx context.Context, cmd model.Cmd, w io.Writer) chan fakeRun(ctx, cmd, w, statusCh, exitCh) e.mu.Lock() + close(closeCh) delete(e.processes, cmd.String()) e.mu.Unlock() }()