moonstream/nodes/node_balancer/cmd/clients_test.go

89 wiersze
2.4 KiB
Go
Czysty Zwykły widok Historia

package cmd
import (
"reflect"
"testing"
"time"
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
)
2022-01-14 18:10:23 +00:00
func TestAddClientNode(t *testing.T) {
var cases = []struct {
clients map[string]*Client
expected string
}{
{map[string]*Client{"1": {Blockchain: "ethereum", Node: &Node{Alive: true}}}, "1"},
}
for _, c := range cases {
clientPool.Client = make(map[string]*Client)
for id, client := range c.clients {
clientPool.AddClientNode(id, client.Blockchain, client.Node)
}
for id := range clientPool.Client {
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) {
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},
2022-01-14 14:14:48 +00:00
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", &Node{Alive: true}},
{map[string]*Client{"2": {Blockchain: "polygon", LastCallTs: ts, Node: &Node{Alive: true}}}, "1", nil},
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE, Node: &Node{Alive: true}}}, "1", nil},
}
for _, c := range cases {
2022-01-14 14:14:48 +00:00
clientPool.Client = make(map[string]*Client)
for id, client := range c.clients {
clientPool.Client[id] = client
}
2022-01-14 14:14:48 +00:00
clientNode := clientPool.GetClientNode(c.id)
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) {
ts := time.Now().Unix()
var cases = []struct {
2022-01-14 14:14:48 +00:00
clients map[string]*Client
expected string
}{
2022-01-14 14:14:48 +00:00
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE}}, ""},
{map[string]*Client{"1": {Blockchain: "ethereum", LastCallTs: ts}}, "1"},
{map[string]*Client{
"1": {Blockchain: "ethereum", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE},
"2": {Blockchain: "polygon", LastCallTs: ts - configs.NB_CLIENT_NODE_KEEP_ALIVE - 10},
"3": {Blockchain: "stellar", LastCallTs: ts},
}, "3"},
}
for _, c := range cases {
2022-01-14 14:14:48 +00:00
clientPool.Client = make(map[string]*Client)
for id, client := range c.clients {
clientPool.Client[id] = client
}
2022-01-14 14:14:48 +00:00
clientPool.CleanInactiveClientNodes()
2022-01-14 18:10:23 +00:00
for id := range clientPool.Client {
if id != c.expected {
2022-01-14 14:14:48 +00:00
t.Log("Wrong client was removed")
t.Fatal()
}
}
}
}