OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / flowrate / io_test.go
1 //
2 // Written by Maxim Khitrov (November 2012)
3 //
4
5 package flowrate
6
7 import (
8         "bytes"
9         "testing"
10         "time"
11 )
12
13 const (
14         _50ms  = 50 * time.Millisecond
15         _100ms = 100 * time.Millisecond
16         _200ms = 200 * time.Millisecond
17         _300ms = 300 * time.Millisecond
18         _400ms = 400 * time.Millisecond
19         _500ms = 500 * time.Millisecond
20 )
21
22 func nextStatus(m *Monitor) Status {
23         samples := m.samples
24         for i := 0; i < 30; i++ {
25                 if s := m.Status(); s.Samples != samples {
26                         return s
27                 }
28                 time.Sleep(5 * time.Millisecond)
29         }
30         return m.Status()
31 }
32
33 func TestReader(t *testing.T) {
34         in := make([]byte, 100)
35         for i := range in {
36                 in[i] = byte(i)
37         }
38         b := make([]byte, 100)
39         r := NewReader(bytes.NewReader(in), 100)
40         start := time.Now()
41
42         // Make sure r implements Limiter
43         _ = Limiter(r)
44
45         // 1st read of 10 bytes is performed immediately
46         if n, err := r.Read(b); n != 10 || err != nil {
47                 t.Fatalf("r.Read(b) expected 10 (<nil>); got %v (%v)", n, err)
48         } else if rt := time.Since(start); rt > _50ms {
49                 t.Fatalf("r.Read(b) took too long (%v)", rt)
50         }
51
52         // No new Reads allowed in the current sample
53         r.SetBlocking(false)
54         if n, err := r.Read(b); n != 0 || err != nil {
55                 t.Fatalf("r.Read(b) expected 0 (<nil>); got %v (%v)", n, err)
56         } else if rt := time.Since(start); rt > _50ms {
57                 t.Fatalf("r.Read(b) took too long (%v)", rt)
58         }
59
60         status := [6]Status{0: r.Status()} // No samples in the first status
61
62         // 2nd read of 10 bytes blocks until the next sample
63         r.SetBlocking(true)
64         if n, err := r.Read(b[10:]); n != 10 || err != nil {
65                 t.Fatalf("r.Read(b[10:]) expected 10 (<nil>); got %v (%v)", n, err)
66         } else if rt := time.Since(start); rt < _100ms {
67                 t.Fatalf("r.Read(b[10:]) returned ahead of time (%v)", rt)
68         }
69
70         status[1] = r.Status()            // 1st sample
71         status[2] = nextStatus(r.Monitor) // 2nd sample
72         status[3] = nextStatus(r.Monitor) // No activity for the 3rd sample
73
74         if n := r.Done(); n != 20 {
75                 t.Fatalf("r.Done() expected 20; got %v", n)
76         }
77
78         status[4] = r.Status()
79         status[5] = nextStatus(r.Monitor) // Timeout
80         start = status[0].Start
81
82         // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
83         want := []Status{
84                 Status{true, start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
85                 Status{true, start, _100ms, 0, 10, 1, 100, 100, 100, 100, 0, 0, 0},
86                 Status{true, start, _200ms, _100ms, 20, 2, 100, 100, 100, 100, 0, 0, 0},
87                 Status{true, start, _300ms, _200ms, 20, 3, 0, 90, 67, 100, 0, 0, 0},
88                 Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
89                 Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
90         }
91         for i, s := range status {
92                 if !statusesAreEqual(&s, &want[i]) {
93                         t.Errorf("r.Status(%v)\nexpected: %v\ngot     : %v", i, want[i], s)
94                 }
95         }
96         if !bytes.Equal(b[:20], in[:20]) {
97                 t.Errorf("r.Read() input doesn't match output")
98         }
99 }
100
101 func TestWriter(t *testing.T) {
102         b := make([]byte, 100)
103         for i := range b {
104                 b[i] = byte(i)
105         }
106         w := NewWriter(&bytes.Buffer{}, 200)
107         start := time.Now()
108
109         // Make sure w implements Limiter
110         _ = Limiter(w)
111
112         // Non-blocking 20-byte write for the first sample returns ErrLimit
113         w.SetBlocking(false)
114         if n, err := w.Write(b); n != 20 || err != ErrLimit {
115                 t.Fatalf("w.Write(b) expected 20 (ErrLimit); got %v (%v)", n, err)
116         } else if rt := time.Since(start); rt > _50ms {
117                 t.Fatalf("w.Write(b) took too long (%v)", rt)
118         }
119
120         // Blocking 80-byte write
121         w.SetBlocking(true)
122         if n, err := w.Write(b[20:]); n != 80 || err != nil {
123                 t.Fatalf("w.Write(b[20:]) expected 80 (<nil>); got %v (%v)", n, err)
124         } else if rt := time.Since(start); rt < _400ms {
125                 t.Fatalf("w.Write(b[20:]) returned ahead of time (%v)", rt)
126         }
127
128         w.SetTransferSize(100)
129         status := []Status{w.Status(), nextStatus(w.Monitor)}
130         start = status[0].Start
131
132         // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
133         want := []Status{
134                 Status{true, start, _400ms, 0, 80, 4, 200, 200, 200, 200, 20, _100ms, 80000},
135                 Status{true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},
136         }
137         for i, s := range status {
138                 if !statusesAreEqual(&s, &want[i]) {
139                         t.Errorf("w.Status(%v)\nexpected: %v\ngot     : %v\n", i, want[i], s)
140                 }
141         }
142         if !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {
143                 t.Errorf("w.Write() input doesn't match output")
144         }
145 }
146
147 const maxDeviationForDuration = 50 * time.Millisecond
148 const maxDeviationForRate int64 = 50
149
150 // statusesAreEqual returns true if s1 is equal to s2. Equality here means
151 // general equality of fields except for the duration and rates, which can
152 // drift due to unpredictable delays (e.g. thread wakes up 25ms after
153 // `time.Sleep` has ended).
154 func statusesAreEqual(s1 *Status, s2 *Status) bool {
155         if s1.Active == s2.Active &&
156                 s1.Start == s2.Start &&
157                 durationsAreEqual(s1.Duration, s2.Duration, maxDeviationForDuration) &&
158                 s1.Idle == s2.Idle &&
159                 s1.Bytes == s2.Bytes &&
160                 s1.Samples == s2.Samples &&
161                 ratesAreEqual(s1.InstRate, s2.InstRate, maxDeviationForRate) &&
162                 ratesAreEqual(s1.CurRate, s2.CurRate, maxDeviationForRate) &&
163                 ratesAreEqual(s1.AvgRate, s2.AvgRate, maxDeviationForRate) &&
164                 ratesAreEqual(s1.PeakRate, s2.PeakRate, maxDeviationForRate) &&
165                 s1.BytesRem == s2.BytesRem &&
166                 durationsAreEqual(s1.TimeRem, s2.TimeRem, maxDeviationForDuration) &&
167                 s1.Progress == s2.Progress {
168                 return true
169         }
170         return false
171 }
172
173 func durationsAreEqual(d1 time.Duration, d2 time.Duration, maxDeviation time.Duration) bool {
174         return d2-d1 <= maxDeviation
175 }
176
177 func ratesAreEqual(r1 int64, r2 int64, maxDeviation int64) bool {
178         sub := r1 - r2
179         if sub < 0 {
180                 sub = -sub
181         }
182         if sub <= maxDeviation {
183                 return true
184         }
185         return false
186 }