github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/utils/hedge_test.go (about) 1 // Copyright 2023 LiveKit, Inc. 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 utils 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/require" 23 "go.uber.org/atomic" 24 ) 25 26 func TestHedgeCall(t *testing.T) { 27 var attempts atomic.Uint32 28 res, err := HedgeCall(context.Background(), HedgeParams[uint32]{ 29 Timeout: 200 * time.Millisecond, 30 RetryDelay: 50 * time.Millisecond, 31 MaxAttempts: 2, 32 Func: func(context.Context) (uint32, error) { 33 n := attempts.Add(1) 34 time.Sleep(75 * time.Millisecond) 35 return n, nil 36 }, 37 }) 38 require.NoError(t, err) 39 require.EqualValues(t, 1, res) 40 require.EqualValues(t, 2, attempts.Load()) 41 }