Skip to content

Commit

Permalink
[UNIT TEST] Update mockgen and create client mock (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
afasari committed Nov 10, 2023
1 parent 02b964c commit c17ff97
Show file tree
Hide file tree
Showing 18 changed files with 770 additions and 98 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# iWF Golang SDK

[![Go Reference](https://pkg.go.dev/badge/github.com/indeedeng/iwf-golang-sdk.svg)](https://pkg.go.dev/github.com/indeedeng/iwf-golang-sdk)
[![Go Report Card](https://goreportcard.com/badge/github.com/indeedeng/iwf-golang-sdk)](https://goreportcard.com/report/github.com/indeedeng/iwf-golang-sdk)
[![Coverage Status](https://codecov.io/github/indeedeng/iwf-golang-sdk/coverage.svg?branch=main)](https://app.codecov.io/gh/indeedeng/iwf-golang-sdk/branch/main)

[![Build status](https://github.com/indeedeng/iwf-golang-sdk/actions/workflows/ci-integ-test.yml/badge.svg?branch=main)](https://github.com/indeedeng/iwf-golang-sdk/actions/workflows/ci-integ-test.yml)



Golang SDK for [iWF workflow engine](https://github.com/indeedeng/iwf)

See [samples](https://github.com/indeedeng/iwf-golang-samples) for how to use this SDK.
# Contribution

## Contribution

See [contribution guide](CONTRIBUTION.md)

# Development Plan
## Development Plan

## 1.0
### 1.0

- [x] Start workflow API
- [x] Executing `start`/`decide` APIs and completing workflow
Expand All @@ -37,20 +38,22 @@ See [contribution guide](CONTRIBUTION.md)
- [x] Reset workflow by stateId/StateExecutionId
- [x] More workflow start options: initial search attributes

## 1.1
### 1.1

- [x] Skip timer API for testing/operation
- [x] Decider trigger type: any command combination

## 1.2
### 1.2

- [x] API improvements to reduce boilerplate code

## 1.3
### 1.3

- [x] Support failing workflow with results
- [x] Improve workflow uncompleted error return(canceled, failed, timeout, terminated)

### 1.4

- [x] Renaming some concepts/APIs with breaking changes(see releaste notes)
- [x] Renaming some concepts/APIs with breaking changes(see release notes)
- [x] Support workflow RPC
- [x] PARTIAL_WITH_EXCLUSIVE_LOCK persistence loading type
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ go 1.18

require (
github.com/gin-gonic/gin v1.8.2
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.8.1
golang.org/x/oauth2 v0.3.0
go.uber.org/mock v0.3.0
)

require (
Expand All @@ -16,7 +15,7 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
Expand All @@ -29,7 +28,6 @@ require (
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
35 changes: 3 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJ
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down Expand Up @@ -64,53 +60,28 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/oauth2 v0.3.0 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions integ/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ func TestProceedOnStateStartFailWorkflow(t *testing.T) {
var output string
err = client.GetSimpleWorkflowResult(context.Background(), wfId, "", &output)
assert.Equal(t, "input_state1_start_state1_decide_state2_start_state2_decide", output)
assert.Nil(t, err)
}
10 changes: 4 additions & 6 deletions integ/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package integ

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"log"
"net/http"
"os"
"testing"

"github.com/gin-gonic/gin"
"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
"github.com/indeedeng/iwf-golang-sdk/iwf"
)

func TestMain(m *testing.M) {
Expand All @@ -33,7 +34,6 @@ func apiV1WorkflowStateStart(c *gin.Context) {
return
}
c.JSON(http.StatusOK, resp)
return
}
func apiV1WorkflowStateDecide(c *gin.Context) {
var req iwfidl.WorkflowStateExecuteRequest
Expand All @@ -48,7 +48,6 @@ func apiV1WorkflowStateDecide(c *gin.Context) {
return
}
c.JSON(http.StatusOK, resp)
return
}

func apiV1WorkflowWorkerRpc(c *gin.Context) {
Expand All @@ -67,7 +66,6 @@ func apiV1WorkflowWorkerRpc(c *gin.Context) {
return
}
c.JSON(http.StatusOK, resp)
return
}

func startWorkflowWorker() (closeFunc func()) {
Expand Down
2 changes: 1 addition & 1 deletion integ/persistence_workflow_state2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (b persistenceWorkflowState2) WaitUntil(ctx iwf.WorkflowContext, input iwf.
dv := persistence.GetSearchAttributeDatetime(testSearchAttributeDatetime)
bv := persistence.GetSearchAttributeBool(testSearchAttributeBool)
persistence.SetSearchAttributeDouble(testSearchAttributeDouble, 1.0)
if dv.Unix() == do.Datetime.Unix() && bv == true {
if dv.Unix() == do.Datetime.Unix() && bv {
persistence.SetSearchAttributeText(testSearchAttributeText, testText)
return iwf.EmptyCommandRequest(), nil
}
Expand Down
13 changes: 11 additions & 2 deletions integ/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package integ

import (
"context"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"

"github.com/indeedeng/iwf-golang-sdk/iwf"
"github.com/stretchr/testify/assert"
)

func TestSignalWorkflow(t *testing.T) {
Expand All @@ -15,11 +16,15 @@ func TestSignalWorkflow(t *testing.T) {
assert.Nil(t, err)
assert.NotEmpty(t, runId)
err = client.SignalWorkflow(context.Background(), &signalWorkflow{}, wfId, "", testChannelName2, 10)
assert.Nil(t, err)

// wait for timer to be ready to be skipped
time.Sleep(time.Second)
err = client.SignalWorkflow(context.Background(), &signalWorkflow{}, wfId, "", testChannelName1, 100)
assert.Nil(t, err)

err = client.SkipTimerByCommandIndex(context.Background(), wfId, "", signalWorkflowState2{}, 1, 0)
assert.Nil(t, err)

var output int
err = client.GetSimpleWorkflowResult(context.Background(), wfId, "", &output)
Expand All @@ -39,11 +44,15 @@ func TestSignalWorkflowWithUntypedClient(t *testing.T) {
assert.Nil(t, err)
assert.NotEmpty(t, runId)
err = unregisteredClient.SignalWorkflow(context.Background(), wfId, "", testChannelName2, 10)
assert.Nil(t, err)

// wait for timer to be ready to be skipped
time.Sleep(time.Second)
err = unregisteredClient.SignalWorkflow(context.Background(), wfId, "", testChannelName1, 100)
assert.Nil(t, err)

err = unregisteredClient.SkipTimerByCommandIndex(context.Background(), wfId, "", iwf.GetFinalWorkflowStateId(signalWorkflowState2{}), 1, 0)
assert.Nil(t, err)

var output int
err = unregisteredClient.GetSimpleWorkflowResult(context.Background(), wfId, "", &output)
Expand Down
3 changes: 3 additions & 0 deletions iwf/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package iwf

//go:generate mockgen -source=./client.go -package=iwftest -destination=../iwftest/client.go

import (
"context"

"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
)

Expand Down
2 changes: 2 additions & 0 deletions iwf/communication.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package iwf

//go:generate mockgen -source=./communication.go -package=iwftest -destination=../iwftest/communication.go

import "github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"

type Communication interface {
Expand Down
5 changes: 4 additions & 1 deletion iwf/persistence.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package iwf

//go:generate mockgen -source=./persistence.go -package=iwftest -destination=../iwftest/persistence.go

import (
"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
"time"

"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
)

// Persistence APIs are for read/write persistence of workflow
Expand Down
7 changes: 7 additions & 0 deletions iwf/worker_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iwf

import (
"context"

"github.com/indeedeng/iwf-golang-sdk/gen/iwfidl"
)

Expand Down Expand Up @@ -33,6 +34,9 @@ func (w *workerServiceImpl) HandleWorkflowWorkerRPC(ctx context.Context, request
}

outputEncoded, err := w.options.ObjectEncoder.Encode(output)
if err != nil {
return nil, err
}
resp = &iwfidl.WorkflowWorkerRpcResponse{
Output: outputEncoded,
}
Expand Down Expand Up @@ -178,6 +182,9 @@ func (w *workerServiceImpl) HandleWorkflowStateExecute(ctx context.Context, requ
return nil, err
}
idlDecision, err := toIdlDecision(decision, wfType, w.registry, w.options.ObjectEncoder)
if err != nil {
return nil, err
}
resp = &iwfidl.WorkflowStateExecuteResponse{
StateDecision: idlDecision,
}
Expand Down
2 changes: 2 additions & 0 deletions iwf/workflow_context.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package iwf

//go:generate mockgen -source=./workflow_context.go -package=iwftest -destination=../iwftest/workflow_context.go

import "context"

type WorkflowContext interface {
Expand Down
13 changes: 10 additions & 3 deletions iwftest/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
## Unit tests APIs for iWF
# Unit tests APIs for iWF

The APIs are generated using [mockgen](https://github.com/uber-go/mock) by the below commands:

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
mockgen -source=iwf/client.go -package=iwftest -destination=iwftest/client.go
```

or running this on sdk root folder

```shell
go generate ./...
```

## Usage

See the [example](./example) for more details.

0 comments on commit c17ff97

Please sign in to comment.