@@ -26,8 +26,9 @@ import (
2626 "encoding/json"
2727 "errors"
2828 "fmt"
29- "io/ioutil "
29+ "io"
3030 "math"
31+ "os"
3132
3233 "github.com/golang/mock/gomock"
3334 "github.com/opentracing/opentracing-go"
@@ -120,6 +121,16 @@ func (r *WorkflowReplayer) RegisterWorkflowWithOptions(w interface{}, options Re
120121 r .registry .RegisterWorkflowWithOptions (w , options )
121122}
122123
124+ // RegisterActivity registers an activity function for this replayer
125+ func (r * WorkflowReplayer ) RegisterActivity (a interface {}) {
126+ r .registry .RegisterActivity (a )
127+ }
128+
129+ // RegisterActivityWithOptions registers an activity function for this replayer with custom options, e.g. an explicit name.
130+ func (r * WorkflowReplayer ) RegisterActivityWithOptions (a interface {}, options RegisterActivityOptions ) {
131+ r .registry .RegisterActivityWithOptions (a , options )
132+ }
133+
123134// ReplayWorkflowHistory executes a single decision task for the given history.
124135// Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
125136// The logger is an optional parameter. Defaults to the noop logger.
@@ -135,19 +146,12 @@ func (r *WorkflowReplayer) ReplayWorkflowHistory(logger *zap.Logger, history *sh
135146 return r .replayWorkflowHistory (logger , service , replayDomainName , nil , history , nil )
136147}
137148
138- // ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
139- // Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
140- // The logger is an optional parameter. Defaults to the noop logger.
141- func (r * WorkflowReplayer ) ReplayWorkflowHistoryFromJSONFile (logger * zap.Logger , jsonfileName string ) error {
142- return r .ReplayPartialWorkflowHistoryFromJSONFile (logger , jsonfileName , 0 )
149+ func (r * WorkflowReplayer ) ReplayWorkflowHistoryFromJSON (logger * zap.Logger , reader io.Reader ) error {
150+ return r .ReplayPartialWorkflowHistoryFromJSON (logger , reader , 0 )
143151}
144152
145- // ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file up to provided
146- // lastEventID(inclusive).
147- // Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
148- // The logger is an optional parameter. Defaults to the noop logger.
149- func (r * WorkflowReplayer ) ReplayPartialWorkflowHistoryFromJSONFile (logger * zap.Logger , jsonfileName string , lastEventID int64 ) error {
150- history , err := extractHistoryFromFile (jsonfileName , lastEventID )
153+ func (r * WorkflowReplayer ) ReplayPartialWorkflowHistoryFromJSON (logger * zap.Logger , reader io.Reader , lastEventID int64 ) error {
154+ history , err := extractHistoryFromReader (reader , lastEventID )
151155
152156 if err != nil {
153157 return err
@@ -164,6 +168,28 @@ func (r *WorkflowReplayer) ReplayPartialWorkflowHistoryFromJSONFile(logger *zap.
164168 return r .replayWorkflowHistory (logger , service , replayDomainName , nil , history , nil )
165169}
166170
171+ // ReplayWorkflowHistoryFromJSONFile executes a single decision task for the given json history file.
172+ // Use for testing the backwards compatibility of code changes and troubleshooting workflows in a debugger.
173+ // The logger is an optional parameter. Defaults to the noop logger.
174+ func (r * WorkflowReplayer ) ReplayWorkflowHistoryFromJSONFile (logger * zap.Logger , jsonfileName string ) error {
175+ return r .ReplayPartialWorkflowHistoryFromJSONFile (logger , jsonfileName , 0 )
176+ }
177+
178+ // ReplayPartialWorkflowHistoryFromJSONFile executes a single decision task for the given json history file up to provided
179+ // lastEventID(inclusive).
180+ // Use for testing backwards compatibility of code changes and troubleshooting workflows in a debugger.
181+ // The logger is an optional parameter. Defaults to the noop logger.
182+ func (r * WorkflowReplayer ) ReplayPartialWorkflowHistoryFromJSONFile (logger * zap.Logger , jsonfileName string , lastEventID int64 ) error {
183+ file , err := os .Open (jsonfileName )
184+ if err != nil {
185+ return fmt .Errorf ("could not open file: %w" , err )
186+ }
187+ defer func () {
188+ _ = file .Close ()
189+ }()
190+ return r .ReplayPartialWorkflowHistoryFromJSON (logger , file , lastEventID )
191+ }
192+
167193// ReplayWorkflowExecution replays workflow execution loading it from Cadence service.
168194// The logger is an optional parameter. Defaults to the noop logger.
169195func (r * WorkflowReplayer ) ReplayWorkflowExecution (
@@ -338,17 +364,17 @@ func (r *WorkflowReplayer) replayWorkflowHistory(
338364 return fmt .Errorf ("replay workflow doesn't return the same result as the last event, resp: %v, last: %v" , resp , last )
339365}
340366
341- func extractHistoryFromFile ( jsonfileName string , lastEventID int64 ) (* shared.History , error ) {
342- raw , err := ioutil . ReadFile ( jsonfileName )
367+ func extractHistoryFromReader ( r io. Reader , lastEventID int64 ) (* shared.History , error ) {
368+ raw , err := io . ReadAll ( r )
343369 if err != nil {
344- return nil , err
370+ return nil , fmt . Errorf ( "failed to read data: %w" , err )
345371 }
346372
347373 var deserializedEvents []* shared.HistoryEvent
348374 err = json .Unmarshal (raw , & deserializedEvents )
349375
350376 if err != nil {
351- return nil , err
377+ return nil , fmt . Errorf ( "invalid json contents: %w" , err )
352378 }
353379
354380 if lastEventID <= 0 {
0 commit comments