yanic/database/socket/database_test.go

73 lines
1.7 KiB
Go
Raw Normal View History

2017-04-27 21:30:54 +02:00
package socket
import (
"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)
config["type"] = "unix"
config["address"] = "/tmp/yanic-database.socket"
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
config["type"] = "unix"
2017-04-27 21:45:44 +02:00
config["address"] = "/tmp/yanic-database2.socket"
2017-04-27 21:30:54 +02:00
conn, err := Connect(config)
assert.NoError(err, "connection should work")
assert.NotNil(conn)
2017-04-27 21:45:44 +02:00
client, err := net.Dial("unix", "/tmp/yanic-database2.socket")
2017-04-27 21:30:54 +02:00
assert.NoError(err, "connection should work")
assert.NotNil(client)
2017-04-27 21:45:44 +02:00
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 21:30:54 +02:00
conn.InsertNode(&runtime.Node{})
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now())
2017-04-27 21:45:44 +02:00
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 21:30:54 +02:00
2017-04-27 21:45:44 +02:00
// to reach in sendJSON removing of disconnection
2017-04-27 21:30:54 +02:00
err = client.Close()
assert.NoError(err, "disconnect should work")
2017-04-27 21:45:44 +02:00
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 21:30:54 +02:00
conn.InsertNode(&runtime.Node{})
2017-04-27 21:45:44 +02:00
time.Sleep(time.Duration(3) * time.Microsecond)
// to reach all parts of conn.Close()
client, err = net.Dial("unix", "/tmp/yanic-database2.socket")
time.Sleep(time.Duration(3) * time.Microsecond)
2017-04-27 21:30:54 +02:00
conn.Close()
}