-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4cdadf
commit 6ea9b39
Showing
3 changed files
with
132 additions
and
0 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
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) | ||
} |
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 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 | ||
} |
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,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) | ||
} |