Skip to content

Commit b7eed0f

Browse files
committed
Remove Client interface, return type instead
1 parent fec222d commit b7eed0f

File tree

26 files changed

+64
-79
lines changed

26 files changed

+64
-79
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Create a `Client` instance then then call `CancelWorkflow` to cancel a workflow.
268268
Sub-workflows will be canceled if their parent workflow is canceled.
269269

270270
```go
271-
var c client.Client
271+
var c *client.Client
272272
err = c.CancelWorkflowInstance(context.Background(), workflowInstance)
273273
if err != nil {
274274
panic("could not cancel workflow")

backend/test/backendtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func BackendTest(t *testing.T, setup func(options ...backend.BackendOption) Test
362362
}
363363
}
364364

365-
func startWorkflow(t *testing.T, ctx context.Context, b backend.Backend, c client.Client, instance *core.WorkflowInstance) {
365+
func startWorkflow(t *testing.T, ctx context.Context, b backend.Backend, c *client.Client, instance *core.WorkflowInstance) {
366366
err := b.CreateWorkflowInstance(
367367
ctx, instance, history.NewHistoryEvent(1, time.Now(), history.EventType_WorkflowExecutionStarted, &history.ExecutionStartedAttributes{}))
368368
require.NoError(t, err)

backend/test/e2e.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type backendTest struct {
2323
name string
2424
options []backend.BackendOption
2525
withoutCache bool // If set, test will only be run when the cache is disabled
26-
f func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend)
26+
f func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend)
2727
}
2828

2929
func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOption) TestBackend, teardown func(b TestBackend)) {
3030
tests := []backendTest{
3131
{
3232
name: "SimpleWorkflow",
33-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
33+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
3434
wf := func(ctx workflow.Context, msg string) (string, error) {
3535
return msg + " world", nil
3636
}
@@ -44,7 +44,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
4444
},
4545
{
4646
name: "SimpleWorkflow_ExpectedHistory",
47-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
47+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
4848
wf := func(ctx workflow.Context, msg string) (string, error) {
4949
return msg + " world", nil
5050
}
@@ -66,7 +66,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
6666
},
6767
{
6868
name: "UnregisteredWorkflow_Errors",
69-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
69+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
7070
wf := func(ctx workflow.Context, msg string) (string, error) {
7171
return msg + " world", nil
7272
}
@@ -80,7 +80,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
8080
},
8181
{
8282
name: "WorkflowArgumentMismatch",
83-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
83+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
8484
wf := func(ctx workflow.Context, p1 int) (int, error) {
8585
return 42, nil
8686
}
@@ -96,7 +96,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
9696
},
9797
{
9898
name: "UnregisteredActivity_Errors",
99-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
99+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
100100
a := func(context.Context) error { return nil }
101101
wf := func(ctx workflow.Context) (int, error) {
102102
return workflow.ExecuteActivity[int](ctx, workflow.ActivityOptions{
@@ -115,7 +115,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
115115
},
116116
{
117117
name: "ActivityArgumentMismatch",
118-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
118+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
119119
a := func(context.Context, int, int) error { return nil }
120120
wf := func(ctx workflow.Context) (int, error) {
121121
return workflow.ExecuteActivity[int](ctx, workflow.ActivityOptions{
@@ -134,7 +134,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
134134
},
135135
{
136136
name: "SideEffect_Simple",
137-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
137+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
138138
i := 2
139139
wf := func(ctx workflow.Context) (int, error) {
140140
r1, _ := workflow.SideEffect(ctx, func(ctx workflow.Context) int {
@@ -163,7 +163,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
163163
},
164164
{
165165
name: "Signal_after_completion",
166-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
166+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
167167
wf := func(ctx workflow.Context) error {
168168
return nil
169169
}
@@ -180,7 +180,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
180180
},
181181
{
182182
name: "SubWorkflow_Simple",
183-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
183+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
184184
swf := func(ctx workflow.Context, i int) (int, error) {
185185
return i * 2, nil
186186
}
@@ -203,7 +203,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
203203
},
204204
{
205205
name: "SubWorkflow_PropagateCancellation",
206-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
206+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
207207
canceled := int32(0)
208208

209209
swf := func(ctx workflow.Context, i int) (int, error) {
@@ -267,7 +267,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
267267
},
268268
{
269269
name: "SubWorkflow_CancelBeforeStarting",
270-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
270+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
271271
swInstanceID := "subworkflow"
272272

273273
swfrun := 0
@@ -313,7 +313,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
313313
},
314314
{
315315
name: "SubWorkflow_Signal",
316-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
316+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
317317
swf := func(ctx workflow.Context, i int) (int, error) {
318318
workflow.NewSignalChannel[string](ctx, "signal").Receive(ctx)
319319

@@ -347,7 +347,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
347347
},
348348
{
349349
name: "SubWorkflow_Signal_BeforeStarting",
350-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
350+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
351351
wf := func(ctx workflow.Context) (int, error) {
352352
id, _ := workflow.SideEffect(ctx, func(ctx workflow.Context) string {
353353
id := uuid.New().String()
@@ -371,7 +371,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
371371
},
372372
{
373373
name: "Timer_CancelWorkflowInstance",
374-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
374+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
375375
a := func(ctx context.Context) error {
376376
return nil
377377
}
@@ -398,7 +398,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
398398
},
399399
{
400400
name: "Timer_CancelBeforeStarting",
401-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
401+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
402402
a := func(ctx context.Context) error {
403403
return nil
404404
}
@@ -441,7 +441,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
441441
},
442442
{
443443
name: "Timer_CancelTwice",
444-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
444+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
445445
a := func(ctx context.Context) error {
446446
return nil
447447
}
@@ -484,7 +484,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
484484
},
485485
{
486486
name: "Timer_CancelBeforeFiringRemovesFutureEvent",
487-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
487+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
488488
a := func(ctx context.Context) error {
489489
return nil
490490
}
@@ -523,7 +523,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
523523
{
524524
name: "NonDeterminism",
525525
withoutCache: true,
526-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
526+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
527527
i := 0
528528
wf := func(ctx workflow.Context) (int, error) {
529529
var r int
@@ -553,7 +553,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
553553
},
554554
{
555555
name: "RemoveWorkflowInstance",
556-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
556+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
557557
wf := func(ctx workflow.Context, msg string) (string, error) {
558558
return msg + " world", nil
559559
}
@@ -576,7 +576,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
576576
{
577577
name: "ContextPropagation",
578578
options: []backend.BackendOption{backend.WithContextPropagator(&testContextPropagator{})},
579-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
579+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
580580
a := func(ctx context.Context) (int, error) {
581581
d := myValues(ctx)
582582
return d.Count, nil
@@ -610,7 +610,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
610610
},
611611
{
612612
name: "ContinueAsNew",
613-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
613+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
614614
wf := func(ctx workflow.Context, run int) (int, error) {
615615
run = run + 1
616616
if run < 3 {
@@ -634,7 +634,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti
634634
},
635635
{
636636
name: "ContinueAsNew_Subworkflow",
637-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
637+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
638638
swf := func(ctx workflow.Context, run int) (int, error) {
639639
l := workflow.Logger(ctx)
640640

@@ -740,7 +740,7 @@ func register(t *testing.T, ctx context.Context, w *worker.Worker, workflows []i
740740
require.NoError(t, err)
741741
}
742742

743-
func runWorkflow(t *testing.T, ctx context.Context, c client.Client, wf interface{}, inputs ...interface{}) *workflow.Instance {
743+
func runWorkflow(t *testing.T, ctx context.Context, c *client.Client, wf interface{}, inputs ...interface{}) *workflow.Instance {
744744
instance, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
745745
InstanceID: uuid.NewString(),
746746
}, wf, inputs...)
@@ -749,7 +749,7 @@ func runWorkflow(t *testing.T, ctx context.Context, c client.Client, wf interfac
749749
return instance
750750
}
751751

752-
func runWorkflowWithResult[T any](t *testing.T, ctx context.Context, c client.Client, wf interface{}, inputs ...interface{}) (T, error) {
752+
func runWorkflowWithResult[T any](t *testing.T, ctx context.Context, c *client.Client, wf interface{}, inputs ...interface{}) (T, error) {
753753
instance := runWorkflow(t, ctx, c, wf, inputs...)
754754
return client.GetWorkflowResult[T](ctx, c, instance, time.Second*10)
755755
}

backend/test/e2e_activity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (e *CustomError) Error() string {
2222
var e2eActivityTests = []backendTest{
2323
{
2424
name: "Activity_Panic",
25-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
25+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
2626
a := func(context.Context) error {
2727
panic("activity panic")
2828
}
@@ -47,7 +47,7 @@ var e2eActivityTests = []backendTest{
4747
},
4848
{
4949
name: "Activity_CustomError",
50-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
50+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
5151
a := func(context.Context) error {
5252
return &CustomError{msg: "custom error"}
5353
}

backend/test/e2e_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
var e2eStatsTests = []backendTest{
1515
{
1616
name: "Stats_ActiveInstance",
17-
f: func(t *testing.T, ctx context.Context, c client.Client, w *worker.Worker, b TestBackend) {
17+
f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) {
1818
as := make(chan bool, 1)
1919
af := make(chan bool, 1)
2020

client/client.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,19 @@ type WorkflowInstanceOptions struct {
3030
InstanceID string
3131
}
3232

33-
type Client interface {
34-
CreateWorkflowInstance(ctx context.Context, options WorkflowInstanceOptions, wf workflow.Workflow, args ...interface{}) (*workflow.Instance, error)
35-
36-
CancelWorkflowInstance(ctx context.Context, instance *workflow.Instance) error
37-
38-
RemoveWorkflowInstance(ctx context.Context, instance *workflow.Instance) error
39-
40-
WaitForWorkflowInstance(ctx context.Context, instance *workflow.Instance, timeout time.Duration) error
41-
42-
SignalWorkflow(ctx context.Context, instanceID string, name string, arg interface{}) error
43-
44-
GetStats(ctx context.Context) (*backend.Stats, error)
45-
}
46-
47-
type client struct {
33+
type Client struct {
4834
backend backend.Backend
4935
clock clock.Clock
5036
}
5137

52-
func New(backend backend.Backend) Client {
53-
return &client{
38+
func New(backend backend.Backend) *Client {
39+
return &Client{
5440
backend: backend,
5541
clock: clock.New(),
5642
}
5743
}
5844

59-
func (c *client) CreateWorkflowInstance(ctx context.Context, options WorkflowInstanceOptions, wf workflow.Workflow, args ...interface{}) (*workflow.Instance, error) {
45+
func (c *Client) CreateWorkflowInstance(ctx context.Context, options WorkflowInstanceOptions, wf workflow.Workflow, args ...interface{}) (*workflow.Instance, error) {
6046
// Check arguments
6147
if err := a.ParamsMatch(wf, args...); err != nil {
6248
return nil, err
@@ -103,7 +89,7 @@ func (c *client) CreateWorkflowInstance(ctx context.Context, options WorkflowIns
10389
return wfi, nil
10490
}
10591

106-
func (c *client) CancelWorkflowInstance(ctx context.Context, instance *workflow.Instance) error {
92+
func (c *Client) CancelWorkflowInstance(ctx context.Context, instance *workflow.Instance) error {
10793
ctx, span := c.backend.Tracer().Start(ctx, "CancelWorkflowInstance", trace.WithAttributes(
10894
attribute.String(log.InstanceIDKey, instance.InstanceID),
10995
))
@@ -113,7 +99,7 @@ func (c *client) CancelWorkflowInstance(ctx context.Context, instance *workflow.
11399
return c.backend.CancelWorkflowInstance(ctx, instance, cancellationEvent)
114100
}
115101

116-
func (c *client) SignalWorkflow(ctx context.Context, instanceID string, name string, arg interface{}) error {
102+
func (c *Client) SignalWorkflow(ctx context.Context, instanceID string, name string, arg interface{}) error {
117103
ctx, span := c.backend.Tracer().Start(ctx, "SignalWorkflow", trace.WithAttributes(
118104
attribute.String(log.InstanceIDKey, instanceID),
119105
attribute.String(log.SignalNameKey, name),
@@ -145,7 +131,7 @@ func (c *client) SignalWorkflow(ctx context.Context, instanceID string, name str
145131
return nil
146132
}
147133

148-
func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow.Instance, timeout time.Duration) error {
134+
func (c *Client) WaitForWorkflowInstance(ctx context.Context, instance *workflow.Instance, timeout time.Duration) error {
149135
if timeout == 0 {
150136
timeout = time.Second * 20
151137
}
@@ -185,9 +171,8 @@ func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow
185171

186172
// GetWorkflowResult gets the workflow result for the given workflow result. It first waits for the workflow to finish or until
187173
// the given timeout has expired.
188-
func GetWorkflowResult[T any](ctx context.Context, c Client, instance *workflow.Instance, timeout time.Duration) (T, error) {
189-
ic := c.(*client)
190-
b := ic.backend
174+
func GetWorkflowResult[T any](ctx context.Context, c *Client, instance *workflow.Instance, timeout time.Duration) (T, error) {
175+
b := c.backend
191176

192177
ctx, span := b.Tracer().Start(ctx, "GetWorkflowResult", trace.WithAttributes(
193178
attribute.String(log.InstanceIDKey, instance.InstanceID),
@@ -241,7 +226,7 @@ func GetWorkflowResult[T any](ctx context.Context, c Client, instance *workflow.
241226
return *new(T), errors.New("workflow finished, but could not find result event")
242227
}
243228

244-
func (c *client) RemoveWorkflowInstance(ctx context.Context, instance *core.WorkflowInstance) error {
229+
func (c *Client) RemoveWorkflowInstance(ctx context.Context, instance *core.WorkflowInstance) error {
245230
ctx, span := c.backend.Tracer().Start(ctx, "RemoveWorkflowInstance", trace.WithAttributes(
246231
attribute.String(log.InstanceIDKey, instance.InstanceID),
247232
))

client/client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_Client_CreateWorkflowInstance_ParamMismatch(t *testing.T) {
2727
ctx := context.Background()
2828

2929
b := &backend.MockBackend{}
30-
c := &client{
30+
c := &Client{
3131
backend: b,
3232
clock: clock.New(),
3333
}
@@ -49,7 +49,7 @@ func Test_Client_GetWorkflowResultTimeout(t *testing.T) {
4949
b.On("Tracer").Return(trace.NewNoopTracerProvider().Tracer("test"))
5050
b.On("GetWorkflowInstanceState", mock.Anything, instance).Return(core.WorkflowInstanceStateActive, nil)
5151

52-
c := &client{
52+
c := &Client{
5353
backend: b,
5454
clock: clock.New(),
5555
}
@@ -85,7 +85,7 @@ func Test_Client_GetWorkflowResultSuccess(t *testing.T) {
8585
}, nil)
8686
b.On("Converter").Return(converter.DefaultConverter)
8787

88-
c := &client{
88+
c := &Client{
8989
backend: b,
9090
clock: mockClock,
9191
}
@@ -110,7 +110,7 @@ func Test_Client_SignalWorkflow(t *testing.T) {
110110
event.Attributes.(*history.SignalReceivedAttributes).Name == "test"
111111
})).Return(nil)
112112

113-
c := &client{
113+
c := &Client{
114114
backend: b,
115115
clock: clock.New(),
116116
}
@@ -140,7 +140,7 @@ func Test_Client_SignalWorkflow_WithArgs(t *testing.T) {
140140
bytes.Equal(event.Attributes.(*history.SignalReceivedAttributes).Arg, input)
141141
})).Return(nil)
142142

143-
c := &client{
143+
c := &Client{
144144
backend: b,
145145
clock: clock.New(),
146146
}

0 commit comments

Comments
 (0)