-
Notifications
You must be signed in to change notification settings - Fork 632
fix: delete root key returns wrong code #3636
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
Merged
Flo4604
merged 4 commits into
main
from
eng-1942-a-deleted-root-key-should-return-the-correct-error-in-v2
Jul 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,157 @@ | ||
| package cache_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/unkeyed/unkey/go/pkg/cache" | ||
| "github.com/unkeyed/unkey/go/pkg/clock" | ||
| "github.com/unkeyed/unkey/go/pkg/db" | ||
| "github.com/unkeyed/unkey/go/pkg/otel/logging" | ||
| ) | ||
|
|
||
| func TestSWR_CacheHit(t *testing.T) { | ||
| ctx := context.Background() | ||
| mockClock := clock.NewTestClock() | ||
| logger := logging.New() | ||
|
|
||
| c, err := cache.New(cache.Config[string, string]{ | ||
| Fresh: 1 * time.Minute, | ||
| Stale: 5 * time.Minute, | ||
| Logger: logger, | ||
| MaxSize: 100, | ||
| Resource: "test", | ||
| Clock: mockClock, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Run("miss on first call", func(t *testing.T) { | ||
| value, hit, err := c.SWR(ctx, "key1", func(ctx context.Context) (string, error) { | ||
| return "value1", nil | ||
| }, func(err error) cache.Op { | ||
| if err != nil { | ||
| return cache.Noop | ||
| } | ||
| return cache.WriteValue | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "value1", value) | ||
| require.Equal(t, cache.Hit, hit) | ||
| }) | ||
|
|
||
| t.Run("hit on subsequent call within fresh time", func(t *testing.T) { | ||
| // First call to populate cache | ||
| _, _, err := c.SWR(ctx, "key2", func(ctx context.Context) (string, error) { | ||
| return "value2", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Second call should hit cache | ||
| value, hit, err := c.SWR(ctx, "key2", func(ctx context.Context) (string, error) { | ||
| t.Fatal("should not call refresh function") | ||
| return "", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "value2", value) | ||
| require.Equal(t, cache.Hit, hit) | ||
| }) | ||
|
|
||
| t.Run("null cache hit", func(t *testing.T) { | ||
| // First call returns not found error | ||
| _, _, err := c.SWR(ctx, "key3", func(ctx context.Context) (string, error) { | ||
| return "", sql.ErrNoRows | ||
| }, func(err error) cache.Op { | ||
| if db.IsNotFound(err) { | ||
| return cache.WriteNull | ||
| } | ||
| return cache.Noop | ||
| }) | ||
| require.Error(t, err) | ||
| require.True(t, db.IsNotFound(err)) | ||
|
|
||
| // Second call should return null hit | ||
| value, hit, err := c.SWR(ctx, "key3", func(ctx context.Context) (string, error) { | ||
| t.Fatal("should not call refresh function") | ||
| return "", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "", value) | ||
| require.Equal(t, cache.Null, hit) | ||
| }) | ||
|
|
||
| t.Run("stale hit returns cached value", func(t *testing.T) { | ||
| // First call to populate cache | ||
| _, _, err := c.SWR(ctx, "key4", func(ctx context.Context) (string, error) { | ||
| return "value4", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Move time forward past fresh but within stale | ||
| mockClock.Tick(2 * time.Minute) | ||
|
|
||
| // Should return cached value with hit status | ||
| value, hit, err := c.SWR(ctx, "key4", func(ctx context.Context) (string, error) { | ||
| // This will be called in background | ||
| return "updated_value4", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "value4", value) | ||
| require.Equal(t, cache.Hit, hit) | ||
| }) | ||
|
|
||
| t.Run("miss after stale time", func(t *testing.T) { | ||
| // First call to populate cache | ||
| _, _, err := c.SWR(ctx, "key5", func(ctx context.Context) (string, error) { | ||
| return "value5", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Move time forward past stale | ||
| mockClock.Tick(6 * time.Minute) | ||
|
|
||
| // Should call refresh and return new value | ||
| value, hit, err := c.SWR(ctx, "key5", func(ctx context.Context) (string, error) { | ||
| return "new_value5", nil | ||
| }, func(err error) cache.Op { | ||
| return cache.WriteValue | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "new_value5", value) | ||
| require.Equal(t, cache.Hit, hit) | ||
| }) | ||
|
|
||
| t.Run("error returns miss", func(t *testing.T) { | ||
| expectedErr := errors.New("refresh error") | ||
| value, hit, err := c.SWR(ctx, "key6", func(ctx context.Context) (string, error) { | ||
| return "", expectedErr | ||
| }, func(err error) cache.Op { | ||
| return cache.Noop | ||
| }) | ||
|
|
||
| require.Error(t, err) | ||
| require.Equal(t, expectedErr, err) | ||
| require.Equal(t, "", value) | ||
| require.Equal(t, cache.Miss, hit) | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.