revid: added TestMultiSenderWrite

Added a test in senders_test.go to check that we can give a multiSender a few senders and have it
write to them correctly when we call multSender.Write(). This involved writing a dummy
loadSender implementation called dummyLoadSender that stores what has been written to it so that
we can check at a later time.
This commit is contained in:
Saxon 2019-03-12 17:08:36 +10:30
parent e7e3b5007b
commit 7f73e32d4c
1 changed files with 75 additions and 4 deletions

View File

@ -51,6 +51,10 @@ const (
rTimeout = 10 * time.Millisecond rTimeout = 10 * time.Millisecond
) )
var (
errSendFailed = errors.New("send failed")
)
// sender simulates sending of video data, creating discontinuities if // sender simulates sending of video data, creating discontinuities if
// testDiscontinuities is set to true. // testDiscontinuities is set to true.
type sender struct { type sender struct {
@ -65,7 +69,7 @@ type sender struct {
func (ts *sender) send(d []byte) error { func (ts *sender) send(d []byte) error {
if ts.testDiscontinuities && ts.currentPkt == ts.discontinuityAt { if ts.testDiscontinuities && ts.currentPkt == ts.discontinuityAt {
ts.currentPkt++ ts.currentPkt++
return errors.New("could not send") return errSendFailed
} }
cpy := make([]byte, len(d)) cpy := make([]byte, len(d))
copy(cpy, d) copy(cpy, d)
@ -256,12 +260,79 @@ func TestNewMultiSender(t *testing.T) {
// First test without giving an 'active' function. // First test without giving an 'active' function.
_, err := newMultiSender(nil, false, nil) _, err := newMultiSender(nil, false, nil)
if err == nil { if err == nil {
t.Error("did not get expected error on creation of multiSender without active function") t.Fatal("did not get expected error")
} }
// Now test with providing an active function // Now test with providing an active function.
_, err = newMultiSender(nil, false, func() bool { return true }) _, err = newMultiSender(nil, false, func() bool { return true })
if err != nil { if err != nil {
t.Errorf("did not expect to get error on creation of multiSender with active func provided, err: %v", err) t.Fatalf("unespected error: %v", err)
} }
} }
type dummyLoadSender struct {
data []byte
buf [][]byte
failOnSend bool
failHandled bool
}
func newDummyLoadSender(fail bool) *dummyLoadSender {
return &dummyLoadSender{failOnSend: fail, failHandled: true}
}
func (s *dummyLoadSender) load(d []byte) error {
s.data = d
return nil
}
func (s *dummyLoadSender) send() error {
if !s.failOnSend {
s.buf = append(s.buf, s.data)
return nil
}
return errSendFailed
}
func (s *dummyLoadSender) release() {
s.data = nil
}
func (s *dummyLoadSender) close() error { return nil }
func (s *dummyLoadSender) handleSendFail(err error) error {
s.failHandled = true
return nil
}
func TestMultiSenderWrite(t *testing.T) {
senders := []loadSender{
newDummyLoadSender(false),
newDummyLoadSender(false),
newDummyLoadSender(false),
}
ms, err := newMultiSender(senders, false, func() bool { return true })
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Perform some multiSender writes.
const noOfWrites = 5
for i := byte(0); i < noOfWrites; i++ {
ms.Write([]byte{i})
}
// Check that the senders got the data correctly from the writes.
for i := byte(0); i < noOfWrites; i++ {
for j, dest := range ms.senders {
got := dest.(*dummyLoadSender).buf[i][0]
if got != i {
t.Errorf("Did not get expected result for sender: %v. \nGot: %v\nWant: %v\n", j, got, i)
}
}
}
}
// TODO: test that active func works
// TODO: test that send retry works
// TODO: test that send fail works with no retry