From 2e49de5fa0710eb0c41ff1265aae93102f71380d Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 14 Mar 2019 08:30:26 +1030 Subject: [PATCH] revid: newMultiSender panics if active function is not provided --- revid/revid.go | 4 ++-- revid/senders.go | 6 +++--- revid/senders_test.go | 41 +++++------------------------------------ 3 files changed, 10 insertions(+), 41 deletions(-) diff --git a/revid/revid.go b/revid/revid.go index 00679285..3a3e28f7 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -216,7 +216,7 @@ func (r *Revid) reset(config Config) error { // encoder to revid's encoder slice, and give this encoder the mtsSenders // as a destination. if len(mtsSenders) != 0 { - ms, _ := newMultiSender(mtsSenders, r.IsRunning) + ms := newMultiSender(mtsSenders, r.IsRunning) if err != nil { return err } @@ -228,7 +228,7 @@ func (r *Revid) reset(config Config) error { // encoder to revid's encoder slice, and give this encoder the flvSenders // as a destination. if len(flvSenders) != 0 { - ms, _ := newMultiSender(flvSenders, r.IsRunning) + ms := newMultiSender(flvSenders, r.IsRunning) e, err := flv.NewEncoder(ms, true, true, int(r.config.FrameRate)) if err != nil { return err diff --git a/revid/senders.go b/revid/senders.go index d5071a9f..bba427da 100644 --- a/revid/senders.go +++ b/revid/senders.go @@ -63,15 +63,15 @@ type multiSender struct { // newMultiSender returns a pointer to a new multiSender. active is a function // to indicate the state of the multiSenders owner i.e. whether it is running // or not. -func newMultiSender(senders []loadSender, active func() bool) (*multiSender, error) { +func newMultiSender(senders []loadSender, active func() bool) *multiSender { if active == nil { - return nil, errors.New("multi sender requires that active func is provided") + panic("multi sender requires that active func is provided") } s := &multiSender{ senders: senders, active: active, } - return s, nil + return s } // Write implements io.Writer. The written slice will be sent to each loadSender diff --git a/revid/senders_test.go b/revid/senders_test.go index 392dc8ed..82991a3f 100644 --- a/revid/senders_test.go +++ b/revid/senders_test.go @@ -256,22 +256,6 @@ func TestMtsSenderDiscontinuity(t *testing.T) { } } -// TestNewMultiSender checks that newMultiSender performs as expected when an -// active function is not provided, and when an active function is provided. -func TestNewMultiSender(t *testing.T) { - // First test without giving an 'active' function. - _, err := newMultiSender(nil, nil) - if err == nil { - t.Fatal("did not get expected error") - } - - // Now test with providing an active function. - _, err = newMultiSender(nil, func() bool { return true }) - if err != nil { - t.Fatalf("unespected error: %v", err) - } -} - // dummyLoadSender is a loadSender implementation that allows us to simulate // the behaviour of a loadSender and check that it performas as expected. type dummyLoadSender struct { @@ -337,10 +321,7 @@ func TestMultiSenderWrite(t *testing.T) { newDummyLoadSender(false, false), newDummyLoadSender(false, false), } - ms, err := newMultiSender(senders, func() bool { return true }) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + ms := newMultiSender(senders, func() bool { return true }) // Perform some multiSender writes. const noOfWrites = 5 @@ -376,10 +357,7 @@ func TestMultiSenderNotActiveNoRetry(t *testing.T) { return active } - ms, err := newMultiSender(senders, activeFunc) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + ms := newMultiSender(senders, activeFunc) // We will perform two writes. We expect the second write not to be complete, // i.e. the senders should not send anything on this write. @@ -418,10 +396,7 @@ func TestMultiSenderNotActiveRetry(t *testing.T) { return active } - ms, err := newMultiSender(senders, activeFunc) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + ms := newMultiSender(senders, activeFunc) // We run this in background so that we can change running state during the // the write. We then expect done to be true after some period of time. @@ -453,10 +428,7 @@ func TestMultiSenderFailNoRetry(t *testing.T) { newDummyLoadSender(false, false), } - ms, err := newMultiSender(senders, func() bool { return true }) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + ms := newMultiSender(senders, func() bool { return true }) // We will perform two writes. We expect the second write not to be complete, // i.e. the senders should not send anything on this write. @@ -507,10 +479,7 @@ func TestMultiSenderFailNoRetry(t *testing.T) { func TestMultiSenderFailRetry(t *testing.T) { // NB: This is only being tested with one sender - this is AusOcean's use case. senders := []loadSender{newDummyLoadSender(false, true)} - ms, err := newMultiSender(senders, func() bool { return true }) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + ms := newMultiSender(senders, func() bool { return true }) // Perform one write with successful send. ms.Write([]byte{0x00})