progress_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package raft
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestInflightsAdd(t *testing.T) {
  20. // no rotating case
  21. in := &inflights{
  22. size: 10,
  23. buffer: make([]uint64, 10),
  24. }
  25. for i := 0; i < 5; i++ {
  26. in.add(uint64(i))
  27. }
  28. wantIn := &inflights{
  29. start: 0,
  30. count: 5,
  31. size: 10,
  32. // ↓------------
  33. buffer: []uint64{0, 1, 2, 3, 4, 0, 0, 0, 0, 0},
  34. }
  35. if !reflect.DeepEqual(in, wantIn) {
  36. t.Fatalf("in = %+v, want %+v", in, wantIn)
  37. }
  38. for i := 5; i < 10; i++ {
  39. in.add(uint64(i))
  40. }
  41. wantIn2 := &inflights{
  42. start: 0,
  43. count: 10,
  44. size: 10,
  45. // ↓---------------------------
  46. buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  47. }
  48. if !reflect.DeepEqual(in, wantIn2) {
  49. t.Fatalf("in = %+v, want %+v", in, wantIn2)
  50. }
  51. // rotating case
  52. in2 := &inflights{
  53. start: 5,
  54. size: 10,
  55. buffer: make([]uint64, 10),
  56. }
  57. for i := 0; i < 5; i++ {
  58. in2.add(uint64(i))
  59. }
  60. wantIn21 := &inflights{
  61. start: 5,
  62. count: 5,
  63. size: 10,
  64. // ↓------------
  65. buffer: []uint64{0, 0, 0, 0, 0, 0, 1, 2, 3, 4},
  66. }
  67. if !reflect.DeepEqual(in2, wantIn21) {
  68. t.Fatalf("in = %+v, want %+v", in2, wantIn21)
  69. }
  70. for i := 5; i < 10; i++ {
  71. in2.add(uint64(i))
  72. }
  73. wantIn22 := &inflights{
  74. start: 5,
  75. count: 10,
  76. size: 10,
  77. // -------------- ↓------------
  78. buffer: []uint64{5, 6, 7, 8, 9, 0, 1, 2, 3, 4},
  79. }
  80. if !reflect.DeepEqual(in2, wantIn22) {
  81. t.Fatalf("in = %+v, want %+v", in2, wantIn22)
  82. }
  83. }
  84. func TestInflightFreeTo(t *testing.T) {
  85. // no rotating case
  86. in := newInflights(10)
  87. for i := 0; i < 10; i++ {
  88. in.add(uint64(i))
  89. }
  90. in.freeTo(4)
  91. wantIn := &inflights{
  92. start: 5,
  93. count: 5,
  94. size: 10,
  95. // ↓------------
  96. buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  97. }
  98. if !reflect.DeepEqual(in, wantIn) {
  99. t.Fatalf("in = %+v, want %+v", in, wantIn)
  100. }
  101. in.freeTo(8)
  102. wantIn2 := &inflights{
  103. start: 9,
  104. count: 1,
  105. size: 10,
  106. // ↓
  107. buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  108. }
  109. if !reflect.DeepEqual(in, wantIn2) {
  110. t.Fatalf("in = %+v, want %+v", in, wantIn2)
  111. }
  112. // rotating case
  113. for i := 10; i < 15; i++ {
  114. in.add(uint64(i))
  115. }
  116. in.freeTo(12)
  117. wantIn3 := &inflights{
  118. start: 3,
  119. count: 2,
  120. size: 10,
  121. // ↓-----
  122. buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
  123. }
  124. if !reflect.DeepEqual(in, wantIn3) {
  125. t.Fatalf("in = %+v, want %+v", in, wantIn3)
  126. }
  127. in.freeTo(14)
  128. wantIn4 := &inflights{
  129. start: 0,
  130. count: 0,
  131. size: 10,
  132. // ↓
  133. buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
  134. }
  135. if !reflect.DeepEqual(in, wantIn4) {
  136. t.Fatalf("in = %+v, want %+v", in, wantIn4)
  137. }
  138. }
  139. func TestInflightFreeFirstOne(t *testing.T) {
  140. in := newInflights(10)
  141. for i := 0; i < 10; i++ {
  142. in.add(uint64(i))
  143. }
  144. in.freeFirstOne()
  145. wantIn := &inflights{
  146. start: 1,
  147. count: 9,
  148. size: 10,
  149. // ↓------------------------
  150. buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  151. }
  152. if !reflect.DeepEqual(in, wantIn) {
  153. t.Fatalf("in = %+v, want %+v", in, wantIn)
  154. }
  155. }