From c0be89d024f4716f2ede530e772d88614c332d8b Mon Sep 17 00:00:00 2001 From: Jacob LeGrone Date: Wed, 4 Aug 2021 15:28:02 -0400 Subject: [PATCH] use shared helloworld worker implementation --- internal/examples/helloworld/helloworld.go | 33 ++++++++++++++++++++++ server/server_test.go | 22 ++------------- server/temporaltest/server_test.go | 24 +++------------- 3 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 internal/examples/helloworld/helloworld.go diff --git a/internal/examples/helloworld/helloworld.go b/internal/examples/helloworld/helloworld.go new file mode 100644 index 00000000..7f5aff97 --- /dev/null +++ b/internal/examples/helloworld/helloworld.go @@ -0,0 +1,33 @@ +package helloworld + +import ( + "context" + "fmt" + "time" + + "go.temporal.io/sdk/worker" + "go.temporal.io/sdk/workflow" +) + +// Greet implements a Temporal workflow that returns a salutation for a given subject. +func Greet(ctx workflow.Context, subject string) (string, error) { + var greeting string + if err := workflow.ExecuteActivity( + workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ScheduleToCloseTimeout: time.Second}), + PickGreeting, + ).Get(ctx, &greeting); err != nil { + return "", err + } + + return fmt.Sprintf("%s %s", greeting, subject), nil +} + +// PickGreeting is a Temporal activity that returns some greeting text. +func PickGreeting(ctx context.Context) (string, error) { + return "Hello", nil +} + +func RegisterWorkflowsAndActivities(r worker.Registry) { + r.RegisterWorkflow(Greet) + r.RegisterActivity(PickGreeting) +} diff --git a/server/server_test.go b/server/server_test.go index 1bfacd59..fa90df33 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2,16 +2,15 @@ package server_test import ( "context" - "fmt" "os" "testing" "time" + "github.com/DataDog/temporalite/internal/examples/helloworld" "github.com/DataDog/temporalite/server" "github.com/DataDog/temporalite/server/temporaltest" "go.temporal.io/sdk/client" "go.temporal.io/sdk/worker" - "go.temporal.io/sdk/workflow" ) var ts *server.Server @@ -26,20 +25,6 @@ func TestMain(m *testing.M) { os.Exit(code) } -func Greet(ctx workflow.Context, subject string) (string, error) { - var greeting string - if err := workflow.ExecuteActivity(workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ - ScheduleToCloseTimeout: time.Second, - }), PickGreeting).Get(ctx, &greeting); err != nil { - return "", err - } - return fmt.Sprintf("%s %s", greeting, subject), nil -} - -func PickGreeting(ctx context.Context) (string, error) { - return "Hello", nil -} - func BenchmarkRunWorkflow(b *testing.B) { for i := 0; i < b.N; i++ { func(b *testing.B) { @@ -53,15 +38,14 @@ func BenchmarkRunWorkflow(b *testing.B) { defer c.Close() w := worker.New(c, "example", worker.Options{}) - w.RegisterWorkflow(Greet) - w.RegisterActivity(PickGreeting) + helloworld.RegisterWorkflowsAndActivities(w) if err := w.Start(); err != nil { panic(err) } defer w.Stop() - wfr, err := c.ExecuteWorkflow(ctx, client.StartWorkflowOptions{TaskQueue: "example"}, Greet, "world") + wfr, err := c.ExecuteWorkflow(ctx, client.StartWorkflowOptions{TaskQueue: "example"}, helloworld.Greet, "world") if err != nil { b.Fatal(err) } diff --git a/server/temporaltest/server_test.go b/server/temporaltest/server_test.go index 518b4f1f..556a3489 100644 --- a/server/temporaltest/server_test.go +++ b/server/temporaltest/server_test.go @@ -2,32 +2,17 @@ package temporaltest_test import ( "context" - "fmt" "testing" "time" + "github.com/DataDog/temporalite/internal/examples/helloworld" "github.com/DataDog/temporalite/server" "github.com/DataDog/temporalite/server/temporaltest" + "go.temporal.io/sdk/client" "go.temporal.io/sdk/worker" - "go.temporal.io/sdk/workflow" ) -func Greet(ctx workflow.Context, subject string) (string, error) { - var greeting string - if err := workflow.ExecuteActivity(workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ - ScheduleToCloseTimeout: time.Second, - }), PickGreeting).Get(ctx, &greeting); err != nil { - return "", err - } - - return fmt.Sprintf("%s %s", greeting, subject), nil -} - -func PickGreeting(ctx context.Context) (string, error) { - return "Hello", nil -} - func TestNewServer(t *testing.T) { // Create test Temporal server srv := temporaltest.NewServer(server.WithNamespaces("default")) @@ -44,15 +29,14 @@ func TestNewServer(t *testing.T) { defer c.Close() w := worker.New(c, "example", worker.Options{}) - w.RegisterWorkflow(Greet) - w.RegisterActivity(PickGreeting) + helloworld.RegisterWorkflowsAndActivities(w) if err := w.Start(); err != nil { t.Fatal(err) } defer w.Stop() - wfr, err := c.ExecuteWorkflow(ctx, client.StartWorkflowOptions{TaskQueue: "example"}, Greet, "world") + wfr, err := c.ExecuteWorkflow(ctx, client.StartWorkflowOptions{TaskQueue: "example"}, helloworld.Greet, "world") if err != nil { t.Fatal(err) }