Skip to content

Commit 267784b

Browse files
committed
adding orchestration test
Signed-off-by: salaboy <[email protected]>
1 parent b741cc7 commit 267784b

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

backend/activity.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func (p *activityProcessor) ProcessWorkItem(ctx context.Context, awi *ActivityWo
4949
if ts == nil {
5050
return fmt.Errorf("%v: invalid TaskScheduled event", awi.InstanceID)
5151
}
52-
5352
// Create span as child of spanContext found in TaskScheduledEvent
5453
ctx, err := helpers.ContextFromTraceContext(ctx, ts.ParentTraceContext)
5554
if err != nil {
@@ -66,6 +65,9 @@ func (p *activityProcessor) ProcessWorkItem(ctx context.Context, awi *ActivityWo
6665
}()
6766
}
6867

68+
// set the parent trace context to be the newly created activity span
69+
ts.ParentTraceContext = helpers.TraceContextFromSpan(span)
70+
6971
// Execute the activity and get its result
7072
result, err := p.executor.ExecuteActivity(ctx, awi.InstanceID, awi.NewEvent)
7173
if err != nil {
@@ -75,7 +77,6 @@ func (p *activityProcessor) ProcessWorkItem(ctx context.Context, awi *ActivityWo
7577
}
7678
return err
7779
}
78-
7980
awi.Result = result
8081
return nil
8182
}

tests/orchestrations_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020
"github.com/dapr/durabletask-go/backend/sqlite"
2121
"github.com/dapr/durabletask-go/task"
2222
"github.com/dapr/durabletask-go/tests/utils"
23+
"go.opentelemetry.io/otel"
2324
)
2425

26+
var tracer = otel.Tracer("orchestration-test")
27+
2528
func Test_EmptyOrchestration(t *testing.T) {
2629
// Registration
2730
r := task.NewTaskRegistry()
@@ -210,6 +213,57 @@ func Test_SingleActivity(t *testing.T) {
210213
)
211214
}
212215

216+
func Test_SingleActivity_TaskSpan(t *testing.T) {
217+
// Registration
218+
r := task.NewTaskRegistry()
219+
r.AddOrchestratorN("SingleActivity", func(ctx *task.OrchestrationContext) (any, error) {
220+
var input string
221+
if err := ctx.GetInput(&input); err != nil {
222+
return nil, err
223+
}
224+
var output string
225+
err := ctx.CallActivity("SayHello", task.WithActivityInput(input)).Await(&output)
226+
return output, err
227+
})
228+
r.AddActivityN("SayHello", func(ctx task.ActivityContext) (any, error) {
229+
var name string
230+
if err := ctx.GetInput(&name); err != nil {
231+
return nil, err
232+
}
233+
_, childSpan := tracer.Start(ctx.Context(), "activityChild")
234+
childSpan.End()
235+
return fmt.Sprintf("Hello, %s!", name), nil
236+
})
237+
238+
// Initialization
239+
ctx := context.Background()
240+
exporter := utils.InitTracing()
241+
242+
client, worker := initTaskHubWorker(ctx, r)
243+
defer worker.Shutdown(ctx)
244+
245+
// Run the orchestration
246+
id, err := client.ScheduleNewOrchestration(ctx, "SingleActivity", api.WithInput("世界"))
247+
if assert.NoError(t, err) {
248+
metadata, err := client.WaitForOrchestrationCompletion(ctx, id)
249+
if assert.NoError(t, err) {
250+
assert.Equal(t, protos.OrchestrationStatus_ORCHESTRATION_STATUS_COMPLETED, metadata.RuntimeStatus)
251+
assert.Equal(t, `"Hello, 世界!"`, metadata.Output.Value)
252+
}
253+
}
254+
255+
// Validate the exported OTel traces
256+
spans := exporter.GetSpans().Snapshots()
257+
utils.AssertSpanSequence(t, spans,
258+
utils.AssertOrchestratorCreated("SingleActivity", id),
259+
utils.AssertSpan("activityChild"),
260+
utils.AssertActivity("SayHello", id, 0),
261+
utils.AssertOrchestratorExecuted("SingleActivity", id, "COMPLETED"),
262+
)
263+
// assert child-parent relationship
264+
assert.Equal(t, spans[1].Parent().SpanID(), spans[2].SpanContext().SpanID())
265+
}
266+
213267
func Test_ActivityChain(t *testing.T) {
214268
// Registration
215269
r := task.NewTaskRegistry()

0 commit comments

Comments
 (0)