temporal: when testing, how do I pass context into workflows and activities?

huangapple go评论74阅读模式
英文:

temporal: when testing, how do I pass context into workflows and activities?

问题

我正在为一个时间工作流编写集成测试,并使用golang sdk。我的工作流包含以下代码的一个活动:

   stackEnv, ok := ctx.Value("stackEnv").(*StackEnv)
	if !ok {
		return nil, errors.New("stackEnv not configured")
	}

在我的测试中,我创建了一个TestWorkflowEnvironment,并使用s.env.ExecuteWorkflow(EntityCreateWorkflow, wfParams)运行我的工作流。然而,在我的测试中,我得到一个错误,即stackEnv not configured,因为在测试环境中活动运行的context.Context上没有设置stackEnv。如何在TestWorkflowEnvironment中指定自定义上下文?

英文:

I'm writing an integration test for a temporal workflow and using the golang sdk. My workflow has an activity which includes the following code:

   stackEnv, ok := ctx.Value("stackEnv").(*StackEnv)
	if !ok {
		return nil, errors.New("stackEnv not configured")
	}

In my test I create a TestWorkflowEnvironment and run my workflow with s.env.ExecuteWorkflow(EntityCreateWorkflow, wfParams). However I get an error in my test that stackEnv not configured because the context.Context the activity is running with in the test environment does not have stackEnv set on it. How do I specify a custom context to my TestWorkflowEnvironment?

答案1

得分: 2

使用TestWorkflowEnvironment.SetWorkerOptions() API。

在生产代码中,上下文参数是在执行工作流的工作人员上指定的,例如:

    ctx := context.WithValue(context.Background(), "stackEnv", stackEnv)

    worker.New(stackEnv.Temporal, string(taskQueueName), worker.Options{
		BackgroundActivityContext: ctx,
	})

WorkflowTestEnvironment中,模拟了一个工作人员。我们可以使用SetWorkerOptions()来指定选项,例如:

    ctx := context.WithValue(context.Background(), "stackEnv", StackEnv)

	s.env.SetWorkerOptions(worker.Options{
		BackgroundActivityContext: ctx,
	})

(其中s.env是Temporal的testsuite.TestWorkflowEnvironment

英文:

Use the TestWorkflowEnvironment.SetWorkerOptions() API.

In production code, the context parameter is specified on the worker that executes workflows, e.g.:

    ctx := context.WithValue(context.Background(), "stackEnv", stackEnv)

    worker.New(stackEnv.Temporal, string(taskQueueName), worker.Options{
		BackgroundActivityContext: ctx,
	})

In the WorkflowTestEnvironment, a worker is simulated. We can specify options to it with SetWorkerOptions(), e.g.:

    ctx := context.WithValue(context.Background(), "stackEnv", StackEnv)

	s.env.SetWorkerOptions(worker.Options{
		BackgroundActivityContext: ctx,
	})

(where s.env is the temporal testsuite.TestWorkflowEnvironment)

答案2

得分: 2

帕特里克的回答是完全正确的。

我还建议将活动实现为结构的成员。这样,将依赖项传递给它们就不需要处理上下文。

请参考使用这种技术的greetings示例

英文:

Patrick's answer is absolutely correct.

I would also recommend looking into implementing activities as members of a structure. This way passing dependencies to them doesn't require messing with context.

See the greetings sample that uses this technique.

huangapple
  • 本文由 发表于 2021年10月15日 05:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/69577516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定