Skip to content

Commit

Permalink
still run context.WithValue even with orchestrion enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness committed Jul 10, 2024
1 parent 222de37 commit b33d2bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/orchestrion/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func WrapContext(ctx context.Context) context.Context {

// CtxWithValue runs context.WithValue, adds the result to the GLS slot of orchestrion, and returns it.
// If orchestrion is not enabled, it will run context.WithValue and return the result.
// Since we don't support cross-goroutine switch of the GLS we still run context.WithValue in the case
// we are switching goroutines.
func CtxWithValue(parent context.Context, key, val any) context.Context {
if !Enabled() {
return context.WithValue(parent, key, val)
}

getDDContextStack().Push(key, val)
return WrapContext(parent)
return context.WithValue(WrapContext(parent), key, val)
}

// GLSPopValue pops the value from the GLS slot of orchestrion and returns it. Using context.Context values usually does
Expand Down
14 changes: 11 additions & 3 deletions internal/orchestrion/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package orchestrion

import (
"context"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

type key string
Expand Down Expand Up @@ -71,11 +72,18 @@ func TestCtxWithValue(t *testing.T) {
t.Run("true", func(t *testing.T) {
enabled = true
ctx := CtxWithValue(context.Background(), key("key"), "value")
require.Equal(t, &glsContext{context.Background()}, ctx)
require.Equal(t, context.WithValue(&glsContext{context.Background()}, key("key"), "value"), ctx)
require.Equal(t, "value", ctx.Value(key("key")))
require.Equal(t, "value", getDDContextStack().Peek(key("key")))
require.Equal(t, "value", GLSPopValue(key("key")))
require.Nil(t, getDDContextStack().Peek(key("key")))
require.Nil(t, ctx.Value(key("key")))
})

t.Run("cross-goroutine switch", func(t *testing.T) {
enabled = true
ctx := CtxWithValue(context.Background(), key("key"), "value")
go func() {
require.Equal(t, "value", ctx.Value(key("key")))
}()
})
}

0 comments on commit b33d2bf

Please sign in to comment.