From 9e6741e7f3e6db2acbaf3ed104c2de2680bcb198 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sat, 16 Feb 2019 14:37:31 +0100 Subject: [PATCH 1/3] add a fuzzer --- fuzztest/main.go | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 fuzztest/main.go diff --git a/fuzztest/main.go b/fuzztest/main.go new file mode 100644 index 00000000..8f30f04c --- /dev/null +++ b/fuzztest/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "bytes" + "context" + "fmt" + "github.com/allegro/bigcache" + "math/rand" + "os" + "os/signal" + "sync" + "syscall" + "time" +) + +func fuzzDeletePutGet(ctx context.Context) { + c := bigcache.DefaultConfig(time.Second) + c.Shards = 1 + c.MaxEntriesInWindow = 10 + c.MaxEntriesInWindow = 10 + c.HardMaxCacheSize = 1 + + cache, _ := bigcache.NewBigCache(c) + var wg sync.WaitGroup + + // Deleter + wg.Add(1) + go func() { + for { + select { + case <-ctx.Done(): + break + default: + r := uint8(rand.Int()) + key := fmt.Sprintf("thekey%d", r) + cache.Delete(key) + } + } + wg.Done() + }() + + // Setter + wg.Add(1) + go func() { + val := make([]byte, 1024) + for { + select { + case <-ctx.Done(): + break + default: + r := byte(rand.Int()) + key := fmt.Sprintf("thekey%d", r) + + for j := 0; j < len(val); j++ { + val[j] = r + } + cache.Set(key, val) + } + } + wg.Done() + }() + + // Getter + wg.Add(1) + go func() { + var ( + val = make([]byte, 1024) + hits = uint64(0) + misses = uint64(0) + ) + for { + select { + case <-ctx.Done(): + break + default: + r := byte(rand.Int()) + key := fmt.Sprintf("thekey%d", r) + + for j := 0; j < len(val); j++ { + val[j] = r + } + if got, err := cache.Get(key); err == nil && !bytes.Equal(got, val) { + errStr := fmt.Sprintf("got %s ->\n %x\n expected:\n %x\n ", key, got, val) + panic(errStr) + } else { + if err == nil { + misses++ + } else { + hits++ + } + } + if total := hits + misses; total%1000000 == 0 { + percentage := float64(100) * float64(hits) / float64(total) + fmt.Printf("Hits %d (%.2f%%) misses %d \n", hits, percentage, misses) + } + } + } + wg.Done() + }() + wg.Wait() + +} +func main() { + + sigs := make(chan os.Signal, 1) + ctx, cancel := context.WithCancel(context.Background()) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + fmt.Println("Press ctrl-c to exit") + go fuzzDeletePutGet(ctx) + + <-sigs + fmt.Println("Exiting...") + cancel() + +} From 8295df22446239f119e7e23d6840c720807f7141 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sat, 16 Feb 2019 15:30:15 +0100 Subject: [PATCH 2/3] fuzztest: correct stats --- fuzztest/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuzztest/main.go b/fuzztest/main.go index 8f30f04c..c477cb2e 100644 --- a/fuzztest/main.go +++ b/fuzztest/main.go @@ -84,9 +84,9 @@ func fuzzDeletePutGet(ctx context.Context) { panic(errStr) } else { if err == nil { - misses++ - } else { hits++ + } else { + misses++ } } if total := hits + misses; total%1000000 == 0 { From d5bc869573def9fcc5706de5760ea726246f232d Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sat, 16 Feb 2019 21:49:13 +0100 Subject: [PATCH 3/3] fix unreachable code flaw --- fuzztest/main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fuzztest/main.go b/fuzztest/main.go index c477cb2e..f8acdfed 100644 --- a/fuzztest/main.go +++ b/fuzztest/main.go @@ -26,27 +26,28 @@ func fuzzDeletePutGet(ctx context.Context) { // Deleter wg.Add(1) go func() { + defer wg.Done() for { select { case <-ctx.Done(): - break + return default: r := uint8(rand.Int()) key := fmt.Sprintf("thekey%d", r) cache.Delete(key) } } - wg.Done() }() // Setter wg.Add(1) go func() { + defer wg.Done() val := make([]byte, 1024) for { select { case <-ctx.Done(): - break + return default: r := byte(rand.Int()) key := fmt.Sprintf("thekey%d", r) @@ -57,12 +58,12 @@ func fuzzDeletePutGet(ctx context.Context) { cache.Set(key, val) } } - wg.Done() }() // Getter wg.Add(1) go func() { + defer wg.Done() var ( val = make([]byte, 1024) hits = uint64(0) @@ -71,7 +72,7 @@ func fuzzDeletePutGet(ctx context.Context) { for { select { case <-ctx.Done(): - break + return default: r := byte(rand.Int()) key := fmt.Sprintf("thekey%d", r) @@ -95,7 +96,6 @@ func fuzzDeletePutGet(ctx context.Context) { } } } - wg.Done() }() wg.Wait()