github.com/blend/go-sdk@v1.20220411.3/breaker/option_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package breaker 9 10 import ( 11 "context" 12 "testing" 13 "time" 14 15 "github.com/blend/go-sdk/assert" 16 ) 17 18 func TestOptHalfOpenMaxActions(t *testing.T) { 19 assert := assert.New(t) 20 21 b := new(Breaker) 22 assert.Zero(b.HalfOpenMaxActions) 23 OptHalfOpenMaxActions(5)(b) 24 assert.Equal(5, b.HalfOpenMaxActions) 25 } 26 27 func TestOptClosedExpiryInterval(t *testing.T) { 28 assert := assert.New(t) 29 30 b := new(Breaker) 31 assert.Zero(b.ClosedExpiryInterval) 32 OptClosedExpiryInterval(5 * time.Second)(b) 33 assert.Equal(5*time.Second, b.ClosedExpiryInterval) 34 } 35 36 func TestOptOpenExpiryInterval(t *testing.T) { 37 assert := assert.New(t) 38 39 b := new(Breaker) 40 assert.Zero(b.OpenExpiryInterval) 41 OptOpenExpiryInterval(5 * time.Second)(b) 42 assert.Equal(5*time.Second, b.OpenExpiryInterval) 43 } 44 45 func TestOptConfig(t *testing.T) { 46 assert := assert.New(t) 47 48 b := new(Breaker) 49 assert.Zero(b.HalfOpenMaxActions) 50 assert.Zero(b.ClosedExpiryInterval) 51 assert.Zero(b.OpenExpiryInterval) 52 OptConfig(Config{ 53 HalfOpenMaxActions: 1, 54 ClosedExpiryInterval: 2 * time.Second, 55 OpenExpiryInterval: 3 * time.Second, 56 })(b) 57 assert.Equal(1, b.HalfOpenMaxActions) 58 assert.Equal(2*time.Second, b.ClosedExpiryInterval) 59 assert.Equal(3*time.Second, b.OpenExpiryInterval) 60 } 61 62 func TestOptOpenAction(t *testing.T) { 63 assert := assert.New(t) 64 65 b := new(Breaker) 66 assert.Nil(b.OpenAction) 67 OptOpenAction(ActionerFunc(func(_ context.Context, _ interface{}) (interface{}, error) { return nil, nil }))(b) 68 assert.NotNil(b.OpenAction) 69 } 70 71 func TestOptOnStateChange(t *testing.T) { 72 assert := assert.New(t) 73 74 b := new(Breaker) 75 assert.Nil(b.OnStateChange) 76 OptOnStateChange(func(_ context.Context, from, to State, generation int64) {})(b) 77 assert.NotNil(b.OnStateChange) 78 } 79 80 func TestOptShouldOpenProvider(t *testing.T) { 81 assert := assert.New(t) 82 83 b := new(Breaker) 84 assert.Nil(b.ShouldOpenProvider) 85 OptShouldOpenProvider(func(ctx context.Context, counts Counts) bool { return false })(b) 86 assert.NotNil(b.ShouldOpenProvider) 87 } 88 89 func TestOptNowProvider(t *testing.T) { 90 assert := assert.New(t) 91 92 b := new(Breaker) 93 assert.Nil(b.NowProvider) 94 OptNowProvider(time.Now)(b) 95 assert.NotNil(b.NowProvider) 96 }