-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sync | ||
|
||
import "context" | ||
|
||
type executorKey struct{} | ||
|
||
func ContextExecutor(ctx context.Context) Executor { | ||
executor, ok := ctx.Value(executorKey{}).(Executor) | ||
if !ok { | ||
return sequentialExecutor{} | ||
} | ||
return executor | ||
} | ||
|
||
func SetContextExecutor(ctx context.Context, executor Executor) context.Context { | ||
return context.WithValue(ctx, executorKey{}, executor) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package sync | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContextExecutor(t *testing.T) { | ||
t.Run("WithExecutorInContext", func(t *testing.T) { | ||
executor := NewExecutor(1) | ||
ctx := SetContextExecutor(context.Background(), executor) | ||
|
||
result := ContextExecutor(ctx) | ||
|
||
require.NotNil(t, result) | ||
require.IsType(t, &boundedExecutor{}, result) | ||
}) | ||
|
||
t.Run("WithoutExecutorInContext", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
result := ContextExecutor(ctx) | ||
|
||
require.NotNil(t, result) | ||
require.IsType(t, sequentialExecutor{}, result) | ||
}) | ||
} |