-
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
d7f2c05
commit ec7971d
Showing
6 changed files
with
251 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
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
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,116 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"github.com/temporalio/cli/app" | ||
"github.com/urfave/cli/v2" | ||
"go.temporal.io/api/operatorservice/v1" | ||
"go.temporal.io/api/workflowservice/v1" | ||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/testsuite" | ||
"go.temporal.io/sdk/worker" | ||
healthpb "google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
type ( | ||
e2eSuite struct { | ||
suite.Suite | ||
app *cli.App | ||
ts *testsuite.DevServer | ||
workers []worker.Worker | ||
defaultWorkerOptions worker.Options | ||
writer *MemWriter | ||
} | ||
) | ||
|
||
func TestClientIntegrationSuite(t *testing.T) { | ||
suite.Run(t, new(e2eSuite)) | ||
} | ||
|
||
func (s *e2eSuite) SetupSuite() { | ||
s.app = app.BuildApp() | ||
server, err := testsuite.StartDevServer(context.Background(), testsuite.DevServerOptions{}) | ||
s.NoError(err) | ||
s.ts = server | ||
} | ||
|
||
func (s *e2eSuite) TearDownSuite() { | ||
} | ||
|
||
func (s *e2eSuite) SetupTest() { | ||
app.SetFactory(&clientFactory{ | ||
frontendClient: nil, | ||
operatorClient: nil, | ||
sdkClient: s.ts.Client(), | ||
}) | ||
s.writer = &MemWriter{} | ||
s.app.Writer = s.writer | ||
} | ||
|
||
func (s *e2eSuite) TearDownTest() { | ||
s.ts.Stop() | ||
} | ||
|
||
func (s *e2eSuite) NewWorker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker { | ||
w := worker.New(s.ts.Client(), taskQueue, s.defaultWorkerOptions) | ||
registerFunc(w) | ||
s.workers = append(s.workers, w) | ||
|
||
err := w.Start() | ||
s.NoError(err) | ||
|
||
return w | ||
} | ||
|
||
type clientFactory struct { | ||
frontendClient workflowservice.WorkflowServiceClient | ||
operatorClient operatorservice.OperatorServiceClient | ||
sdkClient client.Client | ||
} | ||
|
||
func (m *clientFactory) FrontendClient(c *cli.Context) workflowservice.WorkflowServiceClient { | ||
return m.frontendClient | ||
} | ||
|
||
func (m *clientFactory) OperatorClient(c *cli.Context) operatorservice.OperatorServiceClient { | ||
return m.operatorClient | ||
} | ||
|
||
func (m *clientFactory) SDKClient(c *cli.Context, namespace string) client.Client { | ||
return m.sdkClient | ||
} | ||
|
||
func (m *clientFactory) HealthClient(_ *cli.Context) healthpb.HealthClient { | ||
panic("HealthClient mock is not supported") | ||
} | ||
|
||
// MemWriter is an io.Writer implementation that stores the written content. | ||
type MemWriter struct { | ||
content bytes.Buffer | ||
} | ||
|
||
func (tlw *MemWriter) Write(p []byte) (n int, err error) { | ||
return tlw.content.Write(p) | ||
} | ||
|
||
func (tlw *MemWriter) GetContent() string { | ||
return tlw.content.String() | ||
} | ||
|
||
func TestMemWriter(t *testing.T) { | ||
tlw := &MemWriter{} | ||
fmt.Fprintln(tlw, "This message is written to the TestLogWriter.") | ||
|
||
content := tlw.GetContent() | ||
expected := "This message is written to the TestLogWriter." | ||
|
||
if !strings.Contains(content, expected) { | ||
t.Errorf("Expected log content to contain '%s', but it doesn't. Content: '%s'", expected, content) | ||
} | ||
} |
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 *e2eSuite) TestWorkflowShow_ReplayableHistory() { | ||
c := s.ts.Client() | ||
|
||
s.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) | ||
} |