Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug final step #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions debug_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (r *DebugRunner) Run(state StateBag) {
pauseFn = DebugPauseDefault
}

// Rebuild the steps so that we insert the pause step after each
steps := make([]Step, len(r.Steps)*2)
// Wrap steps to call PauseFn after each run and before each cleanup
steps := make([]Step, len(r.Steps))
for i, step := range r.Steps {
steps[i*2] = step
steps[(i*2)+1] = &debugStepPause{
steps[i] = &debugStepPause{
reflect.Indirect(reflect.ValueOf(step)).Type().Name(),
step,
pauseFn,
}
}
Expand Down Expand Up @@ -97,14 +97,17 @@ func DebugPauseDefault(loc DebugLocation, name string, state StateBag) {

type debugStepPause struct {
StepName string
Step Step
PauseFn DebugPauseFn
}

func (s *debugStepPause) Run(state StateBag) StepAction {
action := s.Step.Run(state)
s.PauseFn(DebugLocationAfterRun, s.StepName, state)
return ActionContinue
return action
}

func (s *debugStepPause) Cleanup(state StateBag) {
s.PauseFn(DebugLocationBeforeCleanup, s.StepName, state)
s.Step.Cleanup(state)
}
39 changes: 38 additions & 1 deletion debug_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,48 @@ func TestDebugPauseDefault(t *testing.T) {
case <-time.After(100 * time.Millisecond):
}

w.Write([]byte("\n\n"))
_, _ = w.Write([]byte("\n\n")) // ignore error

select {
case <-complete:
case <-time.After(100 * time.Millisecond):
t.Fatal("didn't complete")
}
}

// confirm that a halting step is debuggable before cleanup
func TestDebugRunner_Halt(t *testing.T) {
data := new(BasicStateBag)
stepA := &TestStepAcc{Data: "a"}
stepB := &TestStepAcc{Data: "b", Halt: true}
stepC := &TestStepAcc{Data: "c"}

key := "data"
pauseFn := func(loc DebugLocation, name string, state StateBag) {
direction := "Run"
if loc == DebugLocationBeforeCleanup {
direction = "Cleanup"
}

if _, ok := state.GetOk(key); !ok {
state.Put(key, []string{})
}

data := state.Get(key).([]string)
state.Put(key, append(data, direction))
}

r := &DebugRunner{
Steps: []Step{stepA, stepB, stepC},
PauseFn: pauseFn,
}

r.Run(data)

// Test data
expected := []string{"a", "Run", "b", "Run", "Cleanup", "Cleanup"}
results := data.Get("data").([]string)
if !reflect.DeepEqual(results, expected) {
t.Errorf("unexpected results: %#v", results)
}
}
4 changes: 2 additions & 2 deletions multistep.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// multistep is a library for bulding up complex actions using individual,
// discrete steps.
// Package multistep is a library for bulding up complex actions using
// individual, discrete steps.
package multistep

// A StepAction determines the next step to take regarding multi-step actions.
Expand Down
2 changes: 1 addition & 1 deletion multistep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s TestStepSync) Cleanup(StateBag) {}

func (s TestStepWaitForever) Run(StateBag) StepAction {
select {}
return ActionContinue
// unreachable
}

func (s TestStepWaitForever) Cleanup(StateBag) {}
Expand Down