You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.8 KiB

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