Skip to content

Commit

Permalink
Add test for history replay
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples committed Mar 25, 2023
1 parent d4cdadf commit 6ea9b39
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tests

import (
"context"
"io/ioutil"
"os"

"github.com/pborman/uuid"
"github.com/temporalio/cli/tests/workflows/helloworld"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
)

const (
testTq = "test-queue"
)

func (s *integrationSuite) TestWorkflowShow_ReplayableHistory() {
c := s.ts.GetDefaultClient()

s.ts.NewWorker(testTq, func(r worker.Registry) {
r.RegisterWorkflow(helloworld.Workflow)
r.RegisterActivity(helloworld.Activity)
})

wfr, err := c.ExecuteWorkflow(
context.Background(),
client.StartWorkflowOptions{TaskQueue: testTq},
helloworld.Workflow,
"world",
)
s.NoError(err)

var result string
err = wfr.Get(context.Background(), &result)
s.NoError(err)

// show history
err = s.app.Run([]string{"", "workflow", "show", "--workflow-id", wfr.GetID(), "--run-id", wfr.GetRunID(), "--output", "json"})
s.NoError(err)

// save history to file
historyFile := uuid.New() + ".json"
logs := s.writer.GetContent()
err = ioutil.WriteFile(historyFile, []byte(logs), 0644)
s.NoError(err)
defer os.Remove(historyFile)

replayer := worker.NewWorkflowReplayer()
replayer.RegisterWorkflow(helloworld.Workflow)
err = replayer.ReplayWorkflowHistoryFromJSONFile(nil, historyFile)
s.NoError(err)
}
40 changes: 40 additions & 0 deletions tests/workflows/helloworld/helloworld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package helloworld

import (
"context"
"time"

"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"

// TODO(cretz): Remove when tagged
_ "go.temporal.io/sdk/contrib/tools/workflowcheck/determinism"
)

// Workflow is a Hello World workflow definition.
func Workflow(ctx workflow.Context, name string) (string, error) {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)

logger := workflow.GetLogger(ctx)
logger.Info("HelloWorld workflow started", "name", name)

var result string
err := workflow.ExecuteActivity(ctx, Activity, name).Get(ctx, &result)
if err != nil {
logger.Error("Activity failed.", "Error", err)
return "", err
}

logger.Info("HelloWorld workflow completed.", "result", result)

return result, nil
}

func Activity(ctx context.Context, name string) (string, error) {
logger := activity.GetLogger(ctx)
logger.Info("Activity", "name", name)
return "Hello " + name + "!", nil
}
39 changes: 39 additions & 0 deletions tests/workflows/helloworld/helloworld_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package helloworld

import (
"testing"

"github.com/stretchr/testify/mock"

"github.com/stretchr/testify/require"
"go.temporal.io/sdk/testsuite"
)

func Test_Workflow(t *testing.T) {
testSuite := &testsuite.WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()

// Mock activity implementation
env.OnActivity(Activity, mock.Anything, "Temporal").Return("Hello Temporal!", nil)

env.ExecuteWorkflow(Workflow, "Temporal")

require.True(t, env.IsWorkflowCompleted())
require.NoError(t, env.GetWorkflowError())
var result string
require.NoError(t, env.GetWorkflowResult(&result))
require.Equal(t, "Hello Temporal!", result)
}

func Test_Activity(t *testing.T) {
testSuite := &testsuite.WorkflowTestSuite{}
env := testSuite.NewTestActivityEnvironment()
env.RegisterActivity(Activity)

val, err := env.ExecuteActivity(Activity, "World")
require.NoError(t, err)

var res string
require.NoError(t, val.Get(&res))
require.Equal(t, "Hello World!", res)
}

0 comments on commit 6ea9b39

Please sign in to comment.