Skip to content

Commit

Permalink
Merge pull request #5 from JoshKCarroll/4-close-not-panic
Browse files Browse the repository at this point in the history
Add test coverage and handling for Close() with nil Source or Sink
  • Loading branch information
JoshuaC215 committed Feb 1, 2019
2 parents 864c15f + 5c7af5a commit 0b83cb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions frizzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func (f *Friz) Fail(m Msg) error {
// the configured Source and Sink. Any Msgs which are still unAcked after the
// timeout has expired are Failed.
func (f *Friz) FlushAndClose(timeout time.Duration) error {
if f.source == nil {
panic("FlushAndClose() should not be called without a Source configured; use Close()")
}
f.source.Stop()
tick := time.NewTicker(timeout / 10).C
timeoutAlarm := time.After(timeout)
Expand All @@ -179,11 +182,15 @@ func (f *Friz) FlushAndClose(timeout time.Duration) error {
// The Frizzle must not be used afterward.
func (f *Friz) Close() error {
f.log.Debug("Attempting to Close frizzle")
if err := f.sink.Close(); err != nil {
return err
if f.sink != nil {
if err := f.sink.Close(); err != nil {
return err
}
}
if err := f.source.Close(); err != nil {
return err
if f.source != nil {
if err := f.source.Close(); err != nil {
return err
}
}
if f.failSink != nil {
if err := f.failSink.Close(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions frizzle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func TestNilSource(t *testing.T) {

err := f.Send(testMsg, testSinkName)
assert.Nil(t, err)

mSink.On("Close").Return(nil)
err = f.Close()
assert.Nil(t, err)
mSink.AssertExpectations(t)
}

Expand Down Expand Up @@ -80,6 +84,10 @@ func TestNilSink(t *testing.T) {

receivedMsg := <-f.Receive()
assert.Equal(t, testMsgData, receivedMsg.Data())

mSource.On("Close").Return(nil)
err := f.Close()
assert.Nil(t, err)
mSource.AssertExpectations(t)
}

Expand Down

0 comments on commit 0b83cb8

Please sign in to comment.