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.

146 lines
4.2 KiB

  1. //
  2. // Written by Maxim Khitrov (November 2012)
  3. //
  4. package flowrate
  5. import (
  6. "bytes"
  7. "reflect"
  8. "testing"
  9. "time"
  10. )
  11. const (
  12. _50ms = 50 * time.Millisecond
  13. _100ms = 100 * time.Millisecond
  14. _200ms = 200 * time.Millisecond
  15. _300ms = 300 * time.Millisecond
  16. _400ms = 400 * time.Millisecond
  17. _500ms = 500 * time.Millisecond
  18. )
  19. func nextStatus(m *Monitor) Status {
  20. samples := m.samples
  21. for i := 0; i < 30; i++ {
  22. if s := m.Status(); s.Samples != samples {
  23. return s
  24. }
  25. time.Sleep(5 * time.Millisecond)
  26. }
  27. return m.Status()
  28. }
  29. func TestReader(t *testing.T) {
  30. in := make([]byte, 100)
  31. for i := range in {
  32. in[i] = byte(i)
  33. }
  34. b := make([]byte, 100)
  35. r := NewReader(bytes.NewReader(in), 100)
  36. start := time.Now()
  37. // Make sure r implements Limiter
  38. _ = Limiter(r)
  39. // 1st read of 10 bytes is performed immediately
  40. if n, err := r.Read(b); n != 10 || err != nil {
  41. t.Fatalf("r.Read(b) expected 10 (<nil>); got %v (%v)", n, err)
  42. } else if rt := time.Since(start); rt > _50ms {
  43. t.Fatalf("r.Read(b) took too long (%v)", rt)
  44. }
  45. // No new Reads allowed in the current sample
  46. r.SetBlocking(false)
  47. if n, err := r.Read(b); n != 0 || err != nil {
  48. t.Fatalf("r.Read(b) expected 0 (<nil>); got %v (%v)", n, err)
  49. } else if rt := time.Since(start); rt > _50ms {
  50. t.Fatalf("r.Read(b) took too long (%v)", rt)
  51. }
  52. status := [6]Status{0: r.Status()} // No samples in the first status
  53. // 2nd read of 10 bytes blocks until the next sample
  54. r.SetBlocking(true)
  55. if n, err := r.Read(b[10:]); n != 10 || err != nil {
  56. t.Fatalf("r.Read(b[10:]) expected 10 (<nil>); got %v (%v)", n, err)
  57. } else if rt := time.Since(start); rt < _100ms {
  58. t.Fatalf("r.Read(b[10:]) returned ahead of time (%v)", rt)
  59. }
  60. status[1] = r.Status() // 1st sample
  61. status[2] = nextStatus(r.Monitor) // 2nd sample
  62. status[3] = nextStatus(r.Monitor) // No activity for the 3rd sample
  63. if n := r.Done(); n != 20 {
  64. t.Fatalf("r.Done() expected 20; got %v", n)
  65. }
  66. status[4] = r.Status()
  67. status[5] = nextStatus(r.Monitor) // Timeout
  68. start = status[0].Start
  69. // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
  70. want := []Status{
  71. Status{true, start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  72. Status{true, start, _100ms, 0, 10, 1, 100, 100, 100, 100, 0, 0, 0},
  73. Status{true, start, _200ms, _100ms, 20, 2, 100, 100, 100, 100, 0, 0, 0},
  74. Status{true, start, _300ms, _200ms, 20, 3, 0, 90, 67, 100, 0, 0, 0},
  75. Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
  76. Status{false, start, _300ms, 0, 20, 3, 0, 0, 67, 100, 0, 0, 0},
  77. }
  78. for i, s := range status {
  79. if !reflect.DeepEqual(&s, &want[i]) {
  80. t.Errorf("r.Status(%v) expected %v; got %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 < _400ms {
  108. t.Fatalf("w.Write(b[20:]) returned ahead of time (%v)", rt)
  109. }
  110. w.SetTransferSize(100)
  111. status := []Status{w.Status(), nextStatus(w.Monitor)}
  112. start = status[0].Start
  113. // Active, Start, Duration, Idle, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, TimeRem, Progress
  114. want := []Status{
  115. Status{true, start, _400ms, 0, 80, 4, 200, 200, 200, 200, 20, _100ms, 80000},
  116. Status{true, start, _500ms, _100ms, 100, 5, 200, 200, 200, 200, 0, 0, 100000},
  117. }
  118. for i, s := range status {
  119. if !reflect.DeepEqual(&s, &want[i]) {
  120. t.Errorf("w.Status(%v) expected %v; got %v", i, want[i], s)
  121. }
  122. }
  123. if !bytes.Equal(b, w.Writer.(*bytes.Buffer).Bytes()) {
  124. t.Errorf("w.Write() input doesn't match output")
  125. }
  126. }