@@ -2,26 +2,117 @@ package workflow
22
33import (
44 "context"
5+ "errors"
56 "testing"
67
78 "github.com/stretchr/testify/require"
89)
910
10- func TestNoopRunStateController (t * testing.T ) {
11- ctrl := testingRunStateController {}
11+ func TestTestingRunStateController (t * testing.T ) {
12+ ctx := context . Background ()
1213
13- ctx := t .Context ()
14- err := ctrl .Pause (ctx , "" )
15- require .NoError (t , err )
14+ t .Run ("methods return nil error when no functions set" , func (t * testing.T ) {
15+ ctrl := testingRunStateController {}
1616
17- err = ctrl .Resume (ctx )
18- require .NoError (t , err )
17+ err : = ctrl .Pause (ctx , "test reason" )
18+ require .NoError (t , err )
1919
20- err = ctrl .Cancel (ctx , "" )
21- require .NoError (t , err )
20+ err = ctrl .Resume (ctx )
21+ require .NoError (t , err )
2222
23- err = ctrl .DeleteData (ctx , "" )
24- require .NoError (t , err )
23+ err = ctrl .Cancel (ctx , "test reason" )
24+ require .NoError (t , err )
25+
26+ err = ctrl .DeleteData (ctx , "test reason" )
27+ require .NoError (t , err )
28+
29+ err = ctrl .SaveAndRepeat (ctx )
30+ require .NoError (t , err )
31+ })
32+
33+ t .Run ("methods call provided functions" , func (t * testing.T ) {
34+ var pauseCalled , resumeCalled , cancelCalled , deleteDataCalled , saveAndRepeatCalled bool
35+
36+ ctrl := testingRunStateController {
37+ pause : func (ctx context.Context ) error {
38+ pauseCalled = true
39+ return nil
40+ },
41+ resume : func (ctx context.Context ) error {
42+ resumeCalled = true
43+ return nil
44+ },
45+ cancel : func (ctx context.Context ) error {
46+ cancelCalled = true
47+ return nil
48+ },
49+ deleteData : func (ctx context.Context ) error {
50+ deleteDataCalled = true
51+ return nil
52+ },
53+ saveAndRepeat : func (ctx context.Context ) error {
54+ saveAndRepeatCalled = true
55+ return nil
56+ },
57+ }
58+
59+ err := ctrl .Pause (ctx , "test reason" )
60+ require .NoError (t , err )
61+ require .True (t , pauseCalled )
62+
63+ err = ctrl .Resume (ctx )
64+ require .NoError (t , err )
65+ require .True (t , resumeCalled )
66+
67+ err = ctrl .Cancel (ctx , "test reason" )
68+ require .NoError (t , err )
69+ require .True (t , cancelCalled )
70+
71+ err = ctrl .DeleteData (ctx , "test reason" )
72+ require .NoError (t , err )
73+ require .True (t , deleteDataCalled )
74+
75+ err = ctrl .SaveAndRepeat (ctx )
76+ require .NoError (t , err )
77+ require .True (t , saveAndRepeatCalled )
78+ })
79+
80+ t .Run ("methods propagate errors" , func (t * testing.T ) {
81+ testErr := errors .New ("test error" )
82+
83+ ctrl := testingRunStateController {
84+ pause : func (ctx context.Context ) error {
85+ return testErr
86+ },
87+ resume : func (ctx context.Context ) error {
88+ return testErr
89+ },
90+ cancel : func (ctx context.Context ) error {
91+ return testErr
92+ },
93+ deleteData : func (ctx context.Context ) error {
94+ return testErr
95+ },
96+ saveAndRepeat : func (ctx context.Context ) error {
97+ return testErr
98+ },
99+ }
100+
101+ err := ctrl .Pause (ctx , "test reason" )
102+ require .Equal (t , testErr , err )
103+
104+ err = ctrl .Resume (ctx )
105+ require .Equal (t , testErr , err )
106+
107+ err = ctrl .Cancel (ctx , "test reason" )
108+ require .Equal (t , testErr , err )
109+
110+ err = ctrl .DeleteData (ctx , "test reason" )
111+ require .Equal (t , testErr , err )
112+
113+ err = ctrl .SaveAndRepeat (ctx )
114+ require .Equal (t , testErr , err )
115+ })
25116}
26117
27118func TestRunStateControllerTransitions (t * testing.T ) {
0 commit comments