circolog/hub_test.go
2019-04-01 16:57:09 +02:00

87 lines
1.5 KiB
Go

package circolog
import (
"testing"
"git.lattuga.net/boyska/circolog/data"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
func msg(s string) format.LogParts {
return format.LogParts{"content": s}
}
func hubCount(h Hub) int {
return len(hubToArray(h))
}
var DefaultClient ClientOptions = ClientOptions{Nofollow: true, BacklogLength: -1}
func hubToArrayOpt(h Hub, opt ClientOptions) []string {
r := make([]string, 0)
cl := Client{Options: opt}
cl.Messages = make(chan data.Message)
h.Register <- cl
for m := range cl.Messages {
r = append(r, m["msg"].(string))
}
return r
}
func hubToArray(h Hub) []string {
return hubToArrayOpt(h, DefaultClient)
}
func TestSingle(t *testing.T) {
h := NewHub(5)
go h.Run()
h.LogMessages <- msg("hi")
if hubCount(h) != 1 {
t.Error("wrong message number")
}
}
func TestMany(t *testing.T) {
h := NewHub(5)
go h.Run()
for i := 0; i < 10; i++ {
h.LogMessages <- msg("hi")
}
if hubCount(h) != 5 {
t.Error("wrong message number")
}
}
func TestOrder(t *testing.T) {
h := NewHub(5)
go h.Run()
h.LogMessages <- msg("one")
h.LogMessages <- msg("two")
r := hubToArray(h)
if r[0] != "one" {
t.Error("wrong first message", r[0])
}
if r[1] != "two" {
t.Error("wrong last message", r[1])
}
}
func TestLimit(t *testing.T) {
h := NewHub(5)
go h.Run()
for i := 0; i < 10; i++ {
h.LogMessages <- msg("hi")
}
if hubCount(h) != 5 {
t.Error("wrong message number")
}
r := hubToArrayOpt(h, ClientOptions{Nofollow: true, BacklogLength: 3})
if len(r) != 3 {
t.Error("non-limited!", r)
}
}