-
Notifications
You must be signed in to change notification settings - Fork 3
/
health_test.go
113 lines (101 loc) · 2.47 KB
/
health_test.go
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
package main
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCheckHealth(t *testing.T) {
ok, err := checkHealth(context.Background(), "tcp://google.com:80")
require.NoError(t, err)
require.True(t, ok)
}
func TestCheckHealthLocal(t *testing.T) {
server := http.Server{
Addr: ":8888",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
}
go server.ListenAndServe()
defer server.Close()
time.Sleep(time.Second)
ok, err := checkHealth(context.Background(), "tcp://:8888")
require.NoError(t, err)
require.True(t, ok)
}
func TestHealthGroupAllHealthy(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:80", "tcp://google.com:443"},
All: true,
Negate: false,
Retry: 0,
Interval: time.Second,
Timeout: time.Second,
}
err := g.Wait(context.Background())
require.NoError(t, err)
}
func TestHealthGroupSomeHealthy(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:80", "tcp://google.com:81"},
All: false,
Negate: false,
Retry: 0,
Interval: time.Second,
Timeout: time.Second,
}
err := g.Wait(context.Background())
require.NoError(t, err)
}
func TestHealthGroupAllUnhealthy(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:81", "tcp://google.com:82"},
All: true,
Negate: true,
Retry: 0,
Interval: time.Second,
Timeout: time.Second,
}
err := g.Wait(context.Background())
require.NoError(t, err)
}
func TestHealthGroupSomeUnhealthy(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:80", "tcp://google.com:81"},
All: false,
Negate: true,
Retry: 0,
Interval: time.Second,
Timeout: time.Second,
}
err := g.Wait(context.Background())
require.NoError(t, err)
}
func TestHealthGroupRetry(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:81"},
All: true,
Negate: false,
Retry: 1,
Interval: time.Second,
Timeout: time.Second,
}
err := g.Wait(context.Background())
require.Error(t, err)
require.ErrorContains(t, err, "wait timeout")
}
func TestHealthGroupCancel(t *testing.T) {
g := HealthGroup{
URLs: []string{"tcp://google.com:81"},
All: true,
Negate: false,
Retry: 0,
Interval: time.Second,
Timeout: time.Second,
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := g.Wait(ctx)
require.Error(t, err)
require.ErrorIs(t, err, context.Canceled)
}