Skip to content

Commit

Permalink
Fix flaky tests and race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Feb 2, 2024
1 parent da00f43 commit 03bcb1d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ wasm:
.PHONY: test/execute
test/execute: PKGS ?= "./..."
test/execute: RUN ?= .*
test/execute: RACE ?= false
test/execute: build test/prep-git-project
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -run="$(RUN)" -timeout=60s -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)
TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -run="$(RUN)" -timeout=60s -race=$(RACE) -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)

.PHONY: test/prep-git-project
test/prep-git-project:
Expand Down
12 changes: 10 additions & 2 deletions internal/runner/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"
"syscall"

"github.com/creack/pty"
Expand Down Expand Up @@ -39,7 +40,8 @@ type command struct {
PreEnv []string
PostEnv []string

cmd *exec.Cmd
cmd *exec.Cmd
cmdDone uint32

// pty and tty as pseud-terminal primary and secondary.
// Might be nil if not allocating a pseudo-terminal.
Expand Down Expand Up @@ -492,10 +494,16 @@ func (c *command) collectEnvs() {
c.Session.envStore = newEnvStore(c.cmd.Env...).Add(newOrUpdated...).Delete(deleted...)
}

func (c *command) ProcessFinished() bool {
return atomic.LoadUint32(&c.cmdDone) == 1
}

// ProcessWait waits only for the process to exit.
// You rather want to use Wait().
func (c *command) ProcessWait() error {
return errors.WithStack(c.cmd.Wait())
err := c.cmd.Wait()
atomic.StoreUint32(&c.cmdDone, 1)
return errors.WithStack(err)
}

// Finalize performs necessary actions and cleanups after the process exits.
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (r *runnerService) Execute(srv runnerv1.RunnerService_ExecuteServer) error
return
}
if err != nil && status.Convert(err).Code() == codes.Canceled {
if cmd.cmd.ProcessState != nil {
if cmd.ProcessFinished() {
logger.Info("stream canceled after the process finished; ignoring")
} else {
logger.Info("stream canceled while the process is still running; program will be stopped if non-background")
Expand Down
2 changes: 0 additions & 2 deletions internal/runner/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ func getExecuteResult(
}

func Test_runnerService(t *testing.T) {
t.Parallel()

lis, stop := testStartRunnerServiceServer(t)
t.Cleanup(stop)
_, client := testCreateRunnerServiceClient(t, lis)
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestRunmeRunAll(t *testing.T) {
}

func TestSkipPromptsWithinAPty(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
_ = os.Setenv("RUNME_VERBOSE", "false")
defer os.Unsetenv("RUNME_VERBOSE")
Expand Down

0 comments on commit 03bcb1d

Please sign in to comment.