Skip to content

Commit 171cdf0

Browse files
committed
Fix test engine root component constraint
1 parent ea21b9b commit 171cdf0

2 files changed

Lines changed: 67 additions & 56 deletions

File tree

chasm/chasmtest/test_engine.go

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ import (
1818
)
1919

2020
type (
21-
Option[T chasm.Component] func(*Engine[T])
21+
Option[T chasm.RootComponent] func(*Engine[T])
2222

23-
Engine[T chasm.Component] struct {
23+
Engine[T chasm.RootComponent] struct {
2424
t *testing.T
2525
registry *chasm.Registry
2626
logger log.Logger
2727
metrics metrics.Handler
2828

2929
rootExecutionKey chasm.ExecutionKey
3030
root T
31-
rootRuntime *runtime
32-
executions map[executionLookupKey]*runtime
31+
rootExecution *execution
32+
executions map[executionLookupKey]*execution
3333
}
3434

35-
runtime struct {
35+
execution struct {
3636
key chasm.ExecutionKey
3737
node *chasm.Node
3838
backend *chasm.MockNodeBackend
3939
timeSource *clock.EventTimeSource
40-
root chasm.Component
40+
root chasm.RootComponent
4141
}
4242

4343
executionLookupKey struct {
@@ -46,7 +46,7 @@ type (
4646
}
4747
)
4848

49-
func NewEngine[T chasm.Component](
49+
func NewEngine[T chasm.RootComponent](
5050
t *testing.T,
5151
registry *chasm.Registry,
5252
opts ...Option[T],
@@ -63,7 +63,7 @@ func NewEngine[T chasm.Component](
6363
BusinessID: "test-workflow-id",
6464
RunID: "test-run-id",
6565
},
66-
executions: make(map[executionLookupKey]*runtime),
66+
executions: make(map[executionLookupKey]*execution),
6767
}
6868

6969
for _, opt := range opts {
@@ -73,24 +73,24 @@ func NewEngine[T chasm.Component](
7373
return e
7474
}
7575

76-
func WithRoot[T chasm.Component](
76+
func WithRoot[T chasm.RootComponent](
7777
factory func(chasm.MutableContext) T,
7878
) Option[T] {
7979
return func(e *Engine[T]) {
80-
runtime := e.newRuntime(e.rootExecutionKey)
81-
ctx := chasm.NewMutableContext(context.Background(), runtime.node)
80+
execution := e.newExecution(e.rootExecutionKey)
81+
ctx := chasm.NewMutableContext(context.Background(), execution.node)
8282
root := factory(ctx)
83-
require.NoError(e.t, runtime.node.SetRootComponent(root))
84-
_, err := runtime.node.CloseTransaction()
83+
require.NoError(e.t, execution.node.SetRootComponent(root))
84+
_, err := execution.node.CloseTransaction()
8585
require.NoError(e.t, err)
8686
e.root = root
87-
runtime.root = root
88-
e.rootRuntime = runtime
89-
e.executions[newExecutionLookupKey(runtime.key)] = runtime
87+
execution.root = root
88+
e.rootExecution = execution
89+
e.executions[newExecutionLookupKey(execution.key)] = execution
9090
}
9191
}
9292

93-
func WithExecutionKey[T chasm.Component](key chasm.ExecutionKey) Option[T] {
93+
func WithExecutionKey[T chasm.RootComponent](key chasm.ExecutionKey) Option[T] {
9494
return func(e *Engine[T]) {
9595
e.rootExecutionKey = key
9696
}
@@ -105,8 +105,8 @@ func (e *Engine[T]) EngineContext() context.Context {
105105
}
106106

107107
func (e *Engine[T]) Ref(component chasm.Component) chasm.ComponentRef {
108-
for _, runtime := range e.executions {
109-
ref, err := runtime.node.Ref(component)
108+
for _, execution := range e.executions {
109+
ref, err := execution.node.Ref(component)
110110
if err != nil {
111111
continue
112112
}
@@ -129,30 +129,30 @@ func (e *Engine[T]) StartExecution(
129129
return chasm.StartExecutionResult{}, chasm.NewExecutionAlreadyStartedErr("already exists", "", ref.RunID)
130130
}
131131

132-
runtime := e.newRuntime(ref.ExecutionKey)
133-
mutableCtx := chasm.NewMutableContext(ctx, runtime.node)
132+
execution := e.newExecution(ref.ExecutionKey)
133+
mutableCtx := chasm.NewMutableContext(ctx, execution.node)
134134
root, err := startFn(mutableCtx)
135135
if err != nil {
136136
return chasm.StartExecutionResult{}, err
137137
}
138-
if err := runtime.node.SetRootComponent(root); err != nil {
138+
if err := execution.node.SetRootComponent(root); err != nil {
139139
return chasm.StartExecutionResult{}, err
140140
}
141-
_, err = runtime.node.CloseTransaction()
141+
_, err = execution.node.CloseTransaction()
142142
if err != nil {
143143
return chasm.StartExecutionResult{}, err
144144
}
145145

146-
runtime.root = root
147-
e.executions[newExecutionLookupKey(runtime.key)] = runtime
146+
execution.root = root
147+
e.executions[newExecutionLookupKey(execution.key)] = execution
148148

149-
serializedRef, err := runtime.node.Ref(root)
149+
serializedRef, err := execution.node.Ref(root)
150150
if err != nil {
151151
return chasm.StartExecutionResult{}, err
152152
}
153153

154154
return chasm.StartExecutionResult{
155-
ExecutionKey: runtime.key,
155+
ExecutionKey: execution.key,
156156
ExecutionRef: serializedRef,
157157
Created: true,
158158
}, nil
@@ -165,45 +165,45 @@ func (e *Engine[T]) UpdateWithStartExecution(
165165
updateFn func(chasm.MutableContext, chasm.Component) error,
166166
_ ...chasm.TransitionOption,
167167
) (chasm.EngineUpdateWithStartExecutionResult, error) {
168-
if runtime, ok := e.executionForKey(ref.ExecutionKey); ok {
169-
serializedRef, err := e.updateComponentInRuntime(ctx, runtime, ref, updateFn)
168+
if execution, ok := e.executionForKey(ref.ExecutionKey); ok {
169+
serializedRef, err := e.updateComponentInExecution(ctx, execution, ref, updateFn)
170170
if err != nil {
171171
return chasm.EngineUpdateWithStartExecutionResult{}, err
172172
}
173173
return chasm.EngineUpdateWithStartExecutionResult{
174-
ExecutionKey: runtime.key,
174+
ExecutionKey: execution.key,
175175
ExecutionRef: serializedRef,
176176
Created: false,
177177
}, nil
178178
}
179179

180-
runtime := e.newRuntime(ref.ExecutionKey)
181-
mutableCtx := chasm.NewMutableContext(ctx, runtime.node)
180+
execution := e.newExecution(ref.ExecutionKey)
181+
mutableCtx := chasm.NewMutableContext(ctx, execution.node)
182182
root, err := startFn(mutableCtx)
183183
if err != nil {
184184
return chasm.EngineUpdateWithStartExecutionResult{}, err
185185
}
186-
if err := runtime.node.SetRootComponent(root); err != nil {
186+
if err := execution.node.SetRootComponent(root); err != nil {
187187
return chasm.EngineUpdateWithStartExecutionResult{}, err
188188
}
189189
if err := updateFn(mutableCtx, root); err != nil {
190190
return chasm.EngineUpdateWithStartExecutionResult{}, err
191191
}
192-
_, err = runtime.node.CloseTransaction()
192+
_, err = execution.node.CloseTransaction()
193193
if err != nil {
194194
return chasm.EngineUpdateWithStartExecutionResult{}, err
195195
}
196196

197-
runtime.root = root
198-
e.executions[newExecutionLookupKey(runtime.key)] = runtime
197+
execution.root = root
198+
e.executions[newExecutionLookupKey(execution.key)] = execution
199199

200-
serializedRef, err := runtime.node.Ref(root)
200+
serializedRef, err := execution.node.Ref(root)
201201
if err != nil {
202202
return chasm.EngineUpdateWithStartExecutionResult{}, err
203203
}
204204

205205
return chasm.EngineUpdateWithStartExecutionResult{
206-
ExecutionKey: runtime.key,
206+
ExecutionKey: execution.key,
207207
ExecutionRef: serializedRef,
208208
Created: true,
209209
}, nil
@@ -215,11 +215,11 @@ func (e *Engine[T]) UpdateComponent(
215215
updateFn func(chasm.MutableContext, chasm.Component) error,
216216
_ ...chasm.TransitionOption,
217217
) ([]byte, error) {
218-
runtime, err := e.mustExecutionForRef(ref)
218+
execution, err := e.mustExecutionForRef(ref)
219219
if err != nil {
220220
return nil, err
221221
}
222-
return e.updateComponentInRuntime(ctx, runtime, ref, updateFn)
222+
return e.updateComponentInExecution(ctx, execution, ref, updateFn)
223223
}
224224

225225
func (e *Engine[T]) ReadComponent(
@@ -228,17 +228,17 @@ func (e *Engine[T]) ReadComponent(
228228
readFn func(chasm.Context, chasm.Component) error,
229229
_ ...chasm.TransitionOption,
230230
) error {
231-
runtime, err := e.mustExecutionForRef(ref)
231+
execution, err := e.mustExecutionForRef(ref)
232232
if err != nil {
233233
return err
234234
}
235235

236-
component, err := runtime.node.Component(chasm.NewContext(ctx, runtime.node), ref)
236+
component, err := execution.node.Component(chasm.NewContext(ctx, execution.node), ref)
237237
if err != nil {
238238
return err
239239
}
240240

241-
readCtx := chasm.NewContext(ctx, runtime.node)
241+
readCtx := chasm.NewContext(ctx, execution.node)
242242
return readFn(readCtx, component)
243243
}
244244

@@ -261,7 +261,7 @@ func (e *Engine[T]) DeleteExecution(
261261

262262
func (e *Engine[T]) NotifyExecution(chasm.ExecutionKey) {}
263263

264-
func (e *Engine[T]) newRuntime(key chasm.ExecutionKey) *runtime {
264+
func (e *Engine[T]) newExecution(key chasm.ExecutionKey) *execution {
265265
key = normalizeExecutionKey(key)
266266
timeSource := clock.NewEventTimeSource()
267267
timeSource.Update(time.Now())
@@ -279,7 +279,7 @@ func (e *Engine[T]) newRuntime(key chasm.ExecutionKey) *runtime {
279279
}
280280
},
281281
}
282-
return &runtime{
282+
return &execution{
283283
key: key,
284284
backend: backend,
285285
timeSource: timeSource,
@@ -294,38 +294,38 @@ func (e *Engine[T]) newRuntime(key chasm.ExecutionKey) *runtime {
294294
}
295295
}
296296

297-
func (e *Engine[T]) executionForKey(key chasm.ExecutionKey) (*runtime, bool) {
298-
runtime, ok := e.executions[newExecutionLookupKey(normalizeExecutionKey(key))]
299-
return runtime, ok
297+
func (e *Engine[T]) executionForKey(key chasm.ExecutionKey) (*execution, bool) {
298+
execution, ok := e.executions[newExecutionLookupKey(normalizeExecutionKey(key))]
299+
return execution, ok
300300
}
301301

302-
func (e *Engine[T]) mustExecutionForRef(ref chasm.ComponentRef) (*runtime, error) {
303-
runtime, ok := e.executionForKey(ref.ExecutionKey)
302+
func (e *Engine[T]) mustExecutionForRef(ref chasm.ComponentRef) (*execution, error) {
303+
execution, ok := e.executionForKey(ref.ExecutionKey)
304304
if !ok {
305305
return nil, serviceerror.NewNotFound(
306306
fmt.Sprintf("execution not found: namespace=%q business_id=%q run_id=%q", ref.NamespaceID, ref.BusinessID, ref.RunID),
307307
)
308308
}
309-
return runtime, nil
309+
return execution, nil
310310
}
311311

312-
func (e *Engine[T]) updateComponentInRuntime(
312+
func (e *Engine[T]) updateComponentInExecution(
313313
ctx context.Context,
314-
runtime *runtime,
314+
execution *execution,
315315
ref chasm.ComponentRef,
316316
updateFn func(chasm.MutableContext, chasm.Component) error,
317317
) ([]byte, error) {
318-
component, err := runtime.node.Component(chasm.NewContext(ctx, runtime.node), ref)
318+
component, err := execution.node.Component(chasm.NewContext(ctx, execution.node), ref)
319319
if err != nil {
320320
return nil, err
321321
}
322322

323-
mutableCtx := chasm.NewMutableContext(ctx, runtime.node)
323+
mutableCtx := chasm.NewMutableContext(ctx, execution.node)
324324
if err := updateFn(mutableCtx, component); err != nil {
325325
return nil, err
326326
}
327327

328-
_, err = runtime.node.CloseTransaction()
328+
_, err = execution.node.CloseTransaction()
329329
if err != nil {
330330
return nil, err
331331
}

chasm/lib/callback/tasks_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func (m *mockNexusCompletionGetterComponent) LifecycleState(_ chasm.Context) cha
5555
return chasm.LifecycleStateRunning
5656
}
5757

58+
func (m *mockNexusCompletionGetterComponent) ContextMetadata(chasm.Context) map[string]string {
59+
return nil
60+
}
61+
62+
func (m *mockNexusCompletionGetterComponent) Terminate(
63+
chasm.MutableContext,
64+
chasm.TerminateComponentRequest,
65+
) (chasm.TerminateComponentResponse, error) {
66+
return chasm.TerminateComponentResponse{}, nil
67+
}
68+
5869
type mockNexusCompletionGetterLibrary struct {
5970
chasm.UnimplementedLibrary
6071
}

0 commit comments

Comments
 (0)