-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
generation.go
67 lines (57 loc) · 1.78 KB
/
generation.go
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
66
67
package testutils
import (
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"math/rand"
"net/url"
"github.com/ironsmile/nedomi/types"
)
// GenerateMeAString generates a pseudo-randomized string of the provided size, with the
func GenerateMeAString(seed, size int64) string {
var b = make([]byte, size/2+size%2) // hex encoding produce twice the amount of bytes it consumes
r := readerFromSource(rand.NewSource(seed))
if _, err := r.Read(b); err != nil {
panic(err)
}
return hex.EncodeToString(b)[:size] // odd cases
}
type sourceReader struct {
rand.Source
}
func (s *sourceReader) Read(b []byte) (int, error) {
l := len(b)
var buf [8]byte
for a := 0; l-1 > a; a += 8 {
binary.LittleEndian.PutUint64(buf[:], uint64(s.Int63()))
copy(b[a:], buf[:])
}
return l, nil
}
func readerFromSource(s rand.Source) io.Reader {
return &sourceReader{s}
}
// GetUpstream returns a fully configured upstream address
func GetUpstream(i int) *types.UpstreamAddress {
return &types.UpstreamAddress{
URL: url.URL{Host: fmt.Sprintf("127.0.%d.%d", (i/256)%256, i%256), Scheme: "http"},
Hostname: fmt.Sprintf("www.upstream%d.com", i),
Port: "80",
OriginalURL: &url.URL{Host: fmt.Sprintf("www.upstream%d.com", i), Scheme: "http"},
Weight: 100 + uint32(rand.Intn(500)),
}
}
// GetUpstreams returns a fully configured slice of sequential upstream address
func GetUpstreams(from, to int) []*types.UpstreamAddress {
result := make([]*types.UpstreamAddress, to-from+1)
for i := from; i <= to; i++ {
result[i-from] = GetUpstream(i)
}
return result
}
// GetRandomUpstreams returns a slice with a random number of upstreams
func GetRandomUpstreams(minCount, maxCount int) []*types.UpstreamAddress {
count := minCount + rand.Intn(maxCount-minCount)
return GetUpstreams(1, count)
}