vitess.io/vitess@v0.16.2/go/vt/vterrors/last_error_test.go (about) 1 /* 2 Copyright 2022 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 vterrors 18 19 import ( 20 "fmt" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 const shortWait = 1 * time.Millisecond 28 const longWait = 150 * time.Millisecond 29 const maxTimeInError = 100 * time.Millisecond 30 31 // TestLastErrorZeroMaxTime tests maxTimeInError = 0, should always retry 32 func TestLastErrorZeroMaxTime(t *testing.T) { 33 le := NewLastError("test", 0) 34 err1 := fmt.Errorf("error1") 35 le.Record(err1) 36 require.True(t, le.ShouldRetry()) 37 time.Sleep(shortWait) 38 require.True(t, le.ShouldRetry()) 39 time.Sleep(longWait) 40 require.True(t, le.ShouldRetry()) 41 } 42 43 // TestLastErrorNoError ensures that an uninitialized lastError always retries 44 func TestLastErrorNoError(t *testing.T) { 45 le := NewLastError("test", maxTimeInError) 46 require.True(t, le.ShouldRetry()) 47 err1 := fmt.Errorf("error1") 48 le.Record(err1) 49 require.True(t, le.ShouldRetry()) 50 le.Record(nil) 51 require.True(t, le.ShouldRetry()) 52 } 53 54 // TestLastErrorOneError validates that we retry an error if happening within the maxTimeInError, but not after 55 func TestLastErrorOneError(t *testing.T) { 56 le := NewLastError("test", maxTimeInError) 57 err1 := fmt.Errorf("error1") 58 le.Record(err1) 59 require.True(t, le.ShouldRetry()) 60 time.Sleep(shortWait) 61 require.True(t, le.ShouldRetry()) 62 time.Sleep(shortWait) 63 require.True(t, le.ShouldRetry()) 64 time.Sleep(longWait) 65 require.False(t, le.ShouldRetry()) 66 } 67 68 // TestLastErrorRepeatedError confirms that if same error is repeated we don't retry 69 // unless it happens after maxTimeInError 70 func TestLastErrorRepeatedError(t *testing.T) { 71 le := NewLastError("test", maxTimeInError) 72 err1 := fmt.Errorf("error1") 73 le.Record(err1) 74 require.True(t, le.ShouldRetry()) 75 for i := 1; i < 10; i++ { 76 le.Record(err1) 77 time.Sleep(shortWait) 78 } 79 require.True(t, le.ShouldRetry()) 80 81 // same error happens after maxTimeInError, so it should retry 82 time.Sleep(longWait) 83 require.False(t, le.ShouldRetry()) 84 le.Record(err1) 85 require.True(t, le.ShouldRetry()) 86 }