moonstream/nodebalancer/cmd/nodebalancer/clients_test.go

111 wiersze
2.3 KiB
Go
Czysty Zwykły widok Historia

2022-07-11 14:01:10 +00:00
package main
import (
"reflect"
"testing"
"time"
)
2023-05-25 11:19:46 +00:00
func clientsSetupSuit(t *testing.T) func(t *testing.T) {
2023-05-16 11:07:24 +00:00
t.Log("Setup suit")
supportedBlockchains = map[string]bool{"ethereum": true}
return func(t *testing.T) {
t.Log("Teardown suit")
}
}
2022-01-14 18:10:23 +00:00
func TestAddClientNode(t *testing.T) {
2023-05-25 11:19:46 +00:00
teardownSuit := clientsSetupSuit(t)
2023-05-16 11:07:24 +00:00
defer teardownSuit(t)
2022-01-14 18:10:23 +00:00
var cases = []struct {
clients map[string]*Client
expected string
}{
{map[string]*Client{"1": {Node: &Node{Alive: true}}}, "1"},
2022-01-14 18:10:23 +00:00
}
for _, c := range cases {
CreateClientPools()
2023-05-16 11:07:24 +00:00
cpool := GetClientPool("ethereum")
2022-01-14 18:10:23 +00:00
for id, client := range c.clients {
2023-05-16 11:07:24 +00:00
cpool.AddClientNode(id, client.Node)
2022-01-14 18:10:23 +00:00
}
2023-05-16 11:07:24 +00:00
for id := range cpool.Client {
2022-01-14 18:10:23 +00:00
if id != c.expected {
t.Log("Wrong client was added")
t.Fatal()
}
}
}
}
2022-01-14 14:14:48 +00:00
func TestGetClientNode(t *testing.T) {
2023-05-25 11:19:46 +00:00
teardownSuit := clientsSetupSuit(t)
2023-05-16 11:07:24 +00:00
defer teardownSuit(t)
ts := time.Now().Unix()
var cases = []struct {
2022-01-14 14:14:48 +00:00
clients map[string]*Client
id string
expected *Node
}{
2022-01-14 18:10:23 +00:00
{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},
}
for _, c := range cases {
CreateClientPools()
2023-05-16 11:07:24 +00:00
cpool := GetClientPool("ethereum")
2022-01-14 14:14:48 +00:00
for id, client := range c.clients {
2023-05-16 11:07:24 +00:00
cpool.AddClientNode(id, client.Node)
}
2023-05-16 11:07:24 +00:00
clientNode := cpool.GetClientNode(c.id)
2022-01-14 14:14:48 +00:00
if !reflect.DeepEqual(clientNode, c.expected) {
t.Log("Wrong node returned")
t.Fatal()
}
}
}
2022-01-14 14:14:48 +00:00
func TestCleanInactiveClientNodes(t *testing.T) {
2023-05-25 11:19:46 +00:00
teardownSuit := clientsSetupSuit(t)
2023-05-16 11:07:24 +00:00
defer teardownSuit(t)
2022-01-14 14:14:48 +00:00
ts := time.Now().Unix()
var cases = []struct {
2022-01-14 14:14:48 +00:00
clients map[string]*Client
expected string
}{
{map[string]*Client{"1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE}}, ""},
{map[string]*Client{"1": {LastCallTs: ts}}, "1"},
2022-01-14 14:14:48 +00:00
{map[string]*Client{
"1": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE},
"2": {LastCallTs: ts - NB_CLIENT_NODE_KEEP_ALIVE - 10},
"3": {LastCallTs: ts},
2022-01-14 14:14:48 +00:00
}, "3"},
}
for _, c := range cases {
CreateClientPools()
2023-05-16 11:07:24 +00:00
cpool := GetClientPool("ethereum")
2022-01-14 14:14:48 +00:00
for id, client := range c.clients {
2023-05-16 11:07:24 +00:00
cpool.Client[id] = client
}
2023-05-16 11:07:24 +00:00
cpool.CleanInactiveClientNodes()
for id := range cpool.Client {
2022-01-14 18:10:23 +00:00
if id != c.expected {
2022-01-14 14:14:48 +00:00
t.Log("Wrong client was removed")
t.Fatal()
}
}
}
}