fix close on closed socket_writer (#2748)

This commit is contained in:
Patrick Hemmer 2017-05-02 14:06:49 -04:00 committed by Daniel Nelson
parent b0a2e8e1bd
commit ce203dc687
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
2 changed files with 11 additions and 1 deletions

View File

@ -124,6 +124,16 @@ func (sw *SocketWriter) Write(metrics []telegraf.Metric) error {
return nil return nil
} }
// Close closes the connection. Noop if already closed.
func (sw *SocketWriter) Close() error {
if sw.Conn == nil {
return nil
}
err := sw.Conn.Close()
sw.Conn = nil
return err
}
func newSocketWriter() *SocketWriter { func newSocketWriter() *SocketWriter {
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
return &SocketWriter{ return &SocketWriter{

View File

@ -143,7 +143,7 @@ func TestSocketWriter_Write_err(t *testing.T) {
// close the socket to generate an error // close the socket to generate an error
lconn.Close() lconn.Close()
sw.Close() sw.Conn.Close()
err = sw.Write(metrics) err = sw.Write(metrics)
require.Error(t, err) require.Error(t, err)
assert.Nil(t, sw.Conn) assert.Nil(t, sw.Conn)