forked from allegro/bigcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are posibility we run into a problem of int32 overflow. To prevent this let's use uint64 everywhere. https://github.com/allegro/bigcache/blob/21e5ca5c3d539f94e8dc563350acd97c5400154f/shard.go#L138 Fixes: allegro#148
- Loading branch information
Showing
3 changed files
with
147 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package bigcache | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"runtime" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_issue_148(t *testing.T) { | ||
const n = 2070400 | ||
var message = bytes.Repeat([]byte{0}, 2<<10) | ||
cache, _ := NewBigCache(Config{ | ||
Shards: 1, | ||
LifeWindow: time.Hour, | ||
MaxEntriesInWindow: 10, | ||
MaxEntrySize: len(message), | ||
HardMaxCacheSize: 2 << 13, | ||
}) | ||
for i := 0; i < n; i++ { | ||
err := cache.Set(strconv.Itoa(i), message) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
err := cache.Set(strconv.Itoa(n), message) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cache.Get(strconv.Itoa(n)) | ||
|
||
i := 0 | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Log("Element: ", i) | ||
t.Fatal(r) | ||
} | ||
}() | ||
|
||
for ; i < n; i++ { | ||
v, err := cache.Get(strconv.Itoa(i)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(v, message) { | ||
t.Fatal("Should be equal", i, v, message) | ||
} | ||
} | ||
} | ||
|
||
const ( | ||
entries = 3000 | ||
repeats = 1000 | ||
) | ||
|
||
var valBig = append(make([]byte, 100*1024), 1) | ||
var valMed = append(make([]byte, 1024), 1) | ||
var valSmall = append(make([]byte, 2), 1) | ||
|
||
func getValue() []byte { | ||
x := rand.Float64() | ||
if x < 0.7 { | ||
return valSmall | ||
} else if x < 0.9 { | ||
return valMed | ||
} | ||
return valBig | ||
} | ||
|
||
// https://github.com/allegro/bigcache/issues/234#issuecomment-673895517 | ||
func Test_issue_234(t *testing.T) { | ||
t.Log("Number of entries: ", entries) | ||
printAllocs(t) | ||
rand.Seed(1) | ||
|
||
config := Config{ | ||
Shards: 128, | ||
LifeWindow: time.Hour, | ||
CleanWindow: time.Second, | ||
MaxEntriesInWindow: entries, | ||
MaxEntrySize: 1024, | ||
Verbose: true, | ||
HardMaxCacheSize: 128, | ||
OnRemoveWithReason: func(key string, entry []byte, reason RemoveReason) { | ||
t.Log("Evicted:", len(entry), " reason: ", reason) | ||
}, | ||
} | ||
|
||
bigcache, err := NewBigCache(config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for i := 0; i < repeats; i++ { | ||
printAllocs(t) | ||
for j := 0; j < entries; j++ { | ||
key := strconv.FormatInt(int64(j), 10) | ||
err := bigcache.Set(key, getValue()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func printAllocs(t *testing.T) { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
t.Logf("Alloc: %6d MB \n", m.Alloc/1e6) | ||
} |
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