-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7556 from vmg/vmg/cache-init
cache: handle all possible initialization cases
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/cache/ristretto" | ||
) | ||
|
||
func TestNewDefaultCacheImpl(t *testing.T) { | ||
assertNullCache := func(t *testing.T, cache Cache) { | ||
_, ok := cache.(*nullCache) | ||
require.True(t, ok) | ||
} | ||
|
||
assertLFUCache := func(t *testing.T, cache Cache) { | ||
_, ok := cache.(*ristretto.Cache) | ||
require.True(t, ok) | ||
} | ||
|
||
assertLRUCache := func(t *testing.T, cache Cache) { | ||
_, ok := cache.(*LRUCache) | ||
require.True(t, ok) | ||
} | ||
|
||
tests := []struct { | ||
cfg *Config | ||
verify func(t *testing.T, cache Cache) | ||
}{ | ||
{&Config{MaxEntries: 0, MaxMemoryUsage: 0, LFU: false}, assertNullCache}, | ||
{&Config{MaxEntries: 0, MaxMemoryUsage: 0, LFU: true}, assertNullCache}, | ||
{&Config{MaxEntries: 100, MaxMemoryUsage: 0, LFU: false}, assertLRUCache}, | ||
{&Config{MaxEntries: 0, MaxMemoryUsage: 1000, LFU: false}, assertNullCache}, | ||
{&Config{MaxEntries: 100, MaxMemoryUsage: 1000, LFU: false}, assertLRUCache}, | ||
{&Config{MaxEntries: 100, MaxMemoryUsage: 0, LFU: true}, assertNullCache}, | ||
{&Config{MaxEntries: 100, MaxMemoryUsage: 1000, LFU: true}, assertLFUCache}, | ||
{&Config{MaxEntries: 0, MaxMemoryUsage: 1000, LFU: true}, assertNullCache}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("%d.%d.%v", tt.cfg.MaxEntries, tt.cfg.MaxMemoryUsage, tt.cfg.LFU), func(t *testing.T) { | ||
cache := NewDefaultCacheImpl(tt.cfg) | ||
tt.verify(t, cache) | ||
}) | ||
} | ||
} |