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.

194 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, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
  69. want := []Status{
  70. {true, start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  71. {true, start, _100ms, 0, 10, 1, 100, 100, 100, 100, 0, 0, 0},
  72. {true, start, _200ms, _100ms, 20, 2, 100, 100, 100, 100, 0, 0, 0},
  73. {true, start, _300ms, _200ms, 20, 3, 0, 90, 67, 100, 0, 0, 0},
  74. {false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
  75. {false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
  76. }
  77. for i, s := range status {
  78. if !statusesAreEqual(&s, &want[i]) {
  79. t.Errorf("r.Status(%v)\nexpected: %v\ngot : %v", i, want[i], s)
  80. }
  81. }
  82. if !bytes.Equal(b[:20], in[:20]) {
  83. t.Errorf("r.Read() input doesn't match output")
  84. }
  85. }
  86. func TestWriter(t *testing.T) {
  87. b := make([]byte, 100)
  88. for i := range b {
  89. b[i] = byte(i)
  90. }
  91. w := NewWriter(&bytes.Buffer{}, 200)
  92. start := time.Now()
  93. // Make sure w implements Limiter
  94. _ = Limiter(w)
  95. // Non-blocking 20-byte write for the first sample returns ErrLimit
  96. w.SetBlocking(false)
  97. if n, err := w.Write(b); n != 20 || err != ErrLimit {
  98. t.Fatalf("w.Write(b) expected 20 (ErrLimit); got %v (%v)", n, err)
  99. } else if rt := time.Since(start); rt > _50ms {
  100. t.Fatalf("w.Write(b) took too long (%v)", rt)
  101. }
  102. // Blocking 80-byte write
  103. w.SetBlocking(true)
  104. if n, err := w.Write(b[20:]); n != 80 || err != nil {
  105. t.Fatalf("w.Write(b[20:]) expected 80 (<nil>); got %v (%v)", n, err)
  106. } else if rt := time.Since(start); rt < _300ms {
  107. // Explanation for `rt < _300ms` (as opposed to `< _400ms`)
  108. //
  109. // |<-- start | |
  110. // epochs: -----0ms|---100ms|---200ms|---300ms|---400ms
  111. // sends: 20|20 |20 |20 |20#
  112. //
  113. // NOTE: The '#' symbol can thus happen before 400ms is up.
  114. // Thus, we can only panic if rt < _300ms.
  115. t.Fatalf("w.Write(b[20:]) returned ahead of time (%v)", rt)
  116. }
  117. w.SetTransferSize(100)
  118. status := []Status{w.Status(), nextStatus(w.Monitor)}
  119. start = status[0].Start
  120. // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
  121. want := []Status{
  122. {true, start, _400ms, 0, 80, 4, 200, 200, 200, 200, 20, _100ms, 80000},
  123. {true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},
  124. }
  125. for i, s := range status {
  126. if !statusesAreEqual(&s, &want[i]) {
  127. t.Errorf("w.Status(%v)\nexpected: %v\ngot : %v\n", i, want[i], s)
  128. }
  129. }
  130. if !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {
  131. t.Errorf("w.Write() input doesn't match output")
  132. }
  133. }
  134. const maxDeviationForDuration = 50 * time.Millisecond
  135. const maxDeviationForRate int64 = 50
  136. // statusesAreEqual returns true if s1 is equal to s2. Equality here means
  137. // general equality of fields except for the duration and rates, which can
  138. // drift due to unpredictable delays (e.g. thread wakes up 25ms after
  139. // `time.Sleep` has ended).
  140. func statusesAreEqual(s1 *Status, s2 *Status) bool {
  141. if s1.Active == s2.Active &&
  142. s1.Start == s2.Start &&
  143. durationsAreEqual(s1.Duration, s2.Duration, maxDeviationForDuration) &&
  144. s1.Idle == s2.Idle &&
  145. s1.Bytes == s2.Bytes &&
  146. s1.Samples == s2.Samples &&
  147. ratesAreEqual(s1.InstRate, s2.InstRate, maxDeviationForRate) &&
  148. ratesAreEqual(s1.CurRate, s2.CurRate, maxDeviationForRate) &&
  149. ratesAreEqual(s1.AvgRate, s2.AvgRate, maxDeviationForRate) &&
  150. ratesAreEqual(s1.PeakRate, s2.PeakRate, maxDeviationForRate) &&
  151. s1.BytesRem == s2.BytesRem &&
  152. durationsAreEqual(s1.TimeRem, s2.TimeRem, maxDeviationForDuration) &&
  153. s1.Progress == s2.Progress {
  154. return true
  155. }
  156. return false
  157. }
  158. func durationsAreEqual(d1 time.Duration, d2 time.Duration, maxDeviation time.Duration) bool {
  159. return d2-d1 <= maxDeviation
  160. }
  161. func ratesAreEqual(r1 int64, r2 int64, maxDeviation int64) bool {
  162. sub := r1 - r2
  163. if sub < 0 {
  164. sub = -sub
  165. }
  166. if sub <= maxDeviation {
  167. return true
  168. }
  169. return false
  170. }