Fixed client tests and small changes

pull/786/head
kompotkot 2023-05-16 11:07:24 +00:00
rodzic 363388857b
commit 9da72eac44
5 zmienionych plików z 55 dodań i 12 usunięć

Wyświetl plik

@ -11,6 +11,8 @@ go build -o nodebalancer .
## Work with nodebalancer
**IMPORTANT** Do not use flag `-debug` in production.
### add-access
Add new access for user:
@ -97,3 +99,17 @@ For Web3 providers `access_id` and `data_source` could be specified in headers
--header 'x-node-balancer-data-source: <blockchain/database>'
--header 'x-node-balancer-access-id: <access_id>'
```
## Tests
### Running all tests
```bash
/usr/local/go/bin/go test -run ^*$ github.com/bugout-dev/moonstream/nodes/node_balancer/cmd/nodebalancer -v -count=1
```
### Running specified test
```bash
/usr/local/go/bin/go test -run ^TestCleanInactiveClientNodes$ github.com/bugout-dev/moonstream/nodes/node_balancer/cmd/nodebalancer -v -count=1
```

Wyświetl plik

@ -206,7 +206,9 @@ func (bpool *BlockchainPool) HealthCheck() {
if err != nil {
n.UpdateNodeState(0, alive)
log.Printf("Unable to reach node: %s", n.Endpoint.Host)
resp.Body.Close()
if resp != nil {
resp.Body.Close()
}
continue
}
@ -214,7 +216,9 @@ func (bpool *BlockchainPool) HealthCheck() {
if err != nil {
n.UpdateNodeState(0, alive)
log.Printf("Unable to parse response from %s node, err %v", n.Endpoint.Host, err)
resp.Body.Close()
if resp.Body != nil {
resp.Body.Close()
}
continue
}
resp.Body.Close()

Wyświetl plik

@ -7,7 +7,20 @@ import (
"time"
)
func setupSuit(t *testing.T) func(t *testing.T) {
t.Log("Setup suit")
supportedBlockchains = map[string]bool{"ethereum": true}
return func(t *testing.T) {
t.Log("Teardown suit")
}
}
func TestAddClientNode(t *testing.T) {
teardownSuit := setupSuit(t)
defer teardownSuit(t)
var cases = []struct {
clients map[string]*Client
expected string
@ -16,10 +29,12 @@ func TestAddClientNode(t *testing.T) {
}
for _, c := range cases {
CreateClientPools()
cpool := GetClientPool("ethereum")
for id, client := range c.clients {
ethereumClientPool.AddClientNode(id, client.Node)
cpool.AddClientNode(id, client.Node)
}
for id := range ethereumClientPool.Client {
for id := range cpool.Client {
if id != c.expected {
t.Log("Wrong client was added")
t.Fatal()
@ -29,6 +44,9 @@ func TestAddClientNode(t *testing.T) {
}
func TestGetClientNode(t *testing.T) {
teardownSuit := setupSuit(t)
defer teardownSuit(t)
ts := time.Now().Unix()
var cases = []struct {
@ -39,15 +57,16 @@ func TestGetClientNode(t *testing.T) {
{map[string]*Client{}, "1", nil},
{map[string]*Client{"1": {LastCallTs: ts, Node: &Node{Alive: true}}}, "1", &Node{Alive: true}},
{map[string]*Client{"2": {LastCallTs: ts, Node: &Node{Alive: true}}}, "1", nil},
{map[string]*Client{"1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil},
}
for _, c := range cases {
CreateClientPools()
cpool := GetClientPool("ethereum")
for id, client := range c.clients {
ethereumClientPool.Client[id] = client
cpool.AddClientNode(id, client.Node)
}
clientNode := ethereumClientPool.GetClientNode(c.id)
clientNode := cpool.GetClientNode(c.id)
if !reflect.DeepEqual(clientNode, c.expected) {
t.Log("Wrong node returned")
t.Fatal()
@ -56,6 +75,9 @@ func TestGetClientNode(t *testing.T) {
}
func TestCleanInactiveClientNodes(t *testing.T) {
teardownSuit := setupSuit(t)
defer teardownSuit(t)
ts := time.Now().Unix()
var cases = []struct {
@ -72,12 +94,14 @@ func TestCleanInactiveClientNodes(t *testing.T) {
}
for _, c := range cases {
CreateClientPools()
cpool := GetClientPool("ethereum")
for id, client := range c.clients {
ethereumClientPool.Client[id] = client
cpool.Client[id] = client
}
ethereumClientPool.CleanInactiveClientNodes()
for id := range ethereumClientPool.Client {
cpool.CleanInactiveClientNodes()
for id := range cpool.Client {
if id != c.expected {
t.Log("Wrong client was removed")
t.Fatal()

Wyświetl plik

@ -20,7 +20,6 @@ var (
supportedBlockchains map[string]bool
// Bugout and application configuration
BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL")
BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5
NB_APPLICATION_ID = os.Getenv("NB_APPLICATION_ID")
NB_CONTROLLER_TOKEN = os.Getenv("NB_CONTROLLER_TOKEN")

Wyświetl plik

@ -1,5 +1,5 @@
# Required environment variables for load balancer
export BUGOUT_AUTH_URL="https://auth.bugout.dev"
export BUGOUT_BROOD_URL="https://auth.bugout.dev"
export NB_APPLICATION_ID="<application_id_to_controll_access>"
export NB_CONTROLLER_TOKEN="<token_of_controller_user>"
export NB_CONTROLLER_ACCESS_ID="<controller_access_id_for_internal_crawlers>"