How to Build workflow.Context to execute activity

how to build ctx?

ao := workflow.ActivityOptions{
        TaskQueue:               "sampleTaskQueue",
        ScheduleToCloseTimeout: time.Second * 60,
        ScheduleToStartTimeout: time.Second * 60,
        StartToCloseTimeout:    time.Second * 60,
        HeartbeatTimeout:       time.Second * 10,
        WaitForCancellation:    false,
}
ctx = workflow.WithActivityOptions(ctx, ao)

var result string
err := workflow.ExecuteActivity(ctx, SimpleActivity, value).Get(ctx, &result)
if err != nil {
        return err
}

I don’t understand the question. Your sample code is fine.

the context in internal package, I need to create a workflow.Context to execute func

You don’t need to create workflow context explicitly. Workflow function is invoked by the worker when a workflow is executed. For starting workflow the standard context.Context is used.

We have some code in interceptors that are reading header values from workflow.Context and converting to our internal.Metadata in our internal key.

We can’t unit test them due to the unique nature of internal.Context creation, is it possible to request a public workflow.Context mock added in the Mocks?

I can’t think of too many use cases outside of Interceptors but since workflow.Context’s are exposed, in theory, should be something we should be able to UnitTest against if we are allowed to use them in our code.

Again it’s real small potato stuff, so we moved on.

Have you tried setting your interceptor through WorkerOptions?

testWorkflowEnvironment.setWorkerOptions(WorkerOptions {
    WorkflowInterceptorChainFactories: <your interceptors>,
})
1 Like

Interceptors on my to do list, apologies for making it confusing.

We have code in the interceptors we are considering UnitTesting (it involves tracing/logging value propagation).

// ExecuteActivity middleware.
func (t *ContextOutboundCallsInterceptor) ExecuteActivity(
    ctx workflow.Context,
    activityType string,
    args ...interface{}) workflow.Future {

    rs := tcontext.GetNonSharedRootSpanFromTemporalContext(&ctx, t.Logger, activityType+"_Interceptor")

    defer rs.End()

    // before execution
    result := t.Next.ExecuteActivity(ctx, activityType, args...)

    // after execution
    return result
}

tcontext.GetNonSharedRootSpanFromTemporalContext is the target of our UnitTest.

I am working around some design choices by our Logging authors in that they mutate context to ensure that values extracted initially from Context are all synchronized back inside the Context after creation (in case they were missing and default values were created).

So you can use the testWorkflowEnvironment to execute this interceptor with a proper context. You can use some fake workflow function for this.

We could do it that way sure, thanks :slight_smile:

In a similar situation, want to unit test a function with ctx workflow.Context in signature. Is there a way to build/mock internal.context just to do the unit testing without init worker/execute workflow?

To test functions that accept workflow.Context use TestWorkflowEnvironment.

Getting error: TaskState(Activity2) Error: A valid StartToClose or ScheduleToCl…+33 more, How do I specify activity option for testworkflowenvironment?

ao := workflow.ActivityOptions{
		ScheduleToStartTimeout: time.Minute,
		StartToCloseTimeout:    time.Minute,
}

never mind, I figured that out.

1 Like