Skip to content

Commit

Permalink
show use of DevServer to test
Browse files Browse the repository at this point in the history
Added a test to "hello-world" sample to illustrate use of
[DevServer](https://pkg.go.dev/go.temporal.io/sdk/testsuite#DevServer)
thus providing an alternative to testing workflows end-to-end as is
without mocks.

Signed-off-by: muralisrini <[email protected]>
  • Loading branch information
muralisrini committed Feb 5, 2024
1 parent 315c616 commit 4f55b11
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion helloworld/helloworld_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 4f55b11

Please sign in to comment.