diff --git a/revid/senders_test.go b/revid/senders_test.go index 15af7a1d..1c516e16 100644 --- a/revid/senders_test.go +++ b/revid/senders_test.go @@ -51,6 +51,10 @@ const ( rTimeout = 10 * time.Millisecond ) +var ( + errSendFailed = errors.New("send failed") +) + // sender simulates sending of video data, creating discontinuities if // testDiscontinuities is set to true. type sender struct { @@ -65,7 +69,7 @@ type sender struct { func (ts *sender) send(d []byte) error { if ts.testDiscontinuities && ts.currentPkt == ts.discontinuityAt { ts.currentPkt++ - return errors.New("could not send") + return errSendFailed } cpy := make([]byte, len(d)) copy(cpy, d) @@ -256,12 +260,79 @@ func TestNewMultiSender(t *testing.T) { // First test without giving an 'active' function. _, err := newMultiSender(nil, false, 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 }) 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