2018-11-05 16:04:10 +01:00
|
|
|
package circolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-04-01 16:57:09 +02:00
|
|
|
"git.lattuga.net/boyska/circolog/data"
|
2018-11-05 16:04:10 +01:00
|
|
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
func msg(s string) format.LogParts {
|
2019-04-01 16:57:09 +02:00
|
|
|
return format.LogParts{"content": s}
|
2018-11-05 16:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func hubCount(h Hub) int {
|
2018-11-08 19:49:29 +01:00
|
|
|
return len(hubToArray(h))
|
2018-11-05 16:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:49:29 +01:00
|
|
|
var DefaultClient ClientOptions = ClientOptions{Nofollow: true, BacklogLength: -1}
|
|
|
|
|
|
|
|
func hubToArrayOpt(h Hub, opt ClientOptions) []string {
|
2018-11-05 16:04:10 +01:00
|
|
|
r := make([]string, 0)
|
2018-11-08 19:49:29 +01:00
|
|
|
cl := Client{Options: opt}
|
2019-04-01 16:57:09 +02:00
|
|
|
cl.Messages = make(chan data.Message)
|
2018-11-05 16:04:10 +01:00
|
|
|
h.Register <- cl
|
|
|
|
for m := range cl.Messages {
|
2019-04-01 16:57:09 +02:00
|
|
|
r = append(r, m["msg"].(string))
|
2018-11-05 16:04:10 +01:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2018-11-08 19:49:29 +01:00
|
|
|
func hubToArray(h Hub) []string {
|
|
|
|
return hubToArrayOpt(h, DefaultClient)
|
|
|
|
}
|
2018-11-05 16:04:10 +01:00
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 19:51:00 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|