forked from boyska/circolog
some unit testing
This commit is contained in:
parent
89c59e5713
commit
bc0e26726c
1 changed files with 71 additions and 0 deletions
71
hub_test.go
Normal file
71
hub_test.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package circolog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mcuadros/go-syslog.v2/format"
|
||||
)
|
||||
|
||||
func msg(s string) format.LogParts {
|
||||
return format.LogParts{"text": s}
|
||||
}
|
||||
|
||||
func hubCount(h Hub) int {
|
||||
cl := Client{Nofollow: true}
|
||||
cl.Messages = make(chan format.LogParts)
|
||||
h.Register <- cl
|
||||
cnt := 0
|
||||
for range cl.Messages {
|
||||
cnt++
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
func hubToArray(h Hub) []string {
|
||||
r := make([]string, 0)
|
||||
cl := Client{Nofollow: true}
|
||||
cl.Messages = make(chan format.LogParts)
|
||||
h.Register <- cl
|
||||
for m := range cl.Messages {
|
||||
r = append(r, m["text"].(string))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue