Skip to content

Commit

Permalink
Merge pull request hashicorp#227 from superfell/fuzz-v2-one
Browse files Browse the repository at this point in the history
Add fuzztest testsuite
  • Loading branch information
slackpad authored Aug 23, 2017
2 parents 3a6f3bd + 57b885f commit 29ea0ff
Show file tree
Hide file tree
Showing 13 changed files with 1,510 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
DEPS = $(go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)

test:
go test -timeout=60s ./...
go test -timeout=60s .

integ: test
INTEG_TESTS=yes go test -timeout=25s -run=Integ ./...
INTEG_TESTS=yes go test -timeout=25s -run=Integ .

fuzz:
go test -timeout=300s ./fuzzy

deps:
go get -d -v ./...
echo $(DEPS) | xargs -n1 go get -d
Expand Down
65 changes: 65 additions & 0 deletions fuzzy/apply_src.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fuzzy

import (
"hash/fnv"
"math/rand"
"testing"
"time"
)

type applySource struct {
rnd *rand.Rand
seed int64
}

// newApplySource will create a new source, any source created with the same seed will generate the same sequence of data.
func newApplySource(seed string) *applySource {
h := fnv.New32()
h.Write([]byte(seed))
s := &applySource{seed: int64(h.Sum32())}
s.reset()
return s
}

// reset this source back to its initial state, it'll generate the same sequence of data it initally did
func (a *applySource) reset() {
a.rnd = rand.New(rand.NewSource(a.seed))
}

func (a *applySource) nextEntry() []byte {
const sz = 33
r := make([]byte, sz)
for i := 0; i < len(r); i++ {
r[i] = byte(a.rnd.Int31n(256))
}
return r
}

type clusterApplier struct {
stopCh chan bool
applied uint64
src *applySource
}

// runs apply in chunks of n to the cluster, use the returned Applier to Stop() it
func (a *applySource) apply(t *testing.T, c *cluster, n uint) *clusterApplier {
ap := &clusterApplier{stopCh: make(chan bool), src: a}
go ap.apply(t, c, n)
return ap
}

func (ca *clusterApplier) apply(t *testing.T, c *cluster, n uint) {
for true {
select {
case <-ca.stopCh:
return
default:
ca.applied += c.ApplyN(t, time.Second, ca.src, n)
}
}
}

func (ca *clusterApplier) stop() {
ca.stopCh <- true
close(ca.stopCh)
}
Loading

0 comments on commit 29ea0ff

Please sign in to comment.