-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware_health_test.go
68 lines (58 loc) · 1.38 KB
/
middleware_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
package work
import (
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/matryer/is"
"github.com/sirupsen/logrus"
)
func Test_WithHealthCheck(t *testing.T) {
is := is.New(t)
logger := logrus.WithFields(logrus.Fields{
"method": "TestInitDefaultTopicPublisher",
})
r := gin.Default()
health := NewHealthCheck(WithEngine(r), WithHealthTicker(time.NewTicker(2*time.Millisecond)))
defer health.Close()
healthAdapter := WithHealthCheck(health, WithErrorPercentageByRequestCount(float64(.25), 3, 4))
badhandler := Adapt(func(j *Job) error {
j.Ctx.SetStatus(StatusInternalError)
return nil
}, healthAdapter)
goodhandler := Adapt(func(j *Job) error {
j.Ctx.SetStatus(StatusSuccess)
return nil
}, healthAdapter)
w := NewCommonWorker(logger)
err := w.Register(
"bad",
badhandler)
is.NoErr(err)
err = w.Register(
"good",
goodhandler)
is.NoErr(err)
err = w.Perform(
&Job{Handler: "bad"},
WithSync(true))
is.NoErr(err)
is.True(health.GetStatus() == 200)
for i := 0; i < 10; i++ {
err = w.Perform(
&Job{Handler: "bad"},
WithSync(true))
is.NoErr(err)
time.Sleep(1 * time.Millisecond)
}
t.Log(health.GetStatus())
is.True(health.GetStatus() == 500)
for i := 0; i < 3; i++ {
err = w.Perform(
&Job{Handler: "good"},
WithSync(true))
is.NoErr(err)
time.Sleep(20 * time.Millisecond)
}
t.Log(health.GetStatus())
is.True(health.GetStatus() == 200)
}