-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmio_test.go
169 lines (161 loc) · 4.19 KB
/
mio_test.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package mio
import (
"io"
"io/ioutil"
"os"
"runtime"
"sync"
"testing"
"time"
"github.com/artyom/metrics"
)
func TestWriterDoubleClose(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatal("double close caused panic:", r)
}
}()
histogram := NewSelfCleaningHistogram(
metrics.NewHistogram(metrics.NewUniformSample(100)),
150*time.Millisecond)
mw := NewWriter(ioutil.Discard, histogram)
t.Log("testing double Close(), should call Done() on underlying Registrar only once")
mw.Close()
mw.Close()
}
func TestWriterBasic(t *testing.T) {
histogram := metrics.NewHistogram(metrics.NewUniformSample(100))
mw := NewWriter(ioutil.Discard, histogram)
file, err := os.Open(os.Args[0])
if err != nil {
t.Fatal("failed to open file:", err)
}
defer file.Close()
r := io.LimitReader(file, 1<<19)
n, err := io.Copy(mw, r)
if err != nil {
t.Fatal("failed to copy data:", err)
}
t.Log("bytes copied:", n)
t.Logf("%d writes, latency min: %s, max: %s",
histogram.Count(),
time.Duration(histogram.Min()),
time.Duration(histogram.Max()))
if histogram.Count() == 0 {
t.Fatal("histogram should have some registered samples")
}
}
func TestWriterSelfCleaning(t *testing.T) {
histogram := NewSelfCleaningHistogram(
metrics.NewHistogram(metrics.NewUniformSample(100)),
150*time.Millisecond)
mw := NewWriter(ioutil.Discard, histogram)
file, err := os.Open(os.Args[0])
if err != nil {
t.Fatal("failed to open file:", err)
}
defer file.Close()
r := io.LimitReader(file, 1<<19)
n, err := io.Copy(mw, r)
if err != nil {
t.Fatal("failed to copy data:", err)
}
if err := mw.Close(); err != nil {
t.Fatal("metered writer close error:", err)
}
t.Log("bytes copied:", n)
t.Logf("%d writes, latency min: %s, max: %s",
histogram.Count(),
time.Duration(histogram.Min()),
time.Duration(histogram.Max()))
if histogram.Count() == 0 {
t.Fatal("histogram should have some registered samples")
}
t.Log("waiting for released histogram to clear")
time.Sleep(200 * time.Millisecond)
cnt := histogram.Count()
t.Logf("%d writes, latency min: %s, max: %s",
cnt,
time.Duration(histogram.Min()),
time.Duration(histogram.Max()))
if cnt != 0 {
t.Fatal("histogram should be empty, but has samples:", cnt)
}
}
func TestSelfCleaningHistogram(t *testing.T) {
sh := NewSelfCleaningHistogram(
metrics.NewHistogram(metrics.NewUniformSample(100)),
150*time.Millisecond)
t.Log("registering activity")
sh.Register()
sh.Register()
sh.Update(150)
sh.Update(100)
sh.Update(50)
sh.Done()
sh.Done()
t.Log("activity de-registered")
if cnt := sh.Count(); cnt != 3 {
t.Fatal("should have 3 registered samples, got:", cnt)
}
t.Log("waiting for histogram to clear")
time.Sleep(300 * time.Millisecond)
if cnt := sh.Count(); cnt != 0 {
t.Fatal("should have 0 registered samples, got:", cnt)
}
t.Log("registering activity")
sh.Register()
sh.Update(50)
t.Log("waiting for period longer than clear delay")
time.Sleep(300 * time.Millisecond)
sh.Update(150)
if cnt := sh.Count(); cnt != 2 {
t.Fatal("should have 2 registered samples, got:", cnt)
}
}
func TestSelfCleaningHistogram_Shutdown(t *testing.T) {
sh := NewSelfCleaningHistogram(
metrics.NewHistogram(metrics.NewUniformSample(100)),
100*time.Millisecond)
t.Log("registering activity")
sh.Register()
sh.Register()
sh.Update(150)
sh.Update(100)
sh.Update(50)
sh.Done()
sh.Done()
t.Log("activity de-registered")
if cnt := sh.Count(); cnt != 3 {
t.Fatal("should have 3 registered samples, got:", cnt)
}
sh.Shutdown()
sh.Shutdown()
t.Log("waiting for period longer than clear delay, timer should be stopped")
time.Sleep(200 * time.Millisecond)
if cnt := sh.Count(); cnt != 3 {
t.Fatal("should have 3 registered samples, got:", cnt)
}
}
func TestSelfCleaningHistogram_concurrent(t *testing.T) {
if runtime.NumCPU() < 2 {
t.Skip("NumCPU<2, skipping: this test requires parallelism")
}
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
sh := NewSelfCleaningHistogram(
metrics.NewHistogram(metrics.NewUniformSample(100)),
100*time.Millisecond)
defer sh.Shutdown()
const c = 10000
var wg sync.WaitGroup
wg.Add(c)
for i := 0; i < c; i++ {
go func() {
sh.Register()
sh.Update(100)
sh.Done()
wg.Done()
}()
}
wg.Wait()
}