Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
use shared helloworld worker implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Aug 4, 2021
1 parent 0719c32 commit c0be89d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
33 changes: 33 additions & 0 deletions internal/examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 3 additions & 19 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down
24 changes: 4 additions & 20 deletions server/temporaltest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
}
Expand Down

0 comments on commit c0be89d

Please sign in to comment.