github.com/uber/kraken@v0.1.4/lib/healthcheck/passive_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  	"testing"
    18  	"time"
    19  
    20  	"github.com/uber/kraken/utils/stringset"
    21  
    22  	"github.com/andres-erbsen/clock"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestPassiveFilterUnhealthy(t *testing.T) {
    27  	require := require.New(t)
    28  
    29  	clk := clock.NewMock()
    30  
    31  	f := NewPassiveFilter(
    32  		PassiveFilterConfig{Fails: 3, FailTimeout: 10 * time.Second},
    33  		clk)
    34  
    35  	x := "x:80"
    36  	y := "y:80"
    37  	s := stringset.New(x, y)
    38  
    39  	require.Equal(stringset.New(x, y), f.Run(s))
    40  
    41  	for i := 0; i < 3; i++ {
    42  		f.Failed(x)
    43  	}
    44  
    45  	require.Equal(stringset.New(y), f.Run(s))
    46  }
    47  
    48  func TestPassiveFilterFailTimeout(t *testing.T) {
    49  	require := require.New(t)
    50  
    51  	clk := clock.NewMock()
    52  
    53  	f := NewPassiveFilter(
    54  		PassiveFilterConfig{Fails: 3, FailTimeout: 10 * time.Second},
    55  		clk)
    56  
    57  	x := "x:80"
    58  	y := "y:80"
    59  	s := stringset.New(x, y)
    60  
    61  	f.Failed(x)
    62  	f.Failed(x)
    63  
    64  	clk.Add(11 * time.Second)
    65  
    66  	f.Failed(x)
    67  
    68  	require.Equal(stringset.New(x, y), f.Run(s))
    69  }
    70  
    71  func TestPassiveFilterFailTimeoutAfterUnhealthy(t *testing.T) {
    72  	require := require.New(t)
    73  
    74  	clk := clock.NewMock()
    75  
    76  	f := NewPassiveFilter(
    77  		PassiveFilterConfig{Fails: 3, FailTimeout: 10 * time.Second},
    78  		clk)
    79  
    80  	x := "x:80"
    81  	y := "y:80"
    82  	s := stringset.New(x, y)
    83  
    84  	for i := 0; i < 3; i++ {
    85  		f.Failed(x)
    86  	}
    87  
    88  	require.Equal(stringset.New(y), f.Run(s))
    89  
    90  	clk.Add(5 * time.Second)
    91  
    92  	// Stil unhealthy...
    93  	require.Equal(stringset.New(y), f.Run(s))
    94  
    95  	clk.Add(6 * time.Second)
    96  
    97  	// Timeout has now elapsed, host is healthy again.
    98  	require.Equal(stringset.New(x, y), f.Run(s))
    99  }