From d7a212813fe1329a44cc1036f94fedd8162f6bc7 Mon Sep 17 00:00:00 2001 From: Roey Berman Date: Thu, 3 Oct 2024 14:35:43 -0700 Subject: [PATCH] Enforce nexus request timeout in workflow test suite (#1653) * Enforce nexus request timeout in workflow test suite * Use GreaterOrEqual to compare timeout --- internal/internal_nexus_task_handler.go | 1 + internal/internal_workflow_testsuite.go | 11 +++++++++++ test/nexus_test.go | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/internal/internal_nexus_task_handler.go b/internal/internal_nexus_task_handler.go index 03e3f5387..7134012e2 100644 --- a/internal/internal_nexus_task_handler.go +++ b/internal/internal_nexus_task_handler.go @@ -336,6 +336,7 @@ func (h *nexusTaskHandler) goContextForTask(nctx *NexusOperationContext, header if err != nil { return nil, nil, nexusHandlerError(nexus.HandlerErrorTypeBadRequest, "cannot parse request timeout") } + ctx, cancel := context.WithTimeout(ctx, timeout) return ctx, cancel, nil } diff --git a/internal/internal_workflow_testsuite.go b/internal/internal_workflow_testsuite.go index 44c36ea38..3b9742f73 100644 --- a/internal/internal_workflow_testsuite.go +++ b/internal/internal_workflow_testsuite.go @@ -2358,6 +2358,17 @@ func (env *testWorkflowEnvironmentImpl) ExecuteNexusOperation( ) int64 { seq := env.nextID() taskHandler := env.newTestNexusTaskHandler() + // Use lower case header values to simulate how the Nexus SDK (used internally by the "real" server) would transmit + // these headers over the wire. + nexusHeader := make(map[string]string, len(params.nexusHeader)) + for k, v := range params.nexusHeader { + nexusHeader[strings.ToLower(k)] = v + } + params.nexusHeader = nexusHeader + // The real server allows requests to take up to 10 seconds, mimic that behavior here. + // Note that if a user sets the Request-Timeout header, it gets overridden. + params.nexusHeader[strings.ToLower(nexus.HeaderRequestTimeout)] = "10s" + handle := &testNexusOperationHandle{ env: env, seq: seq, diff --git a/test/nexus_test.go b/test/nexus_test.go index 83cd1a0f9..b5fbadd36 100644 --- a/test/nexus_test.go +++ b/test/nexus_test.go @@ -735,6 +735,10 @@ func TestReplay(t *testing.T) { func TestWorkflowTestSuite_NexusSyncOperation(t *testing.T) { op := nexus.NewSyncOperation("op", func(ctx context.Context, outcome string, opts nexus.StartOperationOptions) (string, error) { + dealine, ok := ctx.Deadline() + require.True(t, ok) + timeout := time.Until(dealine) + require.GreaterOrEqual(t, 10*time.Second, timeout) require.NotPanicsf(t, func() { temporalnexus.GetMetricsHandler(ctx) temporalnexus.GetLogger(ctx)