2016-03-20 17:30:44 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2016-10-08 09:52:11 +00:00
|
|
|
"time"
|
2016-03-20 17:30:44 +00:00
|
|
|
|
2016-03-21 11:26:08 +00:00
|
|
|
"github.com/FreifunkBremen/respond-collector/data"
|
2016-03-20 17:30:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-10-08 09:52:11 +00:00
|
|
|
func TestExpire(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
config := &Config{}
|
2017-01-29 17:30:08 +00:00
|
|
|
config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6
|
2016-10-08 09:52:11 +00:00
|
|
|
nodes := &Nodes{
|
|
|
|
config: config,
|
|
|
|
List: make(map[string]*Node),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes.Update("expire", &data.ResponseData{}) // should expire
|
|
|
|
nodes.Update("offline", &data.ResponseData{}) // should become offline
|
|
|
|
nodes.Update("online", &data.ResponseData{}) // should stay online
|
|
|
|
|
|
|
|
expire := nodes.List["expire"]
|
|
|
|
expire.Lastseen = expire.Lastseen.Add((-6 * time.Hour * 24) - time.Minute)
|
|
|
|
offline := nodes.List["offline"]
|
|
|
|
offline.Lastseen = offline.Lastseen.Add((-6 * time.Hour * 24) + time.Minute)
|
|
|
|
|
|
|
|
nodes.expire()
|
|
|
|
|
|
|
|
// one expired?
|
|
|
|
assert.Equal(2, len(nodes.List))
|
|
|
|
assert.Nil(nodes.List["expire"])
|
|
|
|
|
|
|
|
// one offline?
|
|
|
|
assert.NotNil(nodes.List["offline"])
|
|
|
|
assert.False(nodes.List["offline"].Flags.Online)
|
|
|
|
|
|
|
|
// one online?
|
|
|
|
assert.NotNil(nodes.List["online"])
|
|
|
|
assert.True(nodes.List["online"].Flags.Online)
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:30:44 +00:00
|
|
|
func TestLoadAndSave(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
config := &Config{}
|
2017-01-29 19:40:46 +00:00
|
|
|
config.Nodes.StatePath = "testdata/nodes.json"
|
2016-03-20 17:30:44 +00:00
|
|
|
|
2016-11-20 17:45:18 +00:00
|
|
|
nodes := NewNodes(config)
|
2016-03-20 17:30:44 +00:00
|
|
|
nodes.load()
|
|
|
|
|
|
|
|
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
|
|
|
|
save(nodes, tmpfile.Name())
|
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
assert.Equal(1, len(nodes.List))
|
|
|
|
}
|
2016-03-21 11:26:08 +00:00
|
|
|
|
|
|
|
func TestUpdateNodes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
nodes := &Nodes{List: make(map[string]*Node)}
|
|
|
|
assert.Equal(0, len(nodes.List))
|
|
|
|
|
|
|
|
res := &data.ResponseData{
|
|
|
|
Neighbours: &data.Neighbours{},
|
|
|
|
Statistics: &data.Statistics{},
|
|
|
|
NodeInfo: &data.NodeInfo{},
|
|
|
|
}
|
|
|
|
nodes.Update("abcdef012345", res)
|
|
|
|
|
|
|
|
assert.Equal(1, len(nodes.List))
|
|
|
|
}
|