2017-04-10 18:54:12 +02:00
|
|
|
package runtime
|
2016-03-20 18:30:44 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2016-10-08 11:52:11 +02:00
|
|
|
"time"
|
2016-03-20 18:30:44 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-03 16:19:35 +01:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
2016-03-20 18:30:44 +01:00
|
|
|
)
|
|
|
|
|
2016-10-08 11:52:11 +02:00
|
|
|
func TestExpire(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
config := &Config{}
|
2017-01-29 21:04:10 +01:00
|
|
|
config.Nodes.OfflineAfter.Duration = time.Minute * 10
|
2017-01-29 18:30:08 +01:00
|
|
|
config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6
|
2016-10-08 11:52:11 +02: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?
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 2)
|
2016-10-08 11:52:11 +02:00
|
|
|
assert.Nil(nodes.List["expire"])
|
|
|
|
|
|
|
|
// one offline?
|
|
|
|
assert.NotNil(nodes.List["offline"])
|
2017-04-10 18:54:12 +02:00
|
|
|
assert.False(nodes.List["offline"].Online)
|
2016-10-08 11:52:11 +02:00
|
|
|
|
|
|
|
// one online?
|
|
|
|
assert.NotNil(nodes.List["online"])
|
2017-04-10 18:54:12 +02:00
|
|
|
assert.True(nodes.List["online"].Online)
|
2016-10-08 11:52:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 18:30:44 +01:00
|
|
|
func TestLoadAndSave(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
config := &Config{}
|
2017-01-29 20:40:46 +01:00
|
|
|
config.Nodes.StatePath = "testdata/nodes.json"
|
2016-03-20 18:30:44 +01:00
|
|
|
|
2016-11-20 18:45:18 +01:00
|
|
|
nodes := NewNodes(config)
|
2016-03-20 18:30:44 +01:00
|
|
|
nodes.load()
|
|
|
|
|
|
|
|
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
|
2017-04-10 18:54:12 +02:00
|
|
|
SaveJSON(nodes, tmpfile.Name())
|
2016-03-20 18:30:44 +01:00
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 1)
|
2016-03-20 18:30:44 +01:00
|
|
|
}
|
2016-03-21 12:26:08 +01:00
|
|
|
|
|
|
|
func TestUpdateNodes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
nodes := &Nodes{List: make(map[string]*Node)}
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 0)
|
2016-03-21 12:26:08 +01:00
|
|
|
|
|
|
|
res := &data.ResponseData{
|
|
|
|
Neighbours: &data.Neighbours{},
|
|
|
|
Statistics: &data.Statistics{},
|
|
|
|
NodeInfo: &data.NodeInfo{},
|
|
|
|
}
|
|
|
|
nodes.Update("abcdef012345", res)
|
|
|
|
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 1)
|
2016-03-21 12:26:08 +01:00
|
|
|
}
|