-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add awaiting on external event, update dependencies (#22)
- Loading branch information
Showing
10 changed files
with
230 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,31 @@ | ||
module github.com/tuannh982/simple-workflow-go | ||
|
||
go 1.22 | ||
go 1.23.0 | ||
|
||
toolchain go1.22.2 | ||
toolchain go1.23.6 | ||
|
||
require ( | ||
github.com/google/uuid v1.6.0 | ||
github.com/stretchr/testify v1.9.0 | ||
github.com/stretchr/testify v1.10.0 | ||
go.uber.org/zap v1.27.0 | ||
gorm.io/driver/postgres v1.5.9 | ||
gorm.io/driver/postgres v1.5.11 | ||
gorm.io/gorm v1.25.12 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jackc/pgx/v5 v5.5.5 // indirect | ||
github.com/jackc/puddle/v2 v2.2.1 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||
github.com/jackc/pgx/v5 v5.7.2 // indirect | ||
github.com/jackc/puddle/v2 v2.2.2 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/rogpeppe/go-internal v1.13.1 // indirect | ||
go.uber.org/multierr v1.10.0 // indirect | ||
golang.org/x/crypto v0.31.0 // indirect | ||
golang.org/x/sync v0.10.0 // indirect | ||
golang.org/x/text v0.21.0 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.org/x/crypto v0.33.0 // indirect | ||
golang.org/x/sync v0.11.0 // indirect | ||
golang.org/x/text v0.22.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package workflow | ||
|
||
import "github.com/tuannh982/simple-workflow-go/pkg/utils/promise" | ||
|
||
type EventPromise struct { | ||
WorkflowRuntime *WorkflowRuntime | ||
EventName string | ||
Promise *promise.Promise[[]byte] | ||
} | ||
|
||
func NewEventPromise(runtime *WorkflowRuntime, eventName string) *EventPromise { | ||
p := promise.NewPromise[[]byte]() | ||
return &EventPromise{ | ||
WorkflowRuntime: runtime, | ||
EventName: eventName, | ||
Promise: p, | ||
} | ||
} | ||
|
||
func (a *EventPromise) Await() ([]byte, error) { | ||
for { | ||
if a.Promise.IsDone() { | ||
err := a.Promise.Error() | ||
if a.Promise.Value() != nil { | ||
return *a.Promise.Value(), nil | ||
} else { | ||
return nil, err | ||
} | ||
} else { | ||
done, unExpectedErr := a.WorkflowRuntime.Step() | ||
if unExpectedErr != nil { | ||
panic(unExpectedErr) | ||
} | ||
if done { | ||
break | ||
} | ||
} | ||
} | ||
panic(ErrControlledPanic) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package await_external_event | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/tuannh982/simple-workflow-go/pkg/api/workflow" | ||
) | ||
|
||
type Void struct{} | ||
|
||
type String struct { | ||
Value string | ||
} | ||
|
||
func Activity1(ctx context.Context, input *String) (*String, error) { | ||
return &String{ | ||
Value: fmt.Sprintf("Hello, %s!", input.Value), | ||
}, nil | ||
} | ||
|
||
const HelloEventName = "hello" | ||
|
||
func AwaitExternalEventWorkflow(ctx context.Context, _ *Void) (*String, error) { | ||
message, err := workflow.AwaitEvent(ctx, HelloEventName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
activity1Result, err := workflow.CallActivity(ctx, Activity1, &String{Value: string(message)}).Await() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &String{Value: activity1Result.Value}, nil | ||
} |
71 changes: 71 additions & 0 deletions
71
test/e2e/psql/handle_external_event/handle_external_event_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package handle_external_event | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tuannh982/simple-workflow-go/pkg/api/client" | ||
"github.com/tuannh982/simple-workflow-go/pkg/api/worker" | ||
"github.com/tuannh982/simple-workflow-go/test/e2e/psql" | ||
"go.uber.org/zap" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func InitLogger() (*zap.Logger, error) { | ||
return zap.NewProduction() | ||
} | ||
|
||
func Test(t *testing.T) { | ||
ctx := context.Background() | ||
logger, err := InitLogger() | ||
assert.NoError(t, err) | ||
be, err := psql.InitBackend(logger) | ||
assert.NoError(t, err) | ||
aw, err := worker.NewActivityWorkersBuilder(). | ||
WithName("[e2e test] ActivityWorker"). | ||
WithBackend(be). | ||
WithLogger(logger). | ||
RegisterActivities( | ||
Activity1, | ||
). | ||
Build() | ||
assert.NoError(t, err) | ||
ww, err := worker.NewWorkflowWorkersBuilder(). | ||
WithName("[e2e test] WorkflowWorker"). | ||
WithBackend(be). | ||
WithLogger(logger). | ||
RegisterWorkflows( | ||
HandleExternalEventWorkflow, | ||
). | ||
Build() | ||
assert.NoError(t, err) | ||
aw.Start(ctx) | ||
defer aw.Stop(ctx) | ||
ww.Start(ctx) | ||
defer ww.Stop(ctx) | ||
// mock results | ||
message := "world" | ||
expectedResult := fmt.Sprintf("Hello, %s!", message) | ||
// | ||
uid, err := uuid.NewV6() | ||
assert.NoError(t, err) | ||
workflowID := fmt.Sprintf("e2e-external-event-workflow-%s", uid.String()) | ||
err = client.ScheduleWorkflow(ctx, be, HandleExternalEventWorkflow, &Void{}, client.WorkflowScheduleOptions{ | ||
WorkflowID: workflowID, | ||
Version: "1", | ||
}) | ||
assert.NoError(t, err) | ||
time.Sleep(1 * time.Second) | ||
err = client.SendWorkflowEvent(ctx, be, workflowID, HelloEventName, []byte(message)) | ||
assert.NoError(t, err) | ||
wResult, wErr, err := client.AwaitWorkflowResult(ctx, be, HandleExternalEventWorkflow, workflowID) | ||
assert.NotNil(t, wResult) | ||
assert.NoError(t, wErr) | ||
//goland:noinspection GoDfaErrorMayBeNotNil | ||
assert.Equal(t, expectedResult, wResult.Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters