2017-05-20 14:46:29 +02:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2017-11-14 19:33:28 +01:00
|
|
|
"sync"
|
2017-05-20 14:46:29 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testConn struct {
|
|
|
|
Output
|
2017-11-14 19:33:28 +01:00
|
|
|
countSave int
|
|
|
|
sync.Mutex
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testConn) Save(nodes *runtime.Nodes) {
|
2017-11-14 19:33:28 +01:00
|
|
|
c.Lock()
|
|
|
|
c.countSave++
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
func (c *testConn) Get() int {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
return c.countSave
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
conn := &testConn{}
|
|
|
|
config := &runtime.Config{
|
|
|
|
Nodes: struct {
|
|
|
|
Enable bool `toml:"enable"`
|
|
|
|
StatePath string `toml:"state_path"`
|
|
|
|
SaveInterval runtime.Duration `toml:"save_interval"`
|
|
|
|
OfflineAfter runtime.Duration `toml:"offline_after"`
|
|
|
|
PruneAfter runtime.Duration `toml:"prune_after"`
|
|
|
|
Output map[string]interface{}
|
|
|
|
}{
|
|
|
|
SaveInterval: runtime.Duration{Duration: time.Millisecond * 10},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Nil(quit)
|
|
|
|
|
|
|
|
Start(conn, nil, config)
|
|
|
|
assert.NotNil(quit)
|
|
|
|
|
2017-11-14 19:33:28 +01:00
|
|
|
assert.Equal(0, conn.Get())
|
2017-05-20 14:46:29 +02:00
|
|
|
time.Sleep(time.Millisecond * 12)
|
2017-11-14 19:33:28 +01:00
|
|
|
assert.Equal(1, conn.Get())
|
2017-05-20 14:46:29 +02:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 12)
|
|
|
|
Close()
|
2017-11-14 19:33:28 +01:00
|
|
|
assert.Equal(2, conn.Get())
|
2017-05-20 14:46:29 +02:00
|
|
|
|
|
|
|
}
|