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