forked from hashicorp/nomad
-
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.
Merge pull request hashicorp#227 from superfell/fuzz-v2-one
Add fuzztest testsuite
- Loading branch information
Showing
13 changed files
with
1,510 additions
and
2 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
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,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) | ||
} |
Oops, something went wrong.