-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathbalancer_test.go
More file actions
92 lines (77 loc) · 2 KB
/
balancer_test.go
File metadata and controls
92 lines (77 loc) · 2 KB
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
package main
import (
"bytes"
"fmt"
"net/http"
"net/url"
"os"
"testing"
"time"
)
var testApiUrl = os.Getenv("TEST_API_URL")
func setupBalancerSuit(t *testing.T) func(t *testing.T) {
t.Log("Setup Balancer suit")
testEndpoint1, err := url.Parse(fmt.Sprintf("%s/test1/jsonrpc", testApiUrl))
if err != nil {
t.Error(err)
}
testEndpoint2, err := url.Parse(fmt.Sprintf("%s/test2/jsonrpc", testApiUrl))
if err != nil {
t.Error(err)
}
testNode1 := &Node{
Endpoint: testEndpoint1,
}
testNode2 := &Node{
Endpoint: testEndpoint2,
}
testBlockchainPools := make(map[string]*NodePool)
testBlockchainPools["test"] = &NodePool{
NodesMap: make(map[string][]*Node),
}
testBlockchainPools["test"].NodesMap["tag_1"] = append(
testBlockchainPools["test"].NodesMap["tag_1"], testNode1,
)
testBlockchainPools["test"].NodesMap["tag_2"] = append(
testBlockchainPools["test"].NodesMap["tag_2"], testNode2,
)
testBlockchainPools["test"].NodesSet = append(
testBlockchainPools["test"].NodesSet, testNode1, testNode2,
)
blockchainPools = make(map[string]*NodePool)
blockchainPools = testBlockchainPools
clientPool = make(map[string]ClientPool)
return func(t *testing.T) {
t.Log("Teardown suit")
}
}
func TestHealthCheck(t *testing.T) {
teardownSuit := setupBalancerSuit(t)
defer teardownSuit(t)
makeRequestToNode := func(node *Node) {
alive := false
httpClient := http.Client{Timeout: NB_HEALTH_CHECK_CALL_TIMEOUT}
resp, err := httpClient.Post(
node.Endpoint.String(),
"application/json",
bytes.NewBuffer([]byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}`)),
)
if err != nil {
node.UpdateNodeState(0, alive)
t.Logf("Unable to reach node: %s", node.Endpoint.Host)
}
defer resp.Body.Close()
}
NB_HEALTH_CHECK_INTERVAL = time.Millisecond * 1
c := 0
HealthCheck()
go initHealthCheck()
for c <= 1200300 {
node := GetNextNode(
blockchainPools["test"].NodesSet,
blockchainPools["test"].TopNode,
)
makeRequestToNode(node)
c++
}
}