...

Source file src/go.etcd.io/etcd/raft/v3/tracker/inflights_test.go

Documentation: go.etcd.io/etcd/raft/v3/tracker

     1  // Copyright 2019 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  
    15  package tracker
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestInflightsAdd(t *testing.T) {
    23  	// no rotating case
    24  	in := &Inflights{
    25  		size:   10,
    26  		buffer: make([]uint64, 10),
    27  	}
    28  
    29  	for i := 0; i < 5; i++ {
    30  		in.Add(uint64(i))
    31  	}
    32  
    33  	wantIn := &Inflights{
    34  		start: 0,
    35  		count: 5,
    36  		size:  10,
    37  		//               ↓------------
    38  		buffer: []uint64{0, 1, 2, 3, 4, 0, 0, 0, 0, 0},
    39  	}
    40  
    41  	if !reflect.DeepEqual(in, wantIn) {
    42  		t.Fatalf("in = %+v, want %+v", in, wantIn)
    43  	}
    44  
    45  	for i := 5; i < 10; i++ {
    46  		in.Add(uint64(i))
    47  	}
    48  
    49  	wantIn2 := &Inflights{
    50  		start: 0,
    51  		count: 10,
    52  		size:  10,
    53  		//               ↓---------------------------
    54  		buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    55  	}
    56  
    57  	if !reflect.DeepEqual(in, wantIn2) {
    58  		t.Fatalf("in = %+v, want %+v", in, wantIn2)
    59  	}
    60  
    61  	// rotating case
    62  	in2 := &Inflights{
    63  		start:  5,
    64  		size:   10,
    65  		buffer: make([]uint64, 10),
    66  	}
    67  
    68  	for i := 0; i < 5; i++ {
    69  		in2.Add(uint64(i))
    70  	}
    71  
    72  	wantIn21 := &Inflights{
    73  		start: 5,
    74  		count: 5,
    75  		size:  10,
    76  		//                              ↓------------
    77  		buffer: []uint64{0, 0, 0, 0, 0, 0, 1, 2, 3, 4},
    78  	}
    79  
    80  	if !reflect.DeepEqual(in2, wantIn21) {
    81  		t.Fatalf("in = %+v, want %+v", in2, wantIn21)
    82  	}
    83  
    84  	for i := 5; i < 10; i++ {
    85  		in2.Add(uint64(i))
    86  	}
    87  
    88  	wantIn22 := &Inflights{
    89  		start: 5,
    90  		count: 10,
    91  		size:  10,
    92  		//               -------------- ↓------------
    93  		buffer: []uint64{5, 6, 7, 8, 9, 0, 1, 2, 3, 4},
    94  	}
    95  
    96  	if !reflect.DeepEqual(in2, wantIn22) {
    97  		t.Fatalf("in = %+v, want %+v", in2, wantIn22)
    98  	}
    99  }
   100  
   101  func TestInflightFreeTo(t *testing.T) {
   102  	// no rotating case
   103  	in := NewInflights(10)
   104  	for i := 0; i < 10; i++ {
   105  		in.Add(uint64(i))
   106  	}
   107  
   108  	in.FreeLE(4)
   109  
   110  	wantIn := &Inflights{
   111  		start: 5,
   112  		count: 5,
   113  		size:  10,
   114  		//                              ↓------------
   115  		buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   116  	}
   117  
   118  	if !reflect.DeepEqual(in, wantIn) {
   119  		t.Fatalf("in = %+v, want %+v", in, wantIn)
   120  	}
   121  
   122  	in.FreeLE(8)
   123  
   124  	wantIn2 := &Inflights{
   125  		start: 9,
   126  		count: 1,
   127  		size:  10,
   128  		//                                          ↓
   129  		buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   130  	}
   131  
   132  	if !reflect.DeepEqual(in, wantIn2) {
   133  		t.Fatalf("in = %+v, want %+v", in, wantIn2)
   134  	}
   135  
   136  	// rotating case
   137  	for i := 10; i < 15; i++ {
   138  		in.Add(uint64(i))
   139  	}
   140  
   141  	in.FreeLE(12)
   142  
   143  	wantIn3 := &Inflights{
   144  		start: 3,
   145  		count: 2,
   146  		size:  10,
   147  		//                           ↓-----
   148  		buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
   149  	}
   150  
   151  	if !reflect.DeepEqual(in, wantIn3) {
   152  		t.Fatalf("in = %+v, want %+v", in, wantIn3)
   153  	}
   154  
   155  	in.FreeLE(14)
   156  
   157  	wantIn4 := &Inflights{
   158  		start: 0,
   159  		count: 0,
   160  		size:  10,
   161  		//               ↓
   162  		buffer: []uint64{10, 11, 12, 13, 14, 5, 6, 7, 8, 9},
   163  	}
   164  
   165  	if !reflect.DeepEqual(in, wantIn4) {
   166  		t.Fatalf("in = %+v, want %+v", in, wantIn4)
   167  	}
   168  }
   169  
   170  func TestInflightFreeFirstOne(t *testing.T) {
   171  	in := NewInflights(10)
   172  	for i := 0; i < 10; i++ {
   173  		in.Add(uint64(i))
   174  	}
   175  
   176  	in.FreeFirstOne()
   177  
   178  	wantIn := &Inflights{
   179  		start: 1,
   180  		count: 9,
   181  		size:  10,
   182  		//                  ↓------------------------
   183  		buffer: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   184  	}
   185  
   186  	if !reflect.DeepEqual(in, wantIn) {
   187  		t.Fatalf("in = %+v, want %+v", in, wantIn)
   188  	}
   189  }
   190  

View as plain text