-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
16 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
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
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
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
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,88 @@ | ||
package snp | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
"time" | ||
|
||
"github.com/edgelesssys/nunki/internal/memstore" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/goleak" | ||
testingclock "k8s.io/utils/clock/testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} | ||
|
||
func TestCachedKDSHTTPClient(t *testing.T) { | ||
t.Run("Get", func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
fakeGetter := &fakeHTTPSGetter{ | ||
content: map[string][]byte{ | ||
"foo": []byte("bar"), | ||
}, | ||
hits: map[string]int{}, | ||
} | ||
stepTime := 5 * time.Minute | ||
testClock := testingclock.NewFakeClock(time.Now()) | ||
ticker := testClock.NewTicker(stepTime) | ||
client := &cachedKDSHTTPClient{ | ||
HTTPSGetter: fakeGetter, | ||
gcTicker: ticker, | ||
cache: memstore.New[string, []byte](), | ||
logger: slog.Default(), | ||
} | ||
|
||
res, err := client.Get("foo") | ||
assert.NoError(err) | ||
assert.Equal([]byte("bar"), res) | ||
assert.Equal(1, fakeGetter.hits["foo"]) | ||
|
||
// Expect a second call to return the cached value and not increase the hit counter. | ||
res, err = client.Get("foo") | ||
assert.NoError(err) | ||
assert.Equal([]byte("bar"), res) | ||
assert.Equal(1, fakeGetter.hits["foo"]) | ||
|
||
// After the step time, the cache should be invalidated and hit the backend again. | ||
testClock.Step(stepTime) | ||
res, err = client.Get("foo") | ||
assert.NoError(err) | ||
assert.Equal([]byte("bar"), res) | ||
assert.Equal(2, fakeGetter.hits["foo"]) | ||
}) | ||
t.Run("Get error", func(t *testing.T) { | ||
fakeGetter := &fakeHTTPSGetter{ | ||
getErr: assert.AnError, | ||
content: map[string][]byte{}, | ||
hits: map[string]int{}, | ||
} | ||
testClock := testingclock.NewFakeClock(time.Now()) | ||
ticker := testClock.NewTicker(5 * time.Minute) | ||
client := &cachedKDSHTTPClient{ | ||
HTTPSGetter: fakeGetter, | ||
gcTicker: ticker, | ||
cache: memstore.New[string, []byte](), | ||
logger: slog.Default(), | ||
} | ||
|
||
assert := assert.New(t) | ||
|
||
_, err := client.Get("foo") | ||
assert.Error(err) | ||
assert.Equal(1, fakeGetter.hits["foo"]) | ||
}) | ||
} | ||
|
||
type fakeHTTPSGetter struct { | ||
content map[string][]byte | ||
hits map[string]int | ||
getErr error | ||
} | ||
|
||
func (f *fakeHTTPSGetter) Get(url string) ([]byte, error) { | ||
f.hits[url]++ | ||
return f.content[url], f.getErr | ||
} |
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
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