github.com/uber/kraken@v0.1.4/lib/healthcheck/filter_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, 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 package healthcheck 15 16 import ( 17 "context" 18 "errors" 19 "testing" 20 "time" 21 22 "github.com/uber/kraken/mocks/lib/healthcheck" 23 "github.com/uber/kraken/utils/stringset" 24 25 "github.com/golang/mock/gomock" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestFilterCheckErrors(t *testing.T) { 30 require := require.New(t) 31 32 ctrl := gomock.NewController(t) 33 defer ctrl.Finish() 34 35 checker := mockhealthcheck.NewMockChecker(ctrl) 36 37 x := "x:80" 38 y := "y:80" 39 40 f := NewFilter(FilterConfig{Fails: 1, Passes: 1}, checker) 41 42 checker.EXPECT().Check(gomock.Any(), x).Return(nil) 43 checker.EXPECT().Check(gomock.Any(), y).Return(nil) 44 45 require.Equal(stringset.New(x, y), f.Run(stringset.New(x, y))) 46 47 checker.EXPECT().Check(gomock.Any(), x).Return(errors.New("some error")) 48 checker.EXPECT().Check(gomock.Any(), y).Return(errors.New("some error")) 49 50 require.Empty(f.Run(stringset.New(x, y))) 51 } 52 53 func TestFilterCheckTimeout(t *testing.T) { 54 require := require.New(t) 55 56 ctrl := gomock.NewController(t) 57 defer ctrl.Finish() 58 59 checker := mockhealthcheck.NewMockChecker(ctrl) 60 61 x := "x:80" 62 y := "y:80" 63 64 f := NewFilter(FilterConfig{Fails: 1, Passes: 1, Timeout: time.Second}, checker) 65 66 checker.EXPECT().Check(gomock.Any(), x).Return(nil) 67 checker.EXPECT().Check(gomock.Any(), y).DoAndReturn(func(context.Context, string) error { 68 time.Sleep(2 * time.Second) 69 return nil 70 }) 71 72 require.Equal(stringset.New(x), f.Run(stringset.New(x, y))) 73 } 74 75 func TestFilterSingleHostAlwaysHealthy(t *testing.T) { 76 require := require.New(t) 77 78 ctrl := gomock.NewController(t) 79 defer ctrl.Finish() 80 81 checker := mockhealthcheck.NewMockChecker(ctrl) 82 83 x := "x:80" 84 85 f := NewFilter(FilterConfig{Fails: 1, Passes: 1}, checker) 86 87 // No health checks actually run since only single host is used. 88 require.Equal(stringset.New(x), f.Run(stringset.New(x))) 89 } 90 91 func TestFilterNewHostsStartAsHealthy(t *testing.T) { 92 require := require.New(t) 93 94 ctrl := gomock.NewController(t) 95 defer ctrl.Finish() 96 97 checker := mockhealthcheck.NewMockChecker(ctrl) 98 99 x := "x:80" 100 y := "y:80" 101 102 f := NewFilter(FilterConfig{Fails: 2, Passes: 2}, checker) 103 104 checker.EXPECT().Check(gomock.Any(), x).Return(errors.New("some error")).Times(2) 105 checker.EXPECT().Check(gomock.Any(), y).Return(errors.New("some error")).Times(2) 106 107 // Even though health checks are failing, since Fails=2, it takes two Runs 108 // for the unhealthy addrs to be filtered out. 109 require.Equal(stringset.New(x, y), f.Run(stringset.New(x, y))) 110 require.Empty(f.Run(stringset.New(x, y))) 111 }