Skip to content

Commit

Permalink
fix #480 (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiantang committed Oct 19, 2023
1 parent 3c09ad7 commit 646949d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 0 additions & 8 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Engine struct {
watcherStopCh chan bool
buildRunCh chan bool
buildRunStopCh chan bool
canExit chan bool
binStopCh chan bool
exitCh chan bool

Expand Down Expand Up @@ -56,7 +55,6 @@ func NewEngineWithConfig(cfg *Config, debugMode bool) (*Engine, error) {
watcherStopCh: make(chan bool, 10),
buildRunCh: make(chan bool, 1),
buildRunStopCh: make(chan bool, 1),
canExit: make(chan bool, 1),
binStopCh: make(chan bool),
exitCh: make(chan bool),
fileChecksums: &checksumMap{m: make(map[string]string)},
Expand Down Expand Up @@ -374,19 +372,16 @@ func (e *Engine) buildRun() {
select {
case <-e.buildRunStopCh:
return
case <-e.canExit:
default:
}
var err error
if err = e.runPreCmd(); err != nil {
e.canExit <- true
e.runnerLog("failed to execute pre_cmd: %s", err.Error())
if e.config.Build.StopOnError {
return
}
}
if err = e.building(); err != nil {
e.canExit <- true
e.buildLog("failed to build, error: %s", err.Error())
_ = e.writeBuildErrorLog(err.Error())
if e.config.Build.StopOnError {
Expand All @@ -399,7 +394,6 @@ func (e *Engine) buildRun() {
return
case <-e.exitCh:
e.mainDebug("exit in buildRun")
close(e.canExit)
return
default:
}
Expand Down Expand Up @@ -521,7 +515,6 @@ func (e *Engine) runBin() error {
case <-e.exitCh:
e.mainDebug("exit in runBin")
wg.Wait()
close(e.canExit)
default:
}
}()
Expand Down Expand Up @@ -595,7 +588,6 @@ func (e *Engine) cleanup() {

e.mainDebug("waiting for exit...")

<-e.canExit
e.running = false
e.mainDebug("exited")
}
Expand Down
36 changes: 36 additions & 0 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func TestRebuild(t *testing.T) {
engine.Stop()
t.Logf("engine stopped")
wg.Wait()
time.Sleep(time.Second * 1)
assert.True(t, checkPortConnectionRefused(port))
}

Expand Down Expand Up @@ -449,6 +450,41 @@ func TestCtrlCWhenREngineIsRunning(t *testing.T) {
assert.False(t, engine.running)
}

func TestCtrlCWithFailedBin(t *testing.T) {
timeout := 5 * time.Second
done := make(chan struct{})
go func() {
dir := initWithQuickExitGoCode(t)
chdir(t, dir)
engine, err := NewEngine("", true)
assert.NoError(t, err)
engine.config.Build.Bin = "<WRONGCOMAMND>"
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
engine.Run()
t.Logf("engine stopped")
wg.Done()
}()
go func() {
<-sigs
engine.Stop()
t.Logf("engine stopped")
}()
time.Sleep(time.Second * 1)
sigs <- syscall.SIGINT
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(timeout):
t.Error("Test timed out")
}
}

func TestFixCloseOfChannelAfterCtrlC(t *testing.T) {
// fix https://github.com/cosmtrek/air/issues/294
dir := initWithBuildFailedCode(t)
Expand Down

0 comments on commit 646949d

Please sign in to comment.