forked from gateway-fm/service-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices_list_test.go
78 lines (62 loc) · 1.73 KB
/
services_list_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
package pool
import (
"fmt"
"math"
"testing"
"time"
"github.com/gateway-fm/prover-pool-lib/service"
)
func TestServicesListShuffle(t *testing.T) {
numServices := 20
numTries := 50000
numServicesToPick := 200 // how many we pick for one try
threshold := 0.1
duration, _ := time.ParseDuration("5s")
var services []service.IService
for i := 1; i < numServices; i++ {
srv := newHealthyService(fmt.Sprintf("https://%dgateway.fm", i))
services = append(services, srv)
}
var selectedServices []string
for i := 1; i < numTries; i++ {
srvList := NewServicesList("name", &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: duration,
ChecksInterval: duration,
})
for _, srv := range services {
srvList.Add(srv)
}
srvList.Shuffle()
for j := 1; j < numServicesToPick; j++ {
selectedServices = append(selectedServices, srvList.Next().ID())
}
}
selectionFrequency := make(map[string]int)
for _, srvID := range selectedServices {
selectionFrequency[srvID] = selectionFrequency[srvID] + 1
}
expected := selectionFrequency[services[0].ID()]
for _, count := range selectionFrequency {
if math.Abs(float64(count-expected))/float64(expected) > threshold {
t.Errorf("selection frequencies are different more than a threshold")
}
}
}
func TestServicesListTryUp(t *testing.T) {
list := NewServicesList("testServicesList", &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: 1 * time.Second,
ChecksInterval: 1 * time.Second,
})
srv := &healthyService{0, &service.BaseService{}}
list.Add(srv)
list.FromHealthyToJail(srv.ID())
if list.CountAll() != 1 {
t.Errorf("unexpected num of services in list")
}
list.TryUpService(srv, 0)
if list.Next() == nil {
t.Errorf("unexpected no healthy services")
}
}