lobby2/nodes/main_test.go

93 lines
1.9 KiB
Go
Raw Normal View History

2024-12-08 01:30:07 +00:00
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"})
nodes := np.List()
assert.Equal(t, "test", nodes[0].HostName)
assert.Contains(t, nodes[0].Labels, "mylabel")
assert.Equal(t, "test2", nodes[1].HostName)
assert.Contains(t, nodes[1].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