Skip to content

Commit

Permalink
test: add test for cache and SafeKey
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-ja committed Dec 26, 2023
1 parent 4e074b1 commit 5d4aa62
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 32 deletions.
224 changes: 192 additions & 32 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

func TestInMemory(t *testing.T) {
type args[T any] struct {
ttl time.Duration
fn func(context.Context) (T, error)
forceRefresh bool
ttl time.Duration
fn func(context.Context) (T, error)
options []Option
}
type testCase[T any] struct {
name string
Expand All @@ -34,7 +34,7 @@ func TestInMemory(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 200,
time.Second,
Expand All @@ -49,7 +49,7 @@ func TestInMemory(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Second * 150,
Expand All @@ -64,7 +64,7 @@ func TestInMemory(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Millisecond * 150,
Expand All @@ -79,10 +79,25 @@ func TestInMemory(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: true,
options: []Option{WithForceRefresh()},
},
time.Millisecond * 150,
time.Millisecond * 250,
time.Millisecond * 300,
time.Millisecond * 350,
"test",
false,
},
{
"reset ttl",
args[string]{
ttl: time.Millisecond * 15,
fn: func(_ context.Context) (string, error) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
options: []Option{WithRefreshTTL()},
},
time.Millisecond * 100,
time.Millisecond * 120,
"test",
false,
},
Expand All @@ -94,10 +109,10 @@ func TestInMemory(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "", errors.New("failed")
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Millisecond * 250,
time.Millisecond * 300,
time.Millisecond * 350,
"",
true,
},
Expand All @@ -107,13 +122,25 @@ func TestInMemory(t *testing.T) {
start := time.Now()
fn := InMemory(tt.args.ttl, tt.args.fn)

ctx := context.Background()
// call it once to warm up the cache
_, _ = fn(context.Background(), tt.args.forceRefresh)
time.Sleep(time.Millisecond * 2)
_, _ = fn(ctx, tt.args.options...)
time.Sleep(time.Millisecond * 10)

got, err := fn(context.Background(), tt.args.forceRefresh)
// call it a second time to test the basic cache mechanisms
got, err := fn(ctx, tt.args.options...)
if got != tt.want {
t.Errorf("InMemory() = %v, want = %v", got, tt.wantErr)
t.Errorf("InMemory() = %v, want = %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("InMemory() err = %v, wantErr = %v", err, tt.wantErr)
}
time.Sleep(time.Microsecond * 10)

// call it a third time to test the ttl reset mechanism
got, err = fn(ctx, tt.args.options...)
if got != tt.want {
t.Errorf("InMemory() = %v, want = %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("InMemory() err = %v, wantErr = %v", err, tt.wantErr)
Expand All @@ -133,10 +160,10 @@ func TestInMemory(t *testing.T) {

func TestOnDisk(t *testing.T) {
type args[T any] struct {
file string
ttl time.Duration
fn func(context.Context) (T, error)
forceRefresh bool
file string
ttl time.Duration
fn func(context.Context) (T, error)
options []Option
}
type testCase[T any] struct {
name string
Expand All @@ -158,7 +185,7 @@ func TestOnDisk(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 200,
time.Second,
Expand All @@ -176,7 +203,7 @@ func TestOnDisk(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Second * 150,
Expand All @@ -194,7 +221,7 @@ func TestOnDisk(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Millisecond * 150,
Expand All @@ -212,10 +239,28 @@ func TestOnDisk(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
forceRefresh: true,
options: []Option{WithForceRefresh()},
},
time.Millisecond * 150,
time.Millisecond * 250,
time.Millisecond * 300,
time.Millisecond * 350,
"test",
false,
false,
`"test"`,
},
{
"reset ttl",
args[string]{
file: filepath.Join(t.TempDir(), "test"),
ttl: time.Millisecond * 15,
fn: func(_ context.Context) (string, error) {
time.Sleep(time.Millisecond * 100)
return "test", nil
},
options: []Option{WithRefreshTTL()},
},
time.Millisecond * 100,
time.Millisecond * 130,
"test",
false,
false,
Expand All @@ -230,10 +275,10 @@ func TestOnDisk(t *testing.T) {
time.Sleep(time.Millisecond * 100)
return "", errors.New("failed")
},
forceRefresh: false,
options: []Option{},
},
time.Millisecond * 100,
time.Millisecond * 250,
time.Millisecond * 300,
time.Millisecond * 350,
"",
true,
true,
Expand All @@ -246,10 +291,24 @@ func TestOnDisk(t *testing.T) {
fn := OnDisk(tt.args.file, tt.args.ttl, tt.args.fn)

// call it once to warm up the cache
_, _, _ = fn(context.Background(), tt.args.forceRefresh)
time.Sleep(time.Millisecond * 2)
_, _, _ = fn(context.Background(), tt.args.options...)
time.Sleep(time.Millisecond * 10)

got, cacheErr, err := fn(context.Background(), tt.args.forceRefresh)
// call it a second time to test the basic cache mechanisms
got, cacheErr, err := fn(context.Background(), tt.args.options...)
if got != tt.want {
t.Errorf("OnDisk() = %v, want = %v", got, tt.wantErr)
}
if (err != nil) != tt.wantErr {
t.Errorf("OnDisk() err = %v, wantErr = %v", err, tt.wantErr)
}
if (cacheErr != nil) != tt.wantCacheErr {
t.Errorf("OnDisk() err = %v, wantErr = %v", cacheErr, tt.wantCacheErr)
}
time.Sleep(time.Millisecond * 10)

// call it a third time to test the ttl reset mechanism
got, cacheErr, err = fn(context.Background(), tt.args.options...)
if got != tt.want {
t.Errorf("OnDisk() = %v, want = %v", got, tt.wantErr)
}
Expand Down Expand Up @@ -283,3 +342,104 @@ func TestOnDisk(t *testing.T) {
})
}
}

func TestSkipErr(t *testing.T) {
type args struct {
fn func(context.Context, ...Option) (string, error, error)
}
tests := []struct {
name string
args args
wantString string
wantError bool
}{
{
"skip cache error",
args{
fn: func(_ context.Context, _ ...Option) (string, error, error) {
return "success", errors.New("there was a cache error"), nil
},
},
"success",
false,
},
{
"return error",
args{
fn: func(_ context.Context, _ ...Option) (string, error, error) {
return "", nil, errors.New("there was a value error")
},
},
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fn := SkipErr(tt.args.fn)

got, err := fn(context.Background())
if (err != nil) != tt.wantError {
t.Errorf("SkipErr() err = %v, got = %v", err, tt.wantError)
}
if got != tt.wantString {
t.Errorf("SkipErr() = %s, got = %s", got, tt.wantString)
}
})
}
}

func TestLogErr(t *testing.T) {
type args struct {
fn func(context.Context, ...Option) (string, error, error)
}
tests := []struct {
name string
args args
wantString string
wantLog string
wantError bool
}{
{
"skip cache error",
args{
fn: func(_ context.Context, _ ...Option) (string, error, error) {
return "success", errors.New("there was a cache error"), nil
},
},
"success",
"there was a cache error",
false,
},
{
"return error",
args{
fn: func(_ context.Context, _ ...Option) (string, error, error) {
return "", nil, errors.New("there was a value error")
},
},
"",
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotLog string
fn := LogErr(tt.args.fn, func(_ context.Context, err error) {
gotLog = err.Error()
})

got, err := fn(context.Background())
if (err != nil) != tt.wantError {
t.Errorf("LogErr() err = %v, got = %v", err, tt.wantError)
}
if got != tt.wantString {
t.Errorf("LogErr() = %s, got = %s", got, tt.wantString)
}
if gotLog != tt.wantLog {
t.Errorf("LogErr() log = %s, got = %s", gotLog, tt.wantLog)
}
})
}
}
36 changes: 36 additions & 0 deletions persist/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package persist

import "testing"

func TestSafeKey(t *testing.T) {
type args struct {
key string
}
tests := []struct {
name string
args args
want string
}{
{
"empty key",
args{
key: "",
},
"",
},
{
"replace key",
args{
key: "key123",
},
"a2V5MTIz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeKey(tt.args.key); got != tt.want {
t.Errorf("SafeKey() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5d4aa62

Please sign in to comment.