Skip to content

Commit

Permalink
Actually check the return from Close in our tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eapache committed Nov 5, 2014
1 parent facee0a commit 38c3ae2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
16 changes: 12 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package sarama

import (
"io"
"testing"
)

func safeClose(t *testing.T, c io.Closer) {
err := c.Close()
if err != nil {
t.Error(err)
}
}

func TestDefaultClientConfigValidates(t *testing.T) {
config := NewClientConfig()
if err := config.Validate(); err != nil {
Expand All @@ -21,7 +29,7 @@ func TestSimpleClient(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)
defer mb.Close()
}

Expand All @@ -38,7 +46,7 @@ func TestClientExtraBrokers(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)
defer mb1.Close()
defer mb2.Close()
}
Expand All @@ -57,7 +65,7 @@ func TestClientMetadata(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)
defer mb1.Close()
defer mb5.Close()

Expand Down Expand Up @@ -99,7 +107,7 @@ func TestClientRefreshBehaviour(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)
defer mb1.Close()
defer mb5.Close()

Expand Down
12 changes: 6 additions & 6 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestSimpleConsumer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)

consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", nil)
if err != nil {
t.Fatal(err)
}
defer consumer.Close()
defer safeClose(t, consumer)
defer mb1.Close()
defer mb2.Close()

Expand Down Expand Up @@ -69,7 +69,7 @@ func TestConsumerRawOffset(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)

config := NewConsumerConfig()
config.OffsetMethod = OffsetMethodManual
Expand All @@ -78,7 +78,7 @@ func TestConsumerRawOffset(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer consumer.Close()
defer safeClose(t, consumer)

defer mb1.Close()
defer mb2.Close()
Expand Down Expand Up @@ -106,15 +106,15 @@ func TestConsumerLatestOffset(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)

config := NewConsumerConfig()
config.OffsetMethod = OffsetMethodNewest
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
if err != nil {
t.Fatal(err)
}
defer consumer.Close()
defer safeClose(t, consumer)

defer mb2.Close()
defer mb1.Close()
Expand Down
9 changes: 6 additions & 3 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func testProducingMessages(t *testing.T, config *ProducerConfig) {
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer safeClose(t, client)

consumerConfig := NewConsumerConfig()
consumerConfig.OffsetMethod = OffsetMethodNewest
Expand All @@ -88,7 +88,7 @@ func testProducingMessages(t *testing.T, config *ProducerConfig) {
if err != nil {
t.Fatal(err)
}
defer consumer.Close()
defer safeClose(t, consumer)

config.AckSuccesses = true
producer, err := NewProducer(client, config)
Expand Down Expand Up @@ -118,7 +118,10 @@ func testProducingMessages(t *testing.T, config *ProducerConfig) {
t.Fatal(ret.Err)
}
}
producer.Close()
err = producer.Close()
if err != nil {
t.Error(err)
}

events := consumer.Events()
for i := 1; i <= TestBatchSize; i++ {
Expand Down
13 changes: 9 additions & 4 deletions producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func TestSimpleProducer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)

producer, err := NewSimpleProducer(client, "my_topic", nil)
if err != nil {
t.Fatal(err)
}
defer producer.Close()
defer safeClose(t, producer)

for i := 0; i < 10; i++ {
err = producer.SendMessage(nil, StringEncoder(TestMessage))
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestProducer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)

config := NewProducerConfig()
config.FlushMsgCount = 10
Expand All @@ -77,7 +79,7 @@ func TestProducer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer producer.Close()
defer safeClose(t, producer)

for i := 0; i < 10; i++ {
producer.Input() <- &MessageToSend{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
Expand Down Expand Up @@ -111,6 +113,7 @@ func TestProducerMultipleFlushes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)

config := NewProducerConfig()
config.FlushMsgCount = 5
Expand Down Expand Up @@ -161,6 +164,7 @@ func TestProducerMultipleBrokers(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)

config := NewProducerConfig()
config.FlushMsgCount = 5
Expand All @@ -170,7 +174,7 @@ func TestProducerMultipleBrokers(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer producer.Close()
defer safeClose(t, producer)

for i := 0; i < 10; i++ {
producer.Input() <- &MessageToSend{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
Expand Down Expand Up @@ -199,6 +203,7 @@ func TestProducerFailureRetry(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)

config := NewProducerConfig()
config.FlushMsgCount = 10
Expand All @@ -207,7 +212,7 @@ func TestProducerFailureRetry(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer producer.Close()
defer safeClose(t, producer)

for i := 0; i < 10; i++ {
producer.Input() <- &MessageToSend{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
Expand Down

0 comments on commit 38c3ae2

Please sign in to comment.