vitess.io/vitess@v0.16.2/go/vt/throttler/interval_history_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 "strings" 21 "testing" 22 "time" 23 ) 24 25 func TestIntervalHistory_AverageIncludesPartialIntervals(t *testing.T) { 26 // average() should include intervals which aren't fully covered by from and 27 // to at least partially. 28 h := newIntervalHistory(10, 1*time.Second) 29 30 h.add(record{sinceZero(0 * time.Second), 10000000}) 31 h.add(record{sinceZero(1 * time.Second), 1000}) 32 h.add(record{sinceZero(2 * time.Second), 2000}) 33 h.add(record{sinceZero(3 * time.Second), 10000000}) 34 // Rate within [1s, 2s) = 1000 and within [2s, 3s) = 2000 = average of 1500 35 want := 1500.0 36 if got := h.average(sinceZero(1500*time.Millisecond), sinceZero(2500*time.Millisecond)); got != want { 37 t.Errorf("average(1.5s, 2.5s) = %v, want = %v", got, want) 38 } 39 } 40 41 func TestIntervalHistory_AverageRangeSmallerThanInterval(t *testing.T) { 42 h := newIntervalHistory(10, 1*time.Second) 43 44 h.add(record{sinceZero(0 * time.Second), 10000}) 45 want := 10000.0 46 if got := h.average(sinceZero(250*time.Millisecond), sinceZero(750*time.Millisecond)); got != want { 47 t.Errorf("average(0.25s, 0.75s) = %v, want = %v", got, want) 48 } 49 } 50 51 func TestIntervalHistory_GapsCountedAsZero(t *testing.T) { 52 h := newIntervalHistory(10, 1*time.Second) 53 54 h.add(record{sinceZero(0 * time.Second), 1000}) 55 h.add(record{sinceZero(3 * time.Second), 1000}) 56 57 want := 500.0 58 if got := h.average(sinceZero(0*time.Second), sinceZero(4*time.Second)); got != want { 59 t.Errorf("average(0s, 4s) = %v, want = %v", got, want) 60 } 61 } 62 63 func TestIntervalHistory_AddNoDuplicateInterval(t *testing.T) { 64 defer func() { 65 r := recover() 66 67 if r == nil { 68 t.Fatal("add() did not panic") 69 } 70 want := "BUG: cannot add record because it is already covered by a previous entry" 71 if !strings.Contains(r.(string), want) { 72 t.Fatalf("add() did panic for the wrong reason: got = %v, want = %v", r, want) 73 } 74 }() 75 76 h := newIntervalHistory(10, 1*time.Second) 77 78 h.add(record{sinceZero(0 * time.Second), 1000}) 79 h.add(record{sinceZero(100 * time.Millisecond), 1000}) 80 } 81 82 func TestIntervalHistory_RecordDoesNotStartAtInterval(t *testing.T) { 83 defer func() { 84 r := recover() 85 86 if r == nil { 87 t.Fatal("add() did not panic") 88 } 89 want := "BUG: cannot add record because it does not start at the beginning of the interval" 90 if !strings.Contains(r.(string), want) { 91 t.Fatalf("add() did panic for the wrong reason: got = %v, want = %v", r, want) 92 } 93 }() 94 95 h := newIntervalHistory(1, 1*time.Second) 96 97 h.add(record{sinceZero(10 * time.Millisecond), 1000}) 98 }