-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type safe stashing of context values #546
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ func TestStash(t *testing.T) { | |
} | ||
|
||
var key StashKey = "stash-key" | ||
ctx := WithStash(context.TODO()) | ||
ctx := WithStash(context.Background()) | ||
for _, c := range tests { | ||
t.Run(c.name, func(t *testing.T) { | ||
StashValue(ctx, key, c.value) | ||
|
@@ -59,7 +59,7 @@ func TestStash(t *testing.T) { | |
} | ||
|
||
func TestStash_StashValue_UndecoratedContext(t *testing.T) { | ||
ctx := context.TODO() | ||
ctx := context.Background() | ||
var key StashKey = "stash-key" | ||
value := "value" | ||
|
||
|
@@ -72,7 +72,7 @@ func TestStash_StashValue_UndecoratedContext(t *testing.T) { | |
} | ||
|
||
func TestStash_RetrieveValue_UndecoratedContext(t *testing.T) { | ||
ctx := context.TODO() | ||
ctx := context.Background() | ||
var key StashKey = "stash-key" | ||
|
||
defer func() { | ||
|
@@ -84,10 +84,87 @@ func TestStash_RetrieveValue_UndecoratedContext(t *testing.T) { | |
} | ||
|
||
func TestStash_RetrieveValue_Undefined(t *testing.T) { | ||
ctx := WithStash(context.TODO()) | ||
ctx := WithStash(context.Background()) | ||
var key StashKey = "stash-key" | ||
|
||
if value := RetrieveValue(ctx, key); value != nil { | ||
t.Error("expected RetrieveValue() to return nil for undefined key") | ||
} | ||
} | ||
|
||
func TestStasher(t *testing.T) { | ||
ctx := WithStash(context.Background()) | ||
stasher := NewStasher[string]("my-key") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit]: I think testing against a stashed value of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All types in go have a default empty value. There's a use of a vanilla struct here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get it. ty |
||
|
||
if key := stasher.Key(); key != StashKey("my-key") { | ||
t.Errorf("expected key to be %q got %q", StashKey("my-key"), key) | ||
} | ||
|
||
t.Run("no value", func(t *testing.T) { | ||
t.Run("RetrieveOrEmpty", func(t *testing.T) { | ||
if value := stasher.RetrieveOrEmpty(ctx); value != "" { | ||
t.Error("expected value to be empty") | ||
} | ||
}) | ||
t.Run("RetrieveOrError", func(t *testing.T) { | ||
if value, err := stasher.RetrieveOrError(ctx); err == nil { | ||
t.Error("expected err") | ||
} else if value != "" { | ||
t.Error("expected value to be empty") | ||
} | ||
}) | ||
t.Run("RetrieveOrDie", func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
return | ||
} | ||
t.Errorf("expected to recover") | ||
}() | ||
value := stasher.RetrieveOrDie(ctx) | ||
t.Errorf("expected to panic, got %q", value) | ||
}) | ||
}) | ||
|
||
t.Run("has value", func(t *testing.T) { | ||
stasher.Store(ctx, "hello world") | ||
t.Run("RetrieveOrEmpty", func(t *testing.T) { | ||
if value := stasher.RetrieveOrEmpty(ctx); value != "hello world" { | ||
t.Errorf("expected value to be %q got %q", "hello world", value) | ||
} | ||
}) | ||
t.Run("RetrieveOrError", func(t *testing.T) { | ||
if value, err := stasher.RetrieveOrError(ctx); err != nil { | ||
t.Errorf("unexpected err: %s", err) | ||
} else if value != "hello world" { | ||
t.Errorf("expected value to be %q got %q", "hello world", value) | ||
} | ||
}) | ||
t.Run("RetrieveOrDie", func(t *testing.T) { | ||
if value := stasher.RetrieveOrDie(ctx); value != "hello world" { | ||
t.Errorf("expected value to be %q got %q", "hello world", value) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("context scoped", func(t *testing.T) { | ||
stasher.Store(ctx, "hello world") | ||
if value := stasher.RetrieveOrEmpty(ctx); value != "hello world" { | ||
t.Errorf("expected value to be %q got %q", "hello world", value) | ||
} | ||
altCtx := WithStash(context.Background()) | ||
if value := stasher.RetrieveOrEmpty(altCtx); value != "" { | ||
t.Error("expected value to be empty") | ||
} | ||
}) | ||
|
||
t.Run("Clear", func(t *testing.T) { | ||
stasher.Store(ctx, "hello world") | ||
if value := stasher.RetrieveOrEmpty(ctx); value != "hello world" { | ||
t.Errorf("expected value to be %q got %q", "hello world", value) | ||
} | ||
stasher.Clear(ctx) | ||
if value := stasher.RetrieveOrEmpty(ctx); value != "" { | ||
t.Error("expected value to be empty") | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[thought]: Store is a good name. However, intuitively I was expecting it to be Stash. That's in the spirit of Stringer, Writer, Reader etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, the first draft used
Stash
here. I changed it because it seemed to push too much emphasis on stashing while retrieving is just as (or more) important. Stringer, Writer and Reader have a directed purpose, a Stasher is about a round trip.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me @scothis! ty