vitess.io/vitess@v0.16.2/go/vt/throttler/result_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package throttler
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  var (
    26  	resultIncreased = result{
    27  		Now:                          sinceZero(1234 * time.Millisecond),
    28  		RateChange:                   increasedRate,
    29  		lastRateChange:               sinceZero(1 * time.Millisecond),
    30  		OldState:                     stateIncreaseRate,
    31  		TestedState:                  stateIncreaseRate,
    32  		NewState:                     stateIncreaseRate,
    33  		OldRate:                      100,
    34  		NewRate:                      100,
    35  		Reason:                       "increased the rate",
    36  		CurrentRate:                  99,
    37  		GoodOrBad:                    goodRate,
    38  		MemorySkipReason:             "",
    39  		HighestGood:                  95,
    40  		LowestBad:                    0,
    41  		LagRecordNow:                 lagRecord(sinceZero(1234*time.Millisecond), 101, 1),
    42  		LagRecordBefore:              replicationLagRecord{},
    43  		PrimaryRate:                  99,
    44  		GuessedReplicationRate:       0,
    45  		GuessedReplicationBacklogOld: 0,
    46  		GuessedReplicationBacklogNew: 0,
    47  	}
    48  	resultDecreased = result{
    49  		Now:                          sinceZero(5000 * time.Millisecond),
    50  		RateChange:                   decreasedRate,
    51  		lastRateChange:               sinceZero(1234 * time.Millisecond),
    52  		OldState:                     stateIncreaseRate,
    53  		TestedState:                  stateDecreaseAndGuessRate,
    54  		NewState:                     stateDecreaseAndGuessRate,
    55  		OldRate:                      200,
    56  		NewRate:                      100,
    57  		Reason:                       "decreased the rate",
    58  		CurrentRate:                  200,
    59  		GoodOrBad:                    badRate,
    60  		MemorySkipReason:             "",
    61  		HighestGood:                  95,
    62  		LowestBad:                    200,
    63  		LagRecordNow:                 lagRecord(sinceZero(5000*time.Millisecond), 101, 2),
    64  		LagRecordBefore:              lagRecord(sinceZero(1234*time.Millisecond), 101, 1),
    65  		PrimaryRate:                  200,
    66  		GuessedReplicationRate:       150,
    67  		GuessedReplicationBacklogOld: 10,
    68  		GuessedReplicationBacklogNew: 20,
    69  	}
    70  	resultEmergency = result{
    71  		Now:                          sinceZero(10123 * time.Millisecond),
    72  		RateChange:                   decreasedRate,
    73  		lastRateChange:               sinceZero(5000 * time.Millisecond),
    74  		OldState:                     stateDecreaseAndGuessRate,
    75  		TestedState:                  stateEmergency,
    76  		NewState:                     stateEmergency,
    77  		OldRate:                      100,
    78  		NewRate:                      50,
    79  		Reason:                       "emergency state decreased the rate",
    80  		CurrentRate:                  100,
    81  		GoodOrBad:                    badRate,
    82  		MemorySkipReason:             "",
    83  		HighestGood:                  95,
    84  		LowestBad:                    100,
    85  		LagRecordNow:                 lagRecord(sinceZero(10123*time.Millisecond), 101, 23),
    86  		LagRecordBefore:              lagRecord(sinceZero(5000*time.Millisecond), 101, 2),
    87  		PrimaryRate:                  0,
    88  		GuessedReplicationRate:       0,
    89  		GuessedReplicationBacklogOld: 0,
    90  		GuessedReplicationBacklogNew: 0,
    91  	}
    92  )
    93  
    94  func TestResultString(t *testing.T) {
    95  	testcases := []struct {
    96  		r    result
    97  		want string
    98  	}{
    99  		{
   100  			resultIncreased,
   101  			`rate was: increased from: 100 to: 100
   102  alias: cell1-0000000101 lag: 1s
   103  last change: 1.2s rate: 99 good/bad? good skipped b/c:  good/bad: 95/0
   104  state (old/tested/new): I/I/I 
   105  lag before: n/a (n/a ago) rates (primary/replica): 99/0 backlog (old/new): 0/0
   106  reason: increased the rate`,
   107  		},
   108  		{
   109  			resultDecreased,
   110  			`rate was: decreased from: 200 to: 100
   111  alias: cell1-0000000101 lag: 2s
   112  last change: 3.8s rate: 200 good/bad? bad skipped b/c:  good/bad: 95/200
   113  state (old/tested/new): I/D/D 
   114  lag before: 1s (3.8s ago) rates (primary/replica): 200/150 backlog (old/new): 10/20
   115  reason: decreased the rate`,
   116  		},
   117  		{
   118  			resultEmergency,
   119  			`rate was: decreased from: 100 to: 50
   120  alias: cell1-0000000101 lag: 23s
   121  last change: 5.1s rate: 100 good/bad? bad skipped b/c:  good/bad: 95/100
   122  state (old/tested/new): D/E/E 
   123  lag before: 2s (5.1s ago) rates (primary/replica): 0/0 backlog (old/new): 0/0
   124  reason: emergency state decreased the rate`,
   125  		},
   126  	}
   127  
   128  	for _, tc := range testcases {
   129  		got := tc.r.String()
   130  		if got != tc.want {
   131  			t.Fatalf("record.String() = %v, want = %v for full record: %#v", got, tc.want, tc.r)
   132  		}
   133  	}
   134  }
   135  
   136  func TestResultRing(t *testing.T) {
   137  	// Test data.
   138  	r1 := result{Reason: "r1"}
   139  	r2 := result{Reason: "r2"}
   140  	r3 := result{Reason: "r3"}
   141  
   142  	rr := newResultRing(2)
   143  
   144  	// Use the ring partially.
   145  	rr.add(r1)
   146  	if got, want := rr.latestValues(), []result{r1}; !reflect.DeepEqual(got, want) {
   147  		t.Fatalf("items not correctly added to resultRing. got = %v, want = %v", got, want)
   148  	}
   149  
   150  	// Use it fully.
   151  	rr.add(r2)
   152  	if got, want := rr.latestValues(), []result{r2, r1}; !reflect.DeepEqual(got, want) {
   153  		t.Fatalf("items not correctly added to resultRing. got = %v, want = %v", got, want)
   154  	}
   155  
   156  	// Let it wrap.
   157  	rr.add(r3)
   158  	if got, want := rr.latestValues(), []result{r3, r2}; !reflect.DeepEqual(got, want) {
   159  		t.Fatalf("resultRing did not wrap correctly. got = %v, want = %v", got, want)
   160  	}
   161  }