lobby2/nodes/main_test.go
Adam Štrauch ec78765e4f
All checks were successful
Tests / test (push) Successful in 11s
Fix tests
2024-12-08 18:08:34 +01:00

93 lines
2 KiB
Go

package nodes
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var np *NodesProcessor
const testDumpFile = "nodes.json"
func TestMain(m *testing.M) {
np = NewNodesProcessor(testDumpFile, 2)
os.Exit(m.Run())
}
func TestNodesProcessor_GarbageCollect(t *testing.T) {
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
assert.Equal(t, 1, len(np.List()))
time.Sleep(5 * time.Second)
np.garbageCollect()
assert.Equal(t, 0, len(np.List()))
}
func TestNodesProcessor_Refresh(t *testing.T) {
np.Reset()
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
np.Refresh("test2", Labels{"mylabel3"}, KV{"mykey3": "my value3"})
nodeTest, _ := np.Get("test")
nodeTest2, _ := np.Get("test2")
assert.Equal(t, "test", nodeTest.HostName)
assert.Contains(t, nodeTest.Labels, "mylabel")
assert.Equal(t, "test2", nodeTest2.HostName)
assert.Contains(t, nodeTest2.Labels, "mylabel3")
}
func TestNodesProcessor_DumpLoad(t *testing.T) {
np.Reset()
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
np.Refresh("test2", Labels{"mylabel3"}, KV{"mykey3": "my value3"})
os.Remove(testDumpFile)
np.Dump()
np.Reset()
assert.Equal(t, 0, len(np.List()))
np.Load()
assert.Equal(t, 2, len(np.List()))
nodes := np.List()
assert.Len(t, nodes, 2)
}
func TestNodesProcessor_List(t *testing.T) {
np.Reset()
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
nodes := np.List()
assert.Equal(t, "test", nodes[0].HostName)
assert.Contains(t, nodes[0].Labels, "mylabel")
}
func TestNodesProcessor_Get(t *testing.T) {
np.Reset()
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
node, _ := np.Get("test")
assert.Contains(t, node.Labels, "mylabel")
}
func TestNodesProcessor_Drop(t *testing.T) {
np.Reset()
np.Refresh("test", Labels{"mylabel"}, KV{"mykey": "my value"})
node, _ := np.Get("test")
assert.Contains(t, node.Labels, "mylabel")
np.Drop("test")
assert.Zero(t, len(np.List()))
}
// TODO: TestNodesProcessor_GetMetrics