Skip to content

Commit ad31ae8

Browse files
committed
refactor: Introduce waitForProcessState helper in tests and refactor process state management to use stateMtx consistently and return state copies.
1 parent f4b2a51 commit ad31ae8

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/app/process.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const (
3737
type Process struct {
3838
sync.Mutex
3939
globalEnv []string
40-
confMtx sync.Mutex
4140
procConf *types.ProcessConfig
4241
procState *types.ProcessState
4342
stateMtx sync.Mutex
@@ -780,7 +779,8 @@ func (p *Process) getState() *types.ProcessState {
780779
p.updateProcState()
781780
p.stateMtx.Lock()
782781
defer p.stateMtx.Unlock()
783-
return p.procState
782+
stateCopy := *p.procState
783+
return &stateCopy
784784
}
785785

786786
type filterFn func(*types.ProcessState)
@@ -819,7 +819,7 @@ func (p *Process) GetPty() *os.File {
819819
func (p *Process) onStateChange(state string) {
820820
switch state {
821821
case types.ProcessStateSkipped:
822-
p.setExitCode(1)
822+
p.setExitCodeLocked(1)
823823
case types.ProcessStateRestarting:
824824
fallthrough
825825
case types.ProcessStateLaunching:
@@ -1018,20 +1018,25 @@ func (p *Process) logOpenPort(label string, port uint16) {
10181018
}
10191019

10201020
func (p *Process) getExitCode() int {
1021-
defer p.confMtx.Unlock()
1022-
p.confMtx.Lock()
1021+
p.stateMtx.Lock()
1022+
defer p.stateMtx.Unlock()
10231023
return p.procState.ExitCode
10241024
}
10251025

10261026
func (p *Process) setExitCode(code int) {
1027-
defer p.confMtx.Unlock()
1028-
p.confMtx.Lock()
1027+
p.stateMtx.Lock()
1028+
defer p.stateMtx.Unlock()
1029+
p.procState.ExitCode = code
1030+
}
1031+
1032+
// setExitCodeLocked sets the exit code when stateMtx is already held.
1033+
func (p *Process) setExitCodeLocked(code int) {
10291034
p.procState.ExitCode = code
10301035
}
10311036

10321037
func (p *Process) setProcHealth(health string) {
1033-
defer p.confMtx.Unlock()
1034-
p.confMtx.Lock()
1038+
p.stateMtx.Lock()
1039+
defer p.stateMtx.Unlock()
10351040
p.procState.Health = health
10361041
}
10371042

src/app/system_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ import (
2121
"github.com/f1bonacc1/process-compose/src/types"
2222
)
2323

24+
func waitForProcessState(t *testing.T, runner *ProjectRunner, name string, wantStatus string, timeout time.Duration) {
25+
t.Helper()
26+
deadline := time.Now().Add(timeout)
27+
for time.Now().Before(deadline) {
28+
state, err := runner.GetProcessState(name)
29+
if err != nil {
30+
t.Error(err.Error())
31+
return
32+
}
33+
if state.Status == wantStatus {
34+
return
35+
}
36+
time.Sleep(50 * time.Millisecond)
37+
}
38+
state, _ := runner.GetProcessState(name)
39+
t.Errorf("process %s: want %s, got %s (after %v)", name, wantStatus, state.Status, timeout)
40+
}
41+
2442
func getFixtures() []string {
2543
matches, err := filepath.Glob("../../fixtures/process-compose-*.yaml")
2644
if err != nil {
@@ -611,14 +629,8 @@ func TestSystem_TestProcShutDownNoRestart(t *testing.T) {
611629
return
612630
}
613631

614-
time.Sleep(100 * time.Millisecond)
615-
state, err = runner.GetProcessState(restarting)
616-
if err != nil {
617-
t.Error(err.Error())
618-
return
619-
}
620-
if state.Status != types.ProcessStateCompleted {
621-
t.Errorf("process %s want %s got %s", restarting, types.ProcessStateCompleted, state.Status)
632+
waitForProcessState(t, runner, restarting, types.ProcessStateCompleted, 5*time.Second)
633+
if t.Failed() {
622634
return
623635
}
624636
state, err = runner.GetProcessState(notRestarting)
@@ -636,16 +648,7 @@ func TestSystem_TestProcShutDownNoRestart(t *testing.T) {
636648
return
637649
}
638650

639-
time.Sleep(100 * time.Millisecond)
640-
state, err = runner.GetProcessState(notRestarting)
641-
if err != nil {
642-
t.Error(err.Error())
643-
return
644-
}
645-
if state.Status != types.ProcessStateCompleted {
646-
t.Errorf("process %s is running", notRestarting)
647-
return
648-
}
651+
waitForProcessState(t, runner, notRestarting, types.ProcessStateCompleted, 5*time.Second)
649652
}
650653
func TestSystem_TestReadyLine(t *testing.T) {
651654
proc1 := "proc1"

0 commit comments

Comments
 (0)