vail/cmd/vail/repeater_test.go

59 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"testing"
)
2022-05-15 15:57:12 -06:00
type TestMessage struct {
Message
}
func (m TestMessage) bytes() []byte {
b, _ := m.MarshalBinary()
return b
}
func TestRepeater(t *testing.T) {
r := NewRepeater()
2022-05-15 15:57:12 -06:00
m := TestMessage{Message{1, 3, []uint8{3, 4}}}
buf1 := bytes.NewBufferString("buf1")
2022-05-15 15:57:12 -06:00
buf1Expect := bytes.NewBufferString("buf1")
r.Join(buf1)
2020-04-12 20:46:51 -06:00
if r.Listeners() != 1 {
t.Error("Joining did nothing")
}
2022-05-15 15:57:12 -06:00
r.Send(m.Message)
m.Clients = 1
buf1Expect.Write(m.bytes())
if buf1.String() != buf1Expect.String() {
t.Error("Client 1 not repeating", buf1)
}
2020-05-30 22:23:53 -06:00
buf2 := bytes.NewBufferString("buf2")
2022-05-15 15:57:12 -06:00
buf2Expect := bytes.NewBufferString("buf2")
r.Join(buf2)
2022-05-15 15:57:12 -06:00
r.Send(m.Message)
m.Clients = 2
buf1Expect.Write(m.bytes())
buf2Expect.Write(m.bytes())
if buf1.String() != buf1Expect.String() {
t.Errorf("Client 1 not repeating %#v %#v", buf1, buf1Expect)
}
2022-05-15 15:57:12 -06:00
if buf2.String() != buf2Expect.String() {
t.Error("Client 2 not repeating", buf2)
}
2020-05-30 22:23:53 -06:00
r.Part(buf1)
2022-05-15 15:57:12 -06:00
r.Send(m.Message)
m.Clients = 1
buf2Expect.Write(m.bytes())
if buf1.String() != buf1Expect.String() {
t.Error("Client 1 still getting data after part", buf1)
}
2022-05-15 15:57:12 -06:00
if buf2.String() != buf2Expect.String() {
t.Error("Client 2 not getting data after part", buf2)
}
}