Increase test coverage

This commit is contained in:
Julian Kornberger 2016-11-20 18:45:18 +01:00
parent 5c2a655b11
commit 2068d2b1a1
3 changed files with 34 additions and 3 deletions

View File

@ -46,6 +46,8 @@ func main() {
} }
nodes = models.NewNodes(config) nodes = models.NewNodes(config)
nodes.Start()
if config.Respondd.Enable { if config.Respondd.Enable {
collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval) collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval)
collector = respond.NewCollector(db, nodes, collectInterval, config.Respondd.Interface) collector = respond.NewCollector(db, nodes, collectInterval, config.Respondd.Interface)

View File

@ -39,12 +39,15 @@ func NewNodes(config *Config) *Nodes {
if config.Nodes.NodesPath != "" { if config.Nodes.NodesPath != "" {
nodes.load() nodes.load()
} }
go nodes.worker()
nodes.Version = 2 nodes.Version = 2
return nodes return nodes
} }
func (nodes *Nodes) Start() {
go nodes.worker()
}
// Update a Node // Update a Node
func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node { func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node {
now := jsontime.Now() now := jsontime.Now()
@ -95,8 +98,8 @@ func (nodes *Nodes) GetNodesMini() *meshviewer.Nodes {
List: make(map[string]*meshviewer.Node), List: make(map[string]*meshviewer.Node),
Timestamp: nodes.Timestamp, Timestamp: nodes.Timestamp,
} }
for nodeID := range nodes.List {
for nodeID := range nodes.List {
node, _ := meshviewerNodes.List[nodeID] node, _ := meshviewerNodes.List[nodeID]
nodeOrigin := nodes.List[nodeID] nodeOrigin := nodes.List[nodeID]
if node == nil { if node == nil {

View File

@ -49,7 +49,7 @@ func TestLoadAndSave(t *testing.T) {
config := &Config{} config := &Config{}
config.Nodes.NodesPath = "testdata/nodes.json" config.Nodes.NodesPath = "testdata/nodes.json"
nodes := &Nodes{config: config} nodes := NewNodes(config)
nodes.load() nodes.load()
tmpfile, _ := ioutil.TempFile("/tmp", "nodes") tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
@ -73,3 +73,29 @@ func TestUpdateNodes(t *testing.T) {
assert.Equal(1, len(nodes.List)) assert.Equal(1, len(nodes.List))
} }
func TestGlobalStats(t *testing.T) {
stats := createTestNodes().GlobalStats()
assert := assert.New(t)
assert.EqualValues(uint32(1), stats.Nodes)
assert.EqualValues(uint32(23), stats.Clients)
}
func TestNodesMini(t *testing.T) {
mini := createTestNodes().GetNodesMini()
assert := assert.New(t)
assert.Equal(1, len(mini.List))
}
func createTestNodes() *Nodes {
nodes := NewNodes(&Config{})
res := &data.ResponseData{
Statistics: &data.Statistics{},
}
res.Statistics.Clients.Total = 23
nodes.Update("abcdef012345", res)
return nodes
}