Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show use of DevServer to test #330

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading