1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package health
import (
"context"
"net/http"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithPeriodicCheckConfig(t *testing.T) {
// Arrange
expectedName := "test"
cfg := checkerConfig{checks: map[string]*Check{}}
interval := 5 * time.Second
initialDelay := 1 * time.Minute
check := Check{Name: expectedName, updateInterval: interval, initialDelay: initialDelay}
// Act
WithPeriodicCheck(interval, initialDelay, check)(&cfg)
// Assert
assert.Equal(t, 1, len(cfg.checks))
assert.True(t, reflect.DeepEqual(check, *cfg.checks[expectedName]))
}
func TestWithCheckConfig(t *testing.T) {
// Arrange
expectedName := "test"
cfg := checkerConfig{checks: map[string]*Check{}}
check := Check{Name: "test"}
// Act
WithCheck(check)(&cfg)
// Assert
require.Equal(t, 1, len(cfg.checks))
assert.True(t, reflect.DeepEqual(&check, cfg.checks[expectedName]))
}
func TestWithCacheDurationConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
duration := 5 * time.Hour
// Act
WithCacheDuration(duration)(&cfg)
// Assert
assert.Equal(t, duration, cfg.cacheTTL)
}
func TestWithDisabledCacheConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
// Act
WithDisabledCache()(&cfg)
// Assert
assert.Equal(t, 0*time.Second, cfg.cacheTTL)
}
func TestWithTimeoutStartConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
// Act
WithTimeout(5 * time.Hour)(&cfg)
// Assert
assert.Equal(t, 5*time.Hour, cfg.timeout)
}
func TestWithMaxErrorMessageLengthConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
// Act
WithMaxErrorMessageLength(300)(&cfg)
// Assert
assert.Equal(t, uint(300), cfg.maxErrMsgLen)
}
func TestWithDisabledDetailsConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
// Act
WithDisabledDetails()(&cfg)
// Assert
assert.Equal(t, true, cfg.detailsDisabled)
}
func TestWithMiddlewareConfig(t *testing.T) {
// Arrange
cfg := handlerConfig{}
mw := func(MiddlewareFunc) MiddlewareFunc {
return func(r *http.Request) CheckerResult {
return CheckerResult{StatusUp, nil}
}
}
// Act
WithMiddleware(mw)(&cfg)
// Assert
assert.Equal(t, 1, len(cfg.middleware))
}
func TestWithInterceptorConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
interceptor := func(InterceptorFunc) InterceptorFunc {
return func(ctx context.Context, name string, state CheckState) CheckState {
return CheckState{}
}
}
// Act
WithInterceptors(interceptor)(&cfg)
// Assert
assert.Equal(t, 1, len(cfg.interceptors))
}
func TestWithResultWriterConfig(t *testing.T) {
// Arrange
cfg := handlerConfig{}
w := resultWriterMock{}
// Act
WithResultWriter(&w)(&cfg)
// Assert
assert.Equal(t, &w, cfg.resultWriter)
}
func TestWithStatusChangeListenerConfig(t *testing.T) {
// Arrange
cfg := checkerConfig{}
// Act
// Use of non standard AvailabilityStatus codes.
WithStatusListener(func(ctx context.Context, state CheckerState) {})(&cfg)
// Assert
assert.NotNil(t, cfg.statusChangeListener)
// Not possible in Go to compare functions.
}
func TestNewWithDefaults(t *testing.T) {
// Arrange
configApplied := false
opt := func(config *checkerConfig) { configApplied = true }
// Act
checker := NewChecker(opt)
// Assert
ckr := checker.(*defaultChecker)
assert.Equal(t, 1*time.Second, ckr.cfg.cacheTTL)
assert.Equal(t, 30*time.Second, ckr.cfg.timeout)
assert.Equal(t, uint(500), ckr.cfg.maxErrMsgLen)
assert.True(t, configApplied)
}
func TestNewCheckerWithDefaults(t *testing.T) {
// Arrange
configApplied := false
opt := func(config *checkerConfig) { configApplied = true }
// Act
checker := NewChecker(opt)
// Assert
ckr := checker.(*defaultChecker)
assert.Equal(t, 1*time.Second, ckr.cfg.cacheTTL)
assert.Equal(t, 30*time.Second, ckr.cfg.timeout)
assert.Equal(t, uint(500), ckr.cfg.maxErrMsgLen)
assert.True(t, configApplied)
}
func TestCheckerAutostartConfig(t *testing.T) {
// Arrange + Act
c := NewChecker()
// Assert
assert.True(t, c.IsStarted())
}
func TestCheckerAutostartDisabledConfig(t *testing.T) {
// Arrange
c := NewChecker(WithDisabledAutostart())
// Assert
assert.False(t, c.IsStarted())
}
|