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.

189 lines
5.5 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. Status{true, start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  71. Status{true, start, _100ms, 0, 10, 1, 100, 100, 100, 100, 0, 0, 0},
  72. Status{true, start, _200ms, _100ms, 20, 2, 100, 100, 100, 100, 0, 0, 0},
  73. Status{true, start, _300ms, _200ms, 20, 3, 0, 90, 67, 100, 0, 0, 0},
  74. Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
  75. Status{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 < _400ms {
  107. t.Fatalf("w.Write(b[20:]) returned ahead of time (%v)", rt)
  108. }
  109. w.SetTransferSize(100)
  110. status := []Status{w.Status(), nextStatus(w.Monitor)}
  111. start = status[0].Start
  112. // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
  113. want := []Status{
  114. Status{true, start, _400ms, 0, 80, 4, 200, 200, 200, 200, 20, _100ms, 80000},
  115. Status{true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},
  116. }
  117. for i, s := range status {
  118. if !statusesAreEqual(&s, &want[i]) {
  119. t.Errorf("w.Status(%v)\nexpected: %v\ngot : %v\n", i, want[i], s)
  120. }
  121. }
  122. if !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {
  123. t.Errorf("w.Write() input doesn't match output")
  124. }
  125. }
  126. const maxDeviationForDuration = 50 * time.Millisecond
  127. const maxDeviationForRate int64 = 50
  128. // statusesAreEqual returns true if s1 is equal to s2. Equality here means
  129. // general equality of fields except for the duration and rates, which can
  130. // drift due to unpredictable delays (e.g. thread wakes up 25ms after
  131. // `time.Sleep` has ended).
  132. func statusesAreEqual(s1 *Status, s2 *Status) bool {
  133. if s1.Active == s2.Active &&
  134. s1.Start == s2.Start &&
  135. durationsAreEqual(s1.Duration, s2.Duration, maxDeviationForDuration) &&
  136. s1.Idle == s2.Idle &&
  137. s1.Bytes == s2.Bytes &&
  138. s1.Samples == s2.Samples &&
  139. ratesAreEqual(s1.InstRate, s2.InstRate, maxDeviationForRate) &&
  140. ratesAreEqual(s1.CurRate, s2.CurRate, maxDeviationForRate) &&
  141. ratesAreEqual(s1.AvgRate, s2.AvgRate, maxDeviationForRate) &&
  142. ratesAreEqual(s1.PeakRate, s2.PeakRate, maxDeviationForRate) &&
  143. s1.BytesRem == s2.BytesRem &&
  144. durationsAreEqual(s1.TimeRem, s2.TimeRem, maxDeviationForDuration) &&
  145. s1.Progress == s2.Progress {
  146. return true
  147. }
  148. return false
  149. }
  150. func durationsAreEqual(d1 time.Duration, d2 time.Duration, maxDeviation time.Duration) bool {
  151. if d2-d1 <= maxDeviation {
  152. return true
  153. }
  154. return false
  155. }
  156. func ratesAreEqual(r1 int64, r2 int64, maxDeviation int64) bool {
  157. sub := r1 - r2
  158. if sub < 0 {
  159. sub = -sub
  160. }
  161. if sub <= maxDeviation {
  162. return true
  163. }
  164. return false
  165. }