Skip to content

Commit 6728fcf

Browse files
authored
Add DelayInState() receiver function to pause in a state then continu… (#140)
* Add DelayInState() receiver function to pause in a state then continue the workflow. Signed-off-by: Anthony Floeder <anthony.floeder@hpe.com> * Allow multiple states to delay Signed-off-by: Anthony Floeder <anthony.floeder@hpe.com> --------- Signed-off-by: Anthony Floeder <anthony.floeder@hpe.com>
1 parent 8ef546b commit 6728fcf

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

int_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ var tests = []*T{
5656
// Mark a test case as Pending(). Ginkgo will not run any tests that have the Pending decorator
5757
// MakeTest("Pending", "#DW ...").Pending()
5858
//
59-
// Mark a test case so it will stop after the workflow achieves the desired state of PreRun
59+
// Mark a test case, so it will stop after the workflow achieves the desired state of PreRun
6060
// MakeTest("Stop After", "#DW ...").StopAfter(dwsv1alpha7.StatePreRun),
6161
//
62+
// Mark a test case, so it delays for the specified period after a workflow achieves the desired state
63+
// Multiple delays can be added by calling DelayInState multiple times.
64+
// MakeTest("Delay In State", "#DW ...").DelayInState(dwsv1alpha7.StateDataIn, 2*time.Minute).DelayInState(dwsv1alpha7.StateDataOut, 2*time.Minute),
65+
//
6266
// Duplicate a test case 20 times.
6367
// DuplicateTest(
6468
// MakeTest("XFS", "#DW jobdw type=xfs name=xfs capacity=50GB"),

internal/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
// execution. Nil values represent no configuration of that type.
4747
type TOptions struct {
4848
stopAfter *TStopAfter
49+
delayInState []TDelayInState
4950
expectError *TExpectError
5051
storageProfile *TStorageProfile
5152
containerProfile *TContainerProfile
@@ -76,6 +77,18 @@ func (t *T) StopAfter(state dwsv1alpha7.WorkflowState) *T {
7677
return t
7778
}
7879

80+
type TDelayInState struct {
81+
state dwsv1alpha7.WorkflowState
82+
duration time.Duration
83+
}
84+
85+
// DelayInState allows you to pause in a specific state for a given duration.
86+
// Multiple delays can be added by calling this method multiple times.
87+
func (t *T) DelayInState(state dwsv1alpha7.WorkflowState, duration time.Duration) *T {
88+
t.options.delayInState = append(t.options.delayInState, TDelayInState{state: state, duration: duration})
89+
return t
90+
}
91+
7992
type TExpectError struct {
8093
state dwsv1alpha7.WorkflowState
8194
}

internal/states.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,21 @@ func (t *T) Execute(ctx context.Context, k8sClient client.Client) {
4747
for _, fn := range []StateHandler{t.proposal, t.setup, t.dataIn, t.preRun, t.postRun, t.dataOut, t.teardown} {
4848
fn(ctx, k8sClient, t.workflow)
4949

50+
// Extract the current state name from the function
51+
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() // This will return something like `full-import-path.(*T).Function-fm`
52+
fnName = fnName[strings.Index(fnName, "(*T).")+5 : len(fnName)-3] // Extract the function name
53+
state := dwsv1alpha7.WorkflowState(strings.Title(fnName))
54+
55+
// Handle DelayInState - check all delays for this state
56+
for _, delay := range t.options.delayInState {
57+
if state == delay.state {
58+
By(fmt.Sprintf("Delaying in state %s for %v", state, delay.duration))
59+
time.Sleep(delay.duration)
60+
}
61+
}
62+
63+
// Handle StopAfter
5064
if t.options.stopAfter != nil {
51-
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() // This will return something like `full-import-path.(*T).Function-fm`
52-
fnName = fnName[strings.Index(fnName, "(*T).")+5 : len(fnName)-3] // Extract the function name
53-
state := dwsv1alpha7.WorkflowState(strings.Title(fnName))
5465
if state == t.options.stopAfter.state {
5566
break
5667
}

0 commit comments

Comments
 (0)