Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Automatically stop server after test completion via T.Cleanup (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Dec 15, 2021
1 parent 627e05e commit e9d2bf9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions temporaltest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type TestServerOption interface {
}

// WithT directs all worker and client logs to the test logger.
//
// If this option is specified, then server will automatically be stopped when the
// test completes.
func WithT(t *testing.T) TestServerOption {
return newApplyFuncContainer(func(server *TestServer) {
server.t = t
Expand Down
12 changes: 10 additions & 2 deletions temporaltest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ func (ts *TestServer) Stop() {
ts.server.Stop()
}

// NewServer starts and returns a new TestServer. The caller should call Stop
// when finished, to shut it down.
// NewServer starts and returns a new TestServer.
//
// If not specifying the WithT option, the caller should execute Stop when finished to close
// the server and release resources.
func NewServer(opts ...TestServerOption) *TestServer {
rand.Seed(time.Now().UnixNano())
testNamespace := fmt.Sprintf("temporaltest-%d", rand.Intn(999999))
Expand All @@ -111,6 +113,12 @@ func NewServer(opts ...TestServerOption) *TestServer {
opt.apply(&ts)
}

if ts.t != nil {
ts.t.Cleanup(func() {
ts.Stop()
})
}

s, err := temporalite.NewServer(
temporalite.WithNamespaces(ts.defaultTestNamespace),
temporalite.WithPersistenceDisabled(),
Expand Down
12 changes: 4 additions & 8 deletions temporaltest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"testing"
"time"

"github.com/DataDog/temporalite/internal/examples/helloworld"
"github.com/DataDog/temporalite/temporaltest"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"

"github.com/DataDog/temporalite/internal/examples/helloworld"
"github.com/DataDog/temporalite/temporaltest"
)

// to be used in example code
Expand All @@ -22,17 +23,13 @@ var t *testing.T
func ExampleNewServer_testWorker() {
// Create test Temporal server and client
ts := temporaltest.NewServer(temporaltest.WithT(t))
// Stop server and close clients when tests complete
defer ts.Stop()
c := ts.Client()

// Register a new worker on the `hello_world` task queue
ts.Worker("hello_world", func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
})

// Create a test client
c := ts.Client()

// Start test workflow
wfr, err := c.ExecuteWorkflow(
context.Background(),
Expand All @@ -57,7 +54,6 @@ func ExampleNewServer_testWorker() {

func TestNewServer(t *testing.T) {
ts := temporaltest.NewServer(temporaltest.WithT(t))
defer ts.Stop()

ts.Worker("hello_world", func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
Expand Down

0 comments on commit e9d2bf9

Please sign in to comment.