Selaa lähdekoodia

Clean up and document examples.

Willem van Bergen 10 vuotta sitten
vanhempi
commit
4a62390a8f
2 muutettua tiedostoa jossa 41 lisäystä ja 37 poistoa
  1. 30 27
      consumer_test.go
  2. 11 10
      producer_test.go

+ 30 - 27
consumer_test.go

@@ -1,7 +1,9 @@
 package sarama
 package sarama
 
 
 import (
 import (
-	"fmt"
+	"log"
+	"os"
+	"os/signal"
 	"sync"
 	"sync"
 	"testing"
 	"testing"
 	"time"
 	"time"
@@ -243,54 +245,55 @@ func TestConsumerRebalancingMultiplePartitions(t *testing.T) {
 	seedBroker.Close()
 	seedBroker.Close()
 }
 }
 
 
+// This example shows how to use a consumer with a select statement
+// dealing with the different channels.
 func ExampleConsumer_select() {
 func ExampleConsumer_select() {
 	master, err := NewConsumer([]string{"localhost:9092"}, nil)
 	master, err := NewConsumer([]string{"localhost:9092"}, nil)
 	if err != nil {
 	if err != nil {
-		panic(err)
-	} else {
-		fmt.Println("> master consumer ready")
+		log.Fatalln(err)
 	}
 	}
 	defer func() {
 	defer func() {
 		if err := master.Close(); err != nil {
 		if err := master.Close(); err != nil {
-			panic(err)
+			log.Fatalln(err)
 		}
 		}
 	}()
 	}()
 
 
 	consumer, err := master.ConsumePartition("my_topic", 0, 0)
 	consumer, err := master.ConsumePartition("my_topic", 0, 0)
 	if err != nil {
 	if err != nil {
-		panic(err)
-	} else {
-		fmt.Println("> consumer ready")
+		log.Fatalln(err)
 	}
 	}
 	defer func() {
 	defer func() {
 		if err := consumer.Close(); err != nil {
 		if err := consumer.Close(); err != nil {
-			panic(err)
+			log.Fatalln(err)
 		}
 		}
 	}()
 	}()
 
 
 	msgCount := 0
 	msgCount := 0
 
 
+	signals := make(chan os.Signal, 1)
+	signal.Notify(signals, os.Interrupt)
+
 consumerLoop:
 consumerLoop:
 	for {
 	for {
 		select {
 		select {
 		case err := <-consumer.Errors():
 		case err := <-consumer.Errors():
-			panic(err)
+			log.Println(err)
 		case <-consumer.Messages():
 		case <-consumer.Messages():
 			msgCount++
 			msgCount++
-		case <-time.After(5 * time.Second):
-			fmt.Println("> timed out")
+		case <-signals:
+			log.Println("Received interrupt")
 			break consumerLoop
 			break consumerLoop
 		}
 		}
 	}
 	}
-	fmt.Println("Got", msgCount, "messages.")
+	log.Println("Processed", msgCount, "messages.")
 }
 }
 
 
+// This example shows how to use a consumer with different goroutines
+// to read from the Messages and Errors channels.
 func ExampleConsumer_goroutines() {
 func ExampleConsumer_goroutines() {
 	master, err := NewConsumer([]string{"localhost:9092"}, nil)
 	master, err := NewConsumer([]string{"localhost:9092"}, nil)
 	if err != nil {
 	if err != nil {
-		panic(err)
-	} else {
-		fmt.Println("> master consumer ready")
+		log.Fatalln(err)
 	}
 	}
 	defer func() {
 	defer func() {
 		if err := master.Close(); err != nil {
 		if err := master.Close(); err != nil {
@@ -300,15 +303,8 @@ func ExampleConsumer_goroutines() {
 
 
 	consumer, err := master.ConsumePartition("my_topic", 0, OffsetOldest)
 	consumer, err := master.ConsumePartition("my_topic", 0, OffsetOldest)
 	if err != nil {
 	if err != nil {
-		panic(err)
-	} else {
-		fmt.Println("> consumer ready")
+		log.Fatalln(err)
 	}
 	}
-	defer func() {
-		if err := consumer.Close(); err != nil {
-			panic(err)
-		}
-	}()
 
 
 	var (
 	var (
 		wg       sync.WaitGroup
 		wg       sync.WaitGroup
@@ -319,7 +315,7 @@ func ExampleConsumer_goroutines() {
 	go func() {
 	go func() {
 		defer wg.Done()
 		defer wg.Done()
 		for message := range consumer.Messages() {
 		for message := range consumer.Messages() {
-			fmt.Printf("Consumed message with offset %d", message.Offset)
+			log.Printf("Consumed message with offset %d", message.Offset)
 			msgCount++
 			msgCount++
 		}
 		}
 	}()
 	}()
@@ -328,10 +324,17 @@ func ExampleConsumer_goroutines() {
 	go func() {
 	go func() {
 		defer wg.Done()
 		defer wg.Done()
 		for err := range consumer.Errors() {
 		for err := range consumer.Errors() {
-			fmt.Println(err)
+			log.Println(err)
 		}
 		}
 	}()
 	}()
 
 
+	// Wait for an interrupt signal to trigger the shutdown
+	signals := make(chan os.Signal, 1)
+	signal.Notify(signals, os.Interrupt)
+	<-signals
+	consumer.AsyncClose()
+
+	// Wait for the Messages and Errors channel to be fully drained.
 	wg.Wait()
 	wg.Wait()
-	fmt.Println("Got", msgCount, "messages.")
+	log.Println("Processed", msgCount, "messages.")
 }
 }

+ 11 - 10
producer_test.go

@@ -545,7 +545,9 @@ ProducerLoop:
 }
 }
 
 
 // This example shows how to use the producer with separate goroutines
 // This example shows how to use the producer with separate goroutines
-// reading from the Successes and Errors channels.
+// reading from the Successes and Errors channels. Note that in order
+// for the Successes channel to be populated, you have to set
+// config.Producer.AckSuccesses to true.
 func ExampleProducer_goroutines() {
 func ExampleProducer_goroutines() {
 	config := NewConfig()
 	config := NewConfig()
 	config.Producer.AckSuccesses = true
 	config.Producer.AckSuccesses = true
@@ -598,23 +600,22 @@ ProducerLoop:
 	log.Printf("Successfully produced: %d; errors: %d\n", successes, errors)
 	log.Printf("Successfully produced: %d; errors: %d\n", successes, errors)
 }
 }
 
 
+// This example shows the basic usage pattern of the SyncProducer.
 func ExampleSyncProducer() {
 func ExampleSyncProducer() {
 	producer, err := NewSyncProducer([]string{"localhost:9092"}, nil)
 	producer, err := NewSyncProducer([]string{"localhost:9092"}, nil)
 	if err != nil {
 	if err != nil {
-		panic(err)
+		log.Fatalln(err)
 	}
 	}
 	defer func() {
 	defer func() {
 		if err := producer.Close(); err != nil {
 		if err := producer.Close(); err != nil {
-			panic(err)
+			log.Fatalln(err)
 		}
 		}
 	}()
 	}()
 
 
-	for {
-		partition, offset, err := producer.SendMessage("my_topic", nil, StringEncoder("testing 123"))
-		if err != nil {
-			panic(err)
-		} else {
-			log.Printf("> message sent to partition %d at offset %d\n", partition, offset)
-		}
+	partition, offset, err := producer.SendMessage("my_topic", nil, StringEncoder("testing 123"))
+	if err != nil {
+		log.Printf("FAILED to send message: %s\n", err)
+	} else {
+		log.Printf("> message sent to partition %d at offset %d\n", partition, offset)
 	}
 	}
 }
 }