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

Commit

Permalink
Deprecate Worker() and Client() in favor of NewWorker() DefaultClient…
Browse files Browse the repository at this point in the history
…() (#95)
  • Loading branch information
grantfuhr committed Aug 4, 2022
1 parent 15c4892 commit 95f1070
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
31 changes: 24 additions & 7 deletions temporaltest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (ts *TestServer) fatal(err error) {
ts.t.Fatal(err)
}

// Worker registers and starts a Temporal worker on the specified task queue.
func (ts *TestServer) Worker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker {
w := worker.New(ts.Client(), taskQueue, ts.defaultWorkerOptions)
// NewWorker registers and starts a Temporal worker on the specified task queue.
func (ts *TestServer) NewWorker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker {
w := worker.New(ts.DefaultClient(), taskQueue, ts.defaultWorkerOptions)
registerFunc(w)
ts.workers = append(ts.workers, w)

Expand All @@ -59,7 +59,7 @@ func (ts *TestServer) Worker(taskQueue string, registerFunc func(registry worker
func (ts *TestServer) NewWorkerWithOptions(taskQueue string, registerFunc func(registry worker.Registry), opts worker.Options) worker.Worker {
opts.WorkflowPanicPolicy = worker.FailWorkflow

w := worker.New(ts.Client(), taskQueue, opts)
w := worker.New(ts.DefaultClient(), taskQueue, opts)
registerFunc(w)
ts.workers = append(ts.workers, w)

Expand All @@ -70,17 +70,17 @@ func (ts *TestServer) NewWorkerWithOptions(taskQueue string, registerFunc func(r
return w
}

// Client returns a Temporal client configured for making requests to the server.
// DefaultClient returns the default Temporal client configured for making requests to the server.
// It is configured to use a pre-registered test namespace and will
// be closed on TestServer.Stop.
func (ts *TestServer) Client() client.Client {
func (ts *TestServer) DefaultClient() client.Client {
if ts.defaultClient == nil {
ts.defaultClient = ts.NewClientWithOptions(ts.defaultClientOptions)
}
return ts.defaultClient
}

// NewClientWithOptions returns a Temporal client configured for making requests to the server.
// NewClientWithOptions returns a new Temporal client configured for making requests to the server.
// If no namespace option is set it will use a pre-registered test namespace.
// The returned client will be closed on TestServer.Stop.
func (ts *TestServer) NewClientWithOptions(opts client.Options) client.Client {
Expand Down Expand Up @@ -161,3 +161,20 @@ func NewServer(opts ...TestServerOption) *TestServer {

return &ts
}

// Worker registers and starts a Temporal worker on the specified task queue.
//
// Deprecated: Use function NewWorker()
func (ts *TestServer) Worker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker {
return ts.NewWorker(taskQueue, registerFunc)
}

// Client returns a Temporal client configured for making requests to the server.
// It is configured to use a pre-registered test namespace and will
// be closed on TestServer.Stop.
//
// Deprecated: Use function DefaultClient()
func (ts *TestServer) Client() client.Client {
return ts.DefaultClient()
}

22 changes: 11 additions & 11 deletions temporaltest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var t *testing.T
func ExampleNewServer_testWorker() {
// Create test Temporal server and client
ts := temporaltest.NewServer(temporaltest.WithT(t))
c := ts.Client()
c := ts.DefaultClient()

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

Expand Down Expand Up @@ -55,14 +55,14 @@ func ExampleNewServer_testWorker() {
func TestNewServer(t *testing.T) {
ts := temporaltest.NewServer(temporaltest.WithT(t))

ts.Worker("hello_world", func(registry worker.Registry) {
ts.NewWorker("hello_world", func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
})

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

wfr, err := ts.Client().ExecuteWorkflow(
wfr, err := ts.DefaultClient().ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{TaskQueue: "hello_world"},
helloworld.Greet,
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestNewWorkerWithOptions(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

wfr, err := ts.Client().ExecuteWorkflow(
wfr, err := ts.DefaultClient().ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{TaskQueue: "hello_world"},
helloworld.Greet,
Expand Down Expand Up @@ -131,14 +131,14 @@ func TestDefaultWorkerOptions(t *testing.T) {
),
)

ts.Worker("hello_world", func(registry worker.Registry) {
ts.NewWorker("hello_world", func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
})

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

wfr, err := ts.Client().ExecuteWorkflow(
wfr, err := ts.DefaultClient().ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{TaskQueue: "hello_world"},
helloworld.Greet,
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestClientWithDefaultInterceptor(t *testing.T) {
temporaltest.WithBaseClientOptions(opts),
)

ts.Worker(
ts.NewWorker(
"hello_world",
func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
Expand All @@ -176,7 +176,7 @@ func TestClientWithDefaultInterceptor(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

wfr, err := ts.Client().ExecuteWorkflow(
wfr, err := ts.DefaultClient().ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{TaskQueue: "hello_world"},
helloworld.Greet,
Expand All @@ -200,10 +200,10 @@ func BenchmarkRunWorkflow(b *testing.B) {
ts := temporaltest.NewServer()
defer ts.Stop()

ts.Worker("hello_world", func(registry worker.Registry) {
ts.NewWorker("hello_world", func(registry worker.Registry) {
helloworld.RegisterWorkflowsAndActivities(registry)
})
c := ts.Client()
c := ts.DefaultClient()

for i := 0; i < b.N; i++ {
func(b *testing.B) {
Expand Down

0 comments on commit 95f1070

Please sign in to comment.