2020-04-12 16:37:58 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:37:58 -06:00
|
|
|
func TestRepeater(t *testing.T) {
|
|
|
|
r := NewRepeater()
|
2022-05-15 15:57:12 -06:00
|
|
|
m := TestMessage{Message{1, 3, []uint8{3, 4}}}
|
2020-04-12 16:37:58 -06:00
|
|
|
|
|
|
|
buf1 := bytes.NewBufferString("buf1")
|
2022-05-15 15:57:12 -06:00
|
|
|
buf1Expect := bytes.NewBufferString("buf1")
|
2020-04-12 17:30:00 -06:00
|
|
|
r.Join(buf1)
|
2020-04-12 20:46:51 -06:00
|
|
|
if r.Listeners() != 1 {
|
2020-04-12 16:37:58 -06:00
|
|
|
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() {
|
2020-04-12 16:37:58 -06:00
|
|
|
t.Error("Client 1 not repeating", buf1)
|
|
|
|
}
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 16:37:58 -06:00
|
|
|
buf2 := bytes.NewBufferString("buf2")
|
2022-05-15 15:57:12 -06:00
|
|
|
buf2Expect := bytes.NewBufferString("buf2")
|
2020-04-12 17:30:00 -06:00
|
|
|
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)
|
2020-04-12 16:37:58 -06:00
|
|
|
}
|
2022-05-15 15:57:12 -06:00
|
|
|
if buf2.String() != buf2Expect.String() {
|
2020-04-12 16:37:58 -06:00
|
|
|
t.Error("Client 2 not repeating", buf2)
|
|
|
|
}
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 17:30:00 -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() {
|
2020-04-12 16:37:58 -06:00
|
|
|
t.Error("Client 1 still getting data after part", buf1)
|
|
|
|
}
|
2022-05-15 15:57:12 -06:00
|
|
|
if buf2.String() != buf2Expect.String() {
|
2020-04-12 16:37:58 -06:00
|
|
|
t.Error("Client 2 not getting data after part", buf2)
|
|
|
|
}
|
|
|
|
}
|