yanic/database/socket/database_test.go

81 lines
1.6 KiB
Go
Raw Permalink Normal View History

2017-04-27 19:30:54 +00:00
package socket
import (
2017-05-29 19:33:51 +00:00
"encoding/json"
2017-04-27 19:30:54 +00:00
"net"
"testing"
"time"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/stretchr/testify/assert"
)
func TestStartup(t *testing.T) {
assert := assert.New(t)
config := make(map[string]interface{})
config["enable"] = false
conn, err := Connect(config)
assert.Nil(conn)
config["enable"] = true
config["type"] = ""
config["address"] = ""
conn, err = Connect(config)
assert.Error(err, "connection should not work")
assert.Nil(conn)
2017-05-29 20:04:05 +00:00
config["type"] = "tcp6"
config["address"] = "[::]:1337"
2017-04-27 19:30:54 +00:00
conn, err = Connect(config)
assert.NoError(err, "connection should work")
assert.NotNil(conn)
conn.Close()
}
func TestClient(t *testing.T) {
assert := assert.New(t)
config := make(map[string]interface{})
config["enable"] = true
2017-05-29 20:04:05 +00:00
config["type"] = "tcp6"
config["address"] = "[::]:1337"
2017-04-27 19:30:54 +00:00
conn, err := Connect(config)
assert.NoError(err, "connection should work")
assert.NotNil(conn)
2017-05-29 20:04:05 +00:00
client, err := net.Dial("tcp6", "[::]:1337")
2017-04-27 19:30:54 +00:00
assert.NoError(err, "connection should work")
assert.NotNil(client)
2017-04-27 19:45:44 +00:00
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 19:30:54 +00:00
2017-05-29 19:33:51 +00:00
decoder := json.NewDecoder(client)
var msg Message
2017-04-27 19:30:54 +00:00
conn.InsertNode(&runtime.Node{})
2017-05-29 19:33:51 +00:00
decoder.Decode(&msg)
assert.Equal("insert_node", msg.Event)
2017-04-27 19:30:54 +00:00
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now())
2017-05-29 19:33:51 +00:00
decoder.Decode(&msg)
assert.Equal("insert_globals", msg.Event)
conn.PruneNodes(time.Hour * 24 * 7)
decoder.Decode(&msg)
assert.Equal("prune_nodes", msg.Event)
2017-04-27 19:45:44 +00:00
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 19:30:54 +00:00
2017-04-27 19:45:44 +00:00
// to reach in sendJSON removing of disconnection
2017-05-29 19:33:51 +00:00
conn.Close()
2017-04-27 19:45:44 +00:00
2017-05-29 19:33:51 +00:00
conn.InsertNode(&runtime.Node{})
err = decoder.Decode(&msg)
assert.Error(err)
2017-04-27 19:30:54 +00:00
}