-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathnode_test.go
84 lines (64 loc) · 2.17 KB
/
node_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
package topology
import (
"context"
"errors"
"net/url"
"testing"
"time"
"github.com/contentsquare/chproxy/internal/heartbeat"
"github.com/stretchr/testify/assert"
)
var _ heartbeat.HeartBeat = &mockHeartbeat{}
type mockHeartbeat struct {
interval time.Duration
err error
}
func (hb *mockHeartbeat) Interval() time.Duration {
return hb.interval
}
func (hb *mockHeartbeat) IsHealthy(ctx context.Context, addr string) error {
return hb.err
}
func TestPenalize(t *testing.T) {
node := NewNode(&url.URL{Host: "127.0.0.1"}, nil, "test", "test")
expectedLoad := uint32(0)
assert.Equal(t, expectedLoad, node.CurrentLoad(), "got running queries %d; expected %d", node.CurrentLoad(), expectedLoad)
node.Penalize()
expectedLoad = uint32(DefaultPenaltySize)
assert.Equal(t, expectedLoad, node.CurrentLoad(), "got running queries %d; expected %d", node.CurrentLoad(), expectedLoad)
// do more penalties than `penaltyMaxSize` allows
max := int(DefaultMaxSize/DefaultPenaltySize) * 2
for i := 0; i < max; i++ {
node.Penalize()
}
expectedLoad = uint32(DefaultMaxSize)
assert.Equal(t, expectedLoad, node.CurrentLoad(), "got running queries %d; expected %d", node.CurrentLoad(), expectedLoad)
// Still allow connections to increase.
node.IncrementConnections()
expectedLoad++
assert.Equal(t, expectedLoad, node.CurrentLoad(), "got running queries %d; expected %d", node.CurrentLoad(), expectedLoad)
}
func TestStartHeartbeat(t *testing.T) {
hb := &mockHeartbeat{
interval: 10 * time.Millisecond,
err: nil,
}
done := make(chan struct{})
defer close(done)
node := NewNode(&url.URL{Host: "127.0.0.1"}, hb, "test", "test")
// Node is eventually active after start.
go node.StartHeartbeat(done)
assert.Eventually(t, func() bool {
return node.IsActive()
}, time.Second, 100*time.Millisecond)
// change heartbeat to error, node eventually becomes inactive.
hb.err = errors.New("failed connection")
assert.Eventually(t, func() bool {
return !node.IsActive()
}, time.Second, 100*time.Millisecond)
// If error is removed node becomes active again.
hb.err = nil
assert.Eventually(t, func() bool {
return node.IsActive()
}, time.Second, 100*time.Millisecond)
}