hub_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package circolog
  2. import (
  3. "testing"
  4. "gopkg.in/mcuadros/go-syslog.v2/format"
  5. )
  6. func msg(s string) format.LogParts {
  7. return format.LogParts{"text": s}
  8. }
  9. func hubCount(h Hub) int {
  10. cl := Client{Nofollow: true}
  11. cl.Messages = make(chan format.LogParts)
  12. h.Register <- cl
  13. cnt := 0
  14. for range cl.Messages {
  15. cnt++
  16. }
  17. return cnt
  18. }
  19. func hubToArray(h Hub) []string {
  20. r := make([]string, 0)
  21. cl := Client{Nofollow: true}
  22. cl.Messages = make(chan format.LogParts)
  23. h.Register <- cl
  24. for m := range cl.Messages {
  25. r = append(r, m["text"].(string))
  26. }
  27. return r
  28. }
  29. func TestSingle(t *testing.T) {
  30. h := NewHub(5)
  31. go h.Run()
  32. h.LogMessages <- msg("hi")
  33. if hubCount(h) != 1 {
  34. t.Error("wrong message number")
  35. }
  36. }
  37. func TestMany(t *testing.T) {
  38. h := NewHub(5)
  39. go h.Run()
  40. for i := 0; i < 10; i++ {
  41. h.LogMessages <- msg("hi")
  42. }
  43. if hubCount(h) != 5 {
  44. t.Error("wrong message number")
  45. }
  46. }
  47. func TestOrder(t *testing.T) {
  48. h := NewHub(5)
  49. go h.Run()
  50. h.LogMessages <- msg("one")
  51. h.LogMessages <- msg("two")
  52. r := hubToArray(h)
  53. if r[0] != "one" {
  54. t.Error("wrong first message", r[0])
  55. }
  56. if r[1] != "two" {
  57. t.Error("wrong last message", r[1])
  58. }
  59. }