vail/cmd/vail/book_test.go

58 lines
970 B
Go
Raw Permalink Normal View History

2020-04-12 20:40:52 -06:00
package main
import (
"testing"
)
func TestBook(t *testing.T) {
2022-05-15 17:38:57 -06:00
b := Book{
entries: make(map[string]*Repeater),
events: make(chan bookEvent, 5),
makeRepeater: NewTestingRepeater,
}
2020-04-12 20:40:52 -06:00
2022-05-15 17:38:57 -06:00
c1 := NewTestingClient(t)
b.Join("moo", c1)
2020-04-12 20:40:52 -06:00
b.loop()
if len(b.entries) != 1 {
t.Error("Wrong number of entries")
}
2022-05-15 17:38:57 -06:00
c1.Expect(1)
2020-05-30 22:23:53 -06:00
2020-04-12 20:40:52 -06:00
// Send to an empty channel
2022-06-06 09:54:55 -06:00
m := Message{0, 0, []uint16{22, 33}}
2022-05-15 17:38:57 -06:00
b.Send("merf", m)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
if c1.Len() > 0 {
2020-04-12 20:40:52 -06:00
t.Error("Sending to empty channel sent to non-empty channel")
}
2020-05-30 22:23:53 -06:00
2020-04-12 20:40:52 -06:00
// Send to a non-empty channel!
2022-05-15 17:38:57 -06:00
b.Send("moo", m)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
c1.Expect(1, 22, 33)
2020-04-12 20:40:52 -06:00
// Join another client
2022-05-15 17:38:57 -06:00
c2 := NewTestingClient(t)
b.Join("moo", c2)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
c1.Expect(2)
c2.Expect(2)
2020-04-12 20:40:52 -06:00
// Send to both
2022-05-15 17:38:57 -06:00
m.Duration = append(m.Duration, 44)
b.Send("moo", m)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
c1.Expect(2, 22, 33, 44)
c2.Expect(2, 22, 33, 44)
2020-05-30 22:23:53 -06:00
2020-04-12 20:40:52 -06:00
// Part a client
2022-05-15 17:38:57 -06:00
b.Part("moo", c1)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
c2.Expect(1)
2020-05-30 22:23:53 -06:00
2022-05-15 17:38:57 -06:00
b.Send("moo", m)
2020-04-12 20:40:52 -06:00
b.loop()
2022-05-15 17:38:57 -06:00
c2.Expect(1, 22, 33, 44)
2020-04-12 20:40:52 -06:00
}