From f25b9cbe90afb16e4d2af31c334eb75602edc193 Mon Sep 17 00:00:00 2001 From: muralisrini Date: Mon, 5 Feb 2024 12:42:13 -0500 Subject: [PATCH] show use of DevServer to test (#330) --- helloworld/helloworld_test.go | 65 ++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/helloworld/helloworld_test.go b/helloworld/helloworld_test.go index d4b95694..092c2ad2 100644 --- a/helloworld/helloworld_test.go +++ b/helloworld/helloworld_test.go @@ -1,12 +1,15 @@ package helloworld import ( + "context" "testing" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" + + "go.temporal.io/sdk/client" "go.temporal.io/sdk/testsuite" + "go.temporal.io/sdk/worker" ) func Test_Workflow(t *testing.T) { @@ -37,3 +40,63 @@ func Test_Activity(t *testing.T) { require.NoError(t, val.Get(&res)) require.Equal(t, "Hello World!", res) } + +func Test_Using_DevServer(t *testing.T) { + //"" will let use a random port in local env + hostPort := "" + server, err := testsuite.StartDevServer(context.Background(), testsuite.DevServerOptions{ClientOptions: &client.Options{HostPort: hostPort}}) + require.NoError(t, err) + require.NotNil(t, server) + + var ( + c client.Client + w worker.Worker + wInChan <-chan interface{} + ) + + taskQ := "hello-world" + + ch := make(chan interface{}) + go func() { + c = server.Client() + w = worker.New(c, taskQ, worker.Options{}) + wInChan = worker.InterruptCh() + + ch <- struct{}{} + + _ = w.Run(wInChan) + }() + + <-ch + + require.NotNil(t, c) + require.NotNil(t, w) + require.NotNil(t, wInChan) + + // register activity and workflow + w.RegisterWorkflow(Workflow) + w.RegisterActivity(Activity) + + // run the workflow application (equivalent to starter/main.go) + workflowOptions := client.StartWorkflowOptions{ + ID: "hello_world_workflowID", + TaskQueue: taskQ, + } + + we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, Workflow, "Temporal") + require.NoError(t, err) + require.NotNil(t, we) + + // Synchronously wait for the workflow completion. + var result string + err = we.Get(context.Background(), &result) + require.NoError(t, err) + require.Equal(t, "Hello Temporal!", result) + + // stop worker + w.Stop() + + // stop server + err = server.Stop() + require.NoError(t, err) +}