Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go-sandbox/activities/basics/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func YourWorkflowDefinition(ctx workflow.Context, param YourWorkflowParam) (*You

// @@@SNIPEND

// @@@SNIPSTART go-workflows-basics-simple
func YourSimpleWorkflowDefinition(ctx workflow.Context) error {
// ...
return nil
}
// @@@SNIPEND
51 changes: 51 additions & 0 deletions go-sandbox/workflows/basics/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package basics

import (
"time"

"github.com/google/uuid"
"go.temporal.io/sdk/workflow"
)

// @@@SNIPSTART go-workflows-basics-logging
func MyWorkflow(ctx workflow.Context, name string) (string, error) {
logger := workflow.GetLogger(ctx)
logger.Info("Starting workflow", "name", name)
// ...
return "Hello " + name, nil
}
// @@@SNIPEND

type DeterministicValues struct {
ID string
CurrentTime time.Time
}

// DeterministicValuesWorkflow generates values that stay the same when the
// Workflow replays.
func DeterministicValuesWorkflow(ctx workflow.Context) (*DeterministicValues, error) {
// @@@SNIPSTART go-workflows-basics-side-effect-uuid
var id string
encodedID := workflow.SideEffect(ctx, func(ctx workflow.Context) interface{} {
return uuid.New().String()
})
if err := encodedID.Get(&id); err != nil {
return nil, err
}
// @@@SNIPEND

// @@@SNIPSTART go-workflows-basics-now
currentTime := workflow.Now(ctx)
// @@@SNIPEND

// @@@SNIPSTART go-workflows-basics-is-replaying
if !workflow.IsReplaying(ctx) {
emitMetric("workflow_started", 1)
}
// @@@SNIPEND

return &DeterministicValues{ID: id, CurrentTime: currentTime}, nil
}

// emitMetric stands in for a call to an external metrics system.
func emitMetric(name string, value int) {}
44 changes: 44 additions & 0 deletions go-sandbox/workflows/basics/workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package basics_test

import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"

"my-org/quickstart/workflows/basics"
)

type WorkflowBasicsTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}

func TestWorkflowBasicsTestSuite(t *testing.T) {
suite.Run(t, new(WorkflowBasicsTestSuite))
}

func (s *WorkflowBasicsTestSuite) Test_MyWorkflow() {
env := s.NewTestWorkflowEnvironment()
env.ExecuteWorkflow(basics.MyWorkflow, "Temporal")
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())

var result string
s.NoError(env.GetWorkflowResult(&result))
s.Equal("Hello Temporal", result)
}

func (s *WorkflowBasicsTestSuite) Test_DeterministicValuesWorkflow() {
env := s.NewTestWorkflowEnvironment()
env.ExecuteWorkflow(basics.DeterministicValuesWorkflow)
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())

var result basics.DeterministicValues
s.NoError(env.GetWorkflowResult(&result))
_, err := uuid.Parse(result.ID)
s.NoError(err)
s.False(result.CurrentTime.IsZero())
}
Loading