2 // Written by Maxim Khitrov (November 2012)
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
22 func nextStatus(m *Monitor) Status {
24 for i := 0; i < 30; i++ {
25 if s := m.Status(); s.Samples != samples {
28 time.Sleep(5 * time.Millisecond)
33 func TestReader(t *testing.T) {
34 in := make([]byte, 100)
38 b := make([]byte, 100)
39 r := NewReader(bytes.NewReader(in), 100)
42 // Make sure r implements Limiter
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)
52 // No new Reads allowed in the current sample
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)
60 status := [6]Status{0: r.Status()} // No samples in the first status
62 // 2nd read of 10 bytes blocks until the next sample
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)
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
74 if n := r.Done(); n != 20 {
75 t.Fatalf("r.Done() expected 20; got %v", n)
78 status[4] = r.Status()
79 status[5] = nextStatus(r.Monitor) // Timeout
80 start = status[0].Start
82 // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
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},
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)
96 if !bytes.Equal(b[:20], in[:20]) {
97 t.Errorf("r.Read() input doesn't match output")
101 func TestWriter(t *testing.T) {
102 b := make([]byte, 100)
106 w := NewWriter(&bytes.Buffer{}, 200)
109 // Make sure w implements Limiter
112 // Non-blocking 20-byte write for the first sample returns ErrLimit
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)
120 // Blocking 80-byte write
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)
128 w.SetTransferSize(100)
129 status := []Status{w.Status(), nextStatus(w.Monitor)}
130 start = status[0].Start
132 // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
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},
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)
142 if !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {
143 t.Errorf("w.Write() input doesn't match output")
147 const maxDeviationForDuration = 50 * time.Millisecond
148 const maxDeviationForRate int64 = 50
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 {
173 func durationsAreEqual(d1 time.Duration, d2 time.Duration, maxDeviation time.Duration) bool {
174 return d2-d1 <= maxDeviation
177 func ratesAreEqual(r1 int64, r2 int64, maxDeviation int64) bool {
182 if sub <= maxDeviation {