[TASK] improve tests
This commit is contained in:
parent
83c721ba4d
commit
8ab3f832f9
|
@ -1,10 +1,11 @@
|
||||||
package socket
|
package socket
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* This database type is just for,
|
* This socket database is to run another service
|
||||||
* - debugging without a influxconn
|
* (without flooding the network with respondd packages)
|
||||||
* - example for other developers for new databases
|
* e.g. https://github.com/FreifunkBremen/freifunkmanager
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
@ -46,15 +47,15 @@ func Connect(configuration interface{}) (database.Connection, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) InsertNode(node *runtime.Node) {
|
func (conn *Connection) InsertNode(node *runtime.Node) {
|
||||||
conn.sendJSON(EventMessage{Event: "insert_node", Body: node})
|
conn.sendJSON(Message{Event: MessageEventInsertNode, Body: node})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
|
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time) {
|
||||||
conn.sendJSON(EventMessage{Event: "insert_globals", Body: stats})
|
conn.sendJSON(Message{Event: MessageEventInsertGlobals, Body: stats})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
|
||||||
conn.sendJSON(EventMessage{Event: "prune_nodes"})
|
conn.sendJSON(Message{Event: MessageEventPruneNodes})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) Close() {
|
func (conn *Connection) Close() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package socket
|
package socket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -53,20 +54,27 @@ func TestClient(t *testing.T) {
|
||||||
assert.NotNil(client)
|
assert.NotNil(client)
|
||||||
time.Sleep(time.Duration(3) * time.Microsecond)
|
time.Sleep(time.Duration(3) * time.Microsecond)
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(client)
|
||||||
|
var msg Message
|
||||||
|
|
||||||
conn.InsertNode(&runtime.Node{})
|
conn.InsertNode(&runtime.Node{})
|
||||||
|
decoder.Decode(&msg)
|
||||||
|
assert.Equal("insert_node", msg.Event)
|
||||||
|
|
||||||
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now())
|
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now())
|
||||||
|
decoder.Decode(&msg)
|
||||||
|
assert.Equal("insert_globals", msg.Event)
|
||||||
|
|
||||||
|
conn.PruneNodes(time.Hour * 24 * 7)
|
||||||
|
decoder.Decode(&msg)
|
||||||
|
assert.Equal("prune_nodes", msg.Event)
|
||||||
time.Sleep(time.Duration(3) * time.Microsecond)
|
time.Sleep(time.Duration(3) * time.Microsecond)
|
||||||
|
|
||||||
// to reach in sendJSON removing of disconnection
|
// to reach in sendJSON removing of disconnection
|
||||||
err = client.Close()
|
|
||||||
assert.NoError(err, "disconnect should work")
|
|
||||||
time.Sleep(time.Duration(3) * time.Microsecond)
|
|
||||||
conn.InsertNode(&runtime.Node{})
|
|
||||||
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)
|
|
||||||
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
|
||||||
|
conn.InsertNode(&runtime.Node{})
|
||||||
|
err = decoder.Decode(&msg)
|
||||||
|
assert.Error(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EventMessage struct {
|
|
||||||
Event string `json:"event"`
|
|
||||||
Body interface{} `json:"body,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *Connection) handleSocketConnection(ln net.Listener) {
|
func (conn *Connection) handleSocketConnection(ln net.Listener) {
|
||||||
for {
|
for {
|
||||||
c, err := ln.Accept()
|
c, err := ln.Accept()
|
||||||
|
@ -24,7 +19,7 @@ func (conn *Connection) handleSocketConnection(ln net.Listener) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Connection) sendJSON(msg EventMessage) {
|
func (conn *Connection) sendJSON(msg Message) {
|
||||||
conn.clientMux.Lock()
|
conn.clientMux.Lock()
|
||||||
for addr, c := range conn.clients {
|
for addr, c := range conn.clients {
|
||||||
d := json.NewEncoder(c)
|
d := json.NewEncoder(c)
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package socket
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Event string `json:"event"`
|
||||||
|
Body interface{} `json:"body,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MessageEventInsertNode = "insert_node"
|
||||||
|
MessageEventInsertGlobals = "insert_globals"
|
||||||
|
MessageEventPruneNodes = "prune_nodes"
|
||||||
|
)
|
Loading…
Reference in New Issue