File: apply_src.go

package info (click to toggle)
golang-github-hashicorp-raft 1.0.0%2Bgit20180823.82694fb-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 704 kB
  • sloc: makefile: 17; sh: 9
file content (65 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 initially 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)
}