This repository has been archived by the owner on May 4, 2022. It is now read-only.
forked from atlassian/gostatsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
95 lines (79 loc) · 2.68 KB
/
fake.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package fakesocket
import (
"bytes"
"errors"
"fmt"
"math/rand"
"net"
"sync/atomic"
"time"
)
// FakeMetric is a fake metric.
var FakeMetric = []byte("foo.bar.baz:2|c")
// FakeAddr is a fake net.Addr
var FakeAddr = &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 8181,
}
// FakePacketConn is a fake net.PacketConn providing FakeMetric when read from.
type FakePacketConn struct{}
// ReadFrom copies FakeMetric into b.
func (fpc FakePacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n := copy(b, FakeMetric)
return n, FakeAddr, nil
}
// WriteTo dummy impl.
func (fpc FakePacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { return 0, nil }
// Close dummy impl.
func (fpc FakePacketConn) Close() error { return nil }
// LocalAddr dummy impl.
func (fpc FakePacketConn) LocalAddr() net.Addr { return FakeAddr }
// SetDeadline dummy impl.
func (fpc FakePacketConn) SetDeadline(t time.Time) error { return nil }
// SetReadDeadline dummy impl.
func (fpc FakePacketConn) SetReadDeadline(t time.Time) error { return nil }
// SetWriteDeadline dummy impl.
func (fpc FakePacketConn) SetWriteDeadline(t time.Time) error { return nil }
// FakeRandomPacketConn is a fake net.PacketConn providing random fake metrics.
type FakeRandomPacketConn struct {
FakePacketConn
}
// ReadFrom generates random metric and writes in into b.
func (frpc FakeRandomPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
num := rand.Int31n(10000) // Randomize metric name
buf := new(bytes.Buffer)
switch rand.Int31n(4) {
case 0: // Counter
fmt.Fprintf(buf, "statsd.tester.counter_%d:%f|c\n", num, rand.Float64()*100)
case 1: // Gauge
fmt.Fprintf(buf, "statsd.tester.gauge_%d:%f|g\n", num, rand.Float64()*100)
case 2: // Timer
n := 10
for i := 0; i < n; i++ {
fmt.Fprintf(buf, "statsd.tester.timer_%d:%f|ms\n", num, rand.Float64()*100)
}
case 3: // Set
for i := 0; i < 10; i++ {
fmt.Fprintf(buf, "statsd.tester.set_%d:%d|s\n", num, rand.Int31n(9)+1)
}
default:
panic(errors.New("unreachable"))
}
n := copy(b, buf.Bytes())
return n, FakeAddr, nil
}
// CountingFakeRandomPacketConn is a fake net.PacketConn providing random fake metrics and counting number of performed read operations.
// Safe for concurrent use.
type CountingFakeRandomPacketConn struct {
NumReads uint64
FakeRandomPacketConn
}
// ReadFrom generates random metric and writes in into b.
func (frpc *CountingFakeRandomPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
atomic.AddUint64(&frpc.NumReads, 1)
return frpc.FakeRandomPacketConn.ReadFrom(b)
}
// Factory is a replacement for net.ListenPacket() that produces instances of FakeRandomPacketConn.
func Factory() (net.PacketConn, error) {
return FakeRandomPacketConn{}, nil
}