Skip to content

Commit

Permalink
Fix iwftest pkg by exposing private interfaces in persistence and com…
Browse files Browse the repository at this point in the history
…munications (#39)
  • Loading branch information
longquanzheng authored Feb 5, 2023
1 parent 230bffa commit 0e39ed2
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 22 deletions.
6 changes: 5 additions & 1 deletion iwf/communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ type Communication interface {
PublishInterstateChannel(channelName string, value interface{})

// below is for internal implementation
getToPublishInterStateChannel() map[string][]iwfidl.EncodedObject
communicationInternal
}

type communicationInternal interface {
GetToPublishInterStateChannel() map[string][]iwfidl.EncodedObject
}
2 changes: 1 addition & 1 deletion iwf/communication_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type communicationImpl struct {
encoder ObjectEncoder
}

func (c *communicationImpl) getToPublishInterStateChannel() map[string][]iwfidl.EncodedObject {
func (c *communicationImpl) GetToPublishInterStateChannel() map[string][]iwfidl.EncodedObject {
return c.toPublishInterStateChannel
}

Expand Down
7 changes: 5 additions & 2 deletions iwf/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ type Persistence interface {
RecordEvent(key string, value interface{})

// below is for internal implementation
getToReturn() (
persistenceInternal
}

type persistenceInternal interface {
GetToReturn() (
dataObjectsToReturn []iwfidl.KeyValue,
stateLocalToReturn []iwfidl.KeyValue,
recordEvents []iwfidl.KeyValue,
searchAttributes []iwfidl.SearchAttribute,
)
}

2 changes: 1 addition & 1 deletion iwf/persistence_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (p *persistenceImpl) RecordEvent(key string, value interface{}) {
p.recordedEvents[key] = *v
}

func (p *persistenceImpl) getToReturn() (
func (p *persistenceImpl) GetToReturn() (
dataObjectsToReturn []iwfidl.KeyValue,
stateLocalToReturn []iwfidl.KeyValue,
recordEvents []iwfidl.KeyValue,
Expand Down
10 changes: 5 additions & 5 deletions iwf/worker_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (w *workerServiceImpl) HandleWorkflowStateStart(ctx context.Context, reques
return nil, err
}

err = canNotRequestAndPublishTheSameInterStateChannel(comm.getToPublishInterStateChannel(), commandRequest)
err = canNotRequestAndPublishTheSameInterStateChannel(comm.GetToPublishInterStateChannel(), commandRequest)
if err != nil {
return nil, err
}
Expand All @@ -40,14 +40,14 @@ func (w *workerServiceImpl) HandleWorkflowStateStart(ctx context.Context, reques
if err != nil {
return nil, err
}
publishings := toPublishing(comm.getToPublishInterStateChannel())
publishings := toPublishing(comm.GetToPublishInterStateChannel())
resp = &iwfidl.WorkflowStateStartResponse{
CommandRequest: idlCommandRequest,
}
if len(publishings) > 0 {
resp.PublishToInterStateChannel = publishings
}
dataObjectsToReturn, stateLocalToReturn, recordedEvents, upsertSearchAttributes := pers.getToReturn()
dataObjectsToReturn, stateLocalToReturn, recordedEvents, upsertSearchAttributes := pers.GetToReturn()
if len(dataObjectsToReturn) > 0 {
resp.UpsertDataObjects = dataObjectsToReturn
}
Expand Down Expand Up @@ -119,11 +119,11 @@ func (w *workerServiceImpl) HandleWorkflowStateDecide(ctx context.Context, reque
resp = &iwfidl.WorkflowStateDecideResponse{
StateDecision: idlDecision,
}
publishings := toPublishing(comm.getToPublishInterStateChannel())
publishings := toPublishing(comm.GetToPublishInterStateChannel())
if len(publishings) > 0 {
resp.PublishToInterStateChannel = publishings
}
dataObjectsToReturn, stateLocalToReturn, recordedEvents, upsertSearchAttributes := pers.getToReturn()
dataObjectsToReturn, stateLocalToReturn, recordedEvents, upsertSearchAttributes := pers.GetToReturn()
if len(dataObjectsToReturn) > 0 {
resp.UpsertDataObjects = dataObjectsToReturn
}
Expand Down
70 changes: 70 additions & 0 deletions iwftest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## Unit tests APIs for iWF

The APIs are generated by the below commands:
```shell
mockgen -source=iwf/persistence.go -package=iwftest -destination=iwftest/persistence.go
mockgen -source=iwf/communication.go -package=iwftest -destination=iwftest/communication.go
mockgen -source=iwf/workflow_context.go -package=iwftest -destination=iwftest/workflow_context.go
```

## Usage

See the [samples](https://github.com/indeedeng/iwf-golang-samples) for more details:

```go
package subscription

import (
"github.com/golang/mock/gomock"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"github.com/indeedeng/iwf-golang-sdk/iwftest"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

var mockWfCtx *iwftest.MockWorkflowContext
var mockPersistence *iwftest.MockPersistence
var mockCommunication *iwftest.MockCommunication
var emptyCmdResults = iwf.CommandResults{}
var emptyObj = iwftest.NewTestObject(nil)
var mockSvc *MockMyService

func beforeEach(t *testing.T) {
ctrl := gomock.NewController(t)

mockSvc = NewMockMyService(ctrl)
mockWfCtx = iwftest.NewMockWorkflowContext(ctrl)
mockPersistence = iwftest.NewMockPersistence(ctrl)
mockCommunication = iwftest.NewMockCommunication(ctrl)
}


func TestInitState_Start(t *testing.T) {
beforeEach(t)

state := NewInitState()

mockPersistence.EXPECT().SetDataObject(keyCustomer, testCustomer)
cmdReq, err := state.Start(mockWfCtx, testCustomerObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
}


func TestTrialState_Start(t *testing.T) {
beforeEach(t)

state := NewTrialState(mockSvc)

mockSvc.EXPECT().sendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
mockPersistence.EXPECT().GetDataObject(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
cmdReq, err := state.Start(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
firingTime := cmdReq.Commands[0].TimerCommand.FiringUnixTimestampSeconds
assert.Equal(t, iwf.AllCommandsCompletedRequest(
iwf.NewTimerCommand("", time.Unix(firingTime, 0)),
), cmdReq)
}

```
49 changes: 43 additions & 6 deletions iwftest/communication.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 46 additions & 6 deletions iwftest/persistence.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e39ed2

Please sign in to comment.