diff --git a/bigcache.go b/bigcache.go index c4439c1a..7ae1f124 100644 --- a/bigcache.go +++ b/bigcache.go @@ -46,10 +46,18 @@ func NewBigCache(config Config) (*BigCache, error) { } func newBigCache(config Config, clock clock) (*BigCache, error) { - if !isPowerOfTwo(config.Shards) { return nil, fmt.Errorf("Shards number must be power of two") } + if config.MaxEntrySize < 0 { + return nil, fmt.Errorf("MaxEntrySize must be >= 0") + } + if config.MaxEntriesInWindow < 0 { + return nil, fmt.Errorf("MaxEntriesInWindow must be >= 0") + } + if config.HardMaxCacheSize < 0 { + return nil, fmt.Errorf("HardMaxCacheSize must be >= 0") + } if config.Hasher == nil { config.Hasher = newDefaultHasher() diff --git a/bigcache_test.go b/bigcache_test.go index b62a5a99..f4749d16 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -156,19 +156,37 @@ func TestConstructCacheWithDefaultHasher(t *testing.T) { assertEqual(t, true, ok) } -func TestWillReturnErrorOnInvalidNumberOfPartitions(t *testing.T) { +func TestNewBigcacheValidation(t *testing.T) { t.Parallel() - // given - cache, error := NewBigCache(Config{ - Shards: 18, - LifeWindow: 5 * time.Second, - MaxEntriesInWindow: 10, - MaxEntrySize: 256, - }) - - assertEqual(t, (*BigCache)(nil), cache) - assertEqual(t, "Shards number must be power of two", error.Error()) + for _, tc := range []struct { + cfg Config + want string + }{ + { + cfg: Config{Shards: 18}, + want: "Shards number must be power of two", + }, + { + cfg: Config{Shards: 16, MaxEntriesInWindow: -1}, + want: "MaxEntriesInWindow must be >= 0", + }, + { + cfg: Config{Shards: 16, MaxEntrySize: -1}, + want: "MaxEntrySize must be >= 0", + }, + { + cfg: Config{Shards: 16, HardMaxCacheSize: -1}, + want: "HardMaxCacheSize must be >= 0", + }, + } { + t.Run(tc.want, func(t *testing.T) { + cache, error := NewBigCache(tc.cfg) + + assertEqual(t, (*BigCache)(nil), cache) + assertEqual(t, tc.want, error.Error()) + }) + } } func TestEntryNotFound(t *testing.T) {