123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 |
- package sarama
- import (
- "errors"
- "log"
- "os"
- "os/signal"
- "sync"
- "testing"
- "time"
- )
- const TestMessage = "ABC THE MESSAGE"
- func closeProducer(t *testing.T, p AsyncProducer) {
- var wg sync.WaitGroup
- p.AsyncClose()
- wg.Add(2)
- go func() {
- for _ = range p.Successes() {
- t.Error("Unexpected message on Successes()")
- }
- wg.Done()
- }()
- go func() {
- for msg := range p.Errors() {
- t.Error(msg.Err)
- }
- wg.Done()
- }()
- wg.Wait()
- }
- func expectResults(t *testing.T, p AsyncProducer, successes, errors int) {
- for successes > 0 || errors > 0 {
- select {
- case msg := <-p.Errors():
- if msg.Msg.flags != 0 {
- t.Error("Message had flags set")
- }
- errors--
- if errors < 0 {
- t.Error(msg.Err)
- }
- case msg := <-p.Successes():
- if msg.flags != 0 {
- t.Error("Message had flags set")
- }
- successes--
- if successes < 0 {
- t.Error("Too many successes")
- }
- }
- }
- }
- type testPartitioner chan *int32
- func (p testPartitioner) Partition(msg *ProducerMessage, numPartitions int32) (int32, error) {
- part := <-p
- if part == nil {
- return 0, errors.New("BOOM")
- }
- return *part, nil
- }
- func (p testPartitioner) RequiresConsistency() bool {
- return true
- }
- func (p testPartitioner) feed(partition int32) {
- p <- &partition
- }
- func TestAsyncProducer(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage), Metadata: i}
- }
- for i := 0; i < 10; i++ {
- select {
- case msg := <-producer.Errors():
- t.Error(msg.Err)
- if msg.Msg.flags != 0 {
- t.Error("Message had flags set")
- }
- case msg := <-producer.Successes():
- if msg.flags != 0 {
- t.Error("Message had flags set")
- }
- if msg.Metadata.(int) != i {
- t.Error("Message metadata did not match")
- }
- }
- }
- closeProducer(t, producer)
- leader.Close()
- seedBroker.Close()
- }
- func TestAsyncProducerMultipleFlushes(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- leader.Returns(prodSuccess)
- leader.Returns(prodSuccess)
- config := NewConfig()
- config.Producer.Flush.Messages = 5
- config.Producer.Return.Successes = true
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for flush := 0; flush < 3; flush++ {
- for i := 0; i < 5; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- expectResults(t, producer, 5, 0)
- }
- closeProducer(t, producer)
- leader.Close()
- seedBroker.Close()
- }
- func TestAsyncProducerMultipleBrokers(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader0 := newMockBroker(t, 2)
- leader1 := newMockBroker(t, 3)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader0.Addr(), leader0.BrokerID())
- metadataResponse.AddBroker(leader1.Addr(), leader1.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader0.BrokerID(), nil, nil, ErrNoError)
- metadataResponse.AddTopicPartition("my_topic", 1, leader1.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- prodResponse0 := new(ProduceResponse)
- prodResponse0.AddTopicPartition("my_topic", 0, ErrNoError)
- leader0.Returns(prodResponse0)
- prodResponse1 := new(ProduceResponse)
- prodResponse1.AddTopicPartition("my_topic", 1, ErrNoError)
- leader1.Returns(prodResponse1)
- config := NewConfig()
- config.Producer.Flush.Messages = 5
- config.Producer.Return.Successes = true
- config.Producer.Partitioner = NewRoundRobinPartitioner
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- expectResults(t, producer, 10, 0)
- closeProducer(t, producer)
- leader1.Close()
- leader0.Close()
- seedBroker.Close()
- }
- func TestAsyncProducerCustomPartitioner(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- prodResponse := new(ProduceResponse)
- prodResponse.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodResponse)
- config := NewConfig()
- config.Producer.Flush.Messages = 2
- config.Producer.Return.Successes = true
- config.Producer.Partitioner = func(topic string) Partitioner {
- p := make(testPartitioner)
- go func() {
- p.feed(0)
- p <- nil
- p <- nil
- p <- nil
- p.feed(0)
- }()
- return p
- }
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 5; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- expectResults(t, producer, 2, 3)
- closeProducer(t, producer)
- leader.Close()
- seedBroker.Close()
- }
- func TestAsyncProducerFailureRetry(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader1 := newMockBroker(t, 2)
- leader2 := newMockBroker(t, 3)
- metadataLeader1 := new(MetadataResponse)
- metadataLeader1.AddBroker(leader1.Addr(), leader1.BrokerID())
- metadataLeader1.AddTopicPartition("my_topic", 0, leader1.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader1)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- seedBroker.Close()
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- prodNotLeader := new(ProduceResponse)
- prodNotLeader.AddTopicPartition("my_topic", 0, ErrNotLeaderForPartition)
- leader1.Returns(prodNotLeader)
- metadataLeader2 := new(MetadataResponse)
- metadataLeader2.AddBroker(leader2.Addr(), leader2.BrokerID())
- metadataLeader2.AddTopicPartition("my_topic", 0, leader2.BrokerID(), nil, nil, ErrNoError)
- leader1.Returns(metadataLeader2)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader2.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- leader1.Close()
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- leader2.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- leader2.Close()
- closeProducer(t, producer)
- }
- // If a Kafka broker becomes unavailable and then returns back in service, then
- // producer reconnects to it and continues sending messages.
- func TestAsyncProducerBrokerBounce(t *testing.T) {
- // Given
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- leaderAddr := leader.Addr()
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leaderAddr, leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- config := NewConfig()
- config.Producer.Flush.Messages = 1
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- leader.Returns(prodSuccess)
- expectResults(t, producer, 1, 0)
- // When: a broker connection gets reset by a broker (network glitch, restart, you name it).
- leader.Close() // producer should get EOF
- leader = newMockBrokerAddr(t, 2, leaderAddr) // start it up again right away for giggles
- seedBroker.Returns(metadataResponse) // tell it to go to broker 2 again
- // Then: a produced message goes through the new broker connection.
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- leader.Returns(prodSuccess)
- expectResults(t, producer, 1, 0)
- closeProducer(t, producer)
- seedBroker.Close()
- leader.Close()
- }
- func TestAsyncProducerBrokerBounceWithStaleMetadata(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader1 := newMockBroker(t, 2)
- leader2 := newMockBroker(t, 3)
- metadataLeader1 := new(MetadataResponse)
- metadataLeader1.AddBroker(leader1.Addr(), leader1.BrokerID())
- metadataLeader1.AddTopicPartition("my_topic", 0, leader1.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader1)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- config.Producer.Retry.Max = 3
- config.Producer.Retry.Backoff = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- leader1.Close() // producer should get EOF
- seedBroker.Returns(metadataLeader1) // tell it to go to leader1 again even though it's still down
- seedBroker.Returns(metadataLeader1) // tell it to go to leader1 again even though it's still down
- // ok fine, tell it to go to leader2 finally
- metadataLeader2 := new(MetadataResponse)
- metadataLeader2.AddBroker(leader2.Addr(), leader2.BrokerID())
- metadataLeader2.AddTopicPartition("my_topic", 0, leader2.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader2)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader2.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- seedBroker.Close()
- leader2.Close()
- closeProducer(t, producer)
- }
- func TestAsyncProducerMultipleRetries(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader1 := newMockBroker(t, 2)
- leader2 := newMockBroker(t, 3)
- metadataLeader1 := new(MetadataResponse)
- metadataLeader1.AddBroker(leader1.Addr(), leader1.BrokerID())
- metadataLeader1.AddTopicPartition("my_topic", 0, leader1.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader1)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- config.Producer.Retry.Max = 4
- config.Producer.Retry.Backoff = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- prodNotLeader := new(ProduceResponse)
- prodNotLeader.AddTopicPartition("my_topic", 0, ErrNotLeaderForPartition)
- leader1.Returns(prodNotLeader)
- metadataLeader2 := new(MetadataResponse)
- metadataLeader2.AddBroker(leader2.Addr(), leader2.BrokerID())
- metadataLeader2.AddTopicPartition("my_topic", 0, leader2.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader2)
- leader2.Returns(prodNotLeader)
- seedBroker.Returns(metadataLeader1)
- leader1.Returns(prodNotLeader)
- seedBroker.Returns(metadataLeader1)
- leader1.Returns(prodNotLeader)
- seedBroker.Returns(metadataLeader2)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader2.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- leader2.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- seedBroker.Close()
- leader1.Close()
- leader2.Close()
- closeProducer(t, producer)
- }
- func TestAsyncProducerOutOfRetries(t *testing.T) {
- t.Skip("Enable once bug #294 is fixed.")
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- config.Producer.Retry.Max = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- prodNotLeader := new(ProduceResponse)
- prodNotLeader.AddTopicPartition("my_topic", 0, ErrNotLeaderForPartition)
- leader.Returns(prodNotLeader)
- for i := 0; i < 10; i++ {
- select {
- case msg := <-producer.Errors():
- if msg.Err != ErrNotLeaderForPartition {
- t.Error(msg.Err)
- }
- case <-producer.Successes():
- t.Error("Unexpected success")
- }
- }
- seedBroker.Returns(metadataResponse)
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- leader.Close()
- seedBroker.Close()
- safeClose(t, producer)
- }
- func TestAsyncProducerRetryWithReferenceOpen(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- leaderAddr := leader.Addr()
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leaderAddr, leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- config := NewConfig()
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- config.Producer.Retry.Max = 1
- config.Producer.Partitioner = NewRoundRobinPartitioner
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- // prime partition 0
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 1, 0)
- // prime partition 1
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- prodSuccess = new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 1, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 1, 0)
- // reboot the broker (the producer will get EOF on its existing connection)
- leader.Close()
- leader = newMockBrokerAddr(t, 2, leaderAddr)
- // send another message on partition 0 to trigger the EOF and retry
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- // tell partition 0 to go to that broker again
- seedBroker.Returns(metadataResponse)
- // succeed this time
- prodSuccess = new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 1, 0)
- // shutdown
- closeProducer(t, producer)
- seedBroker.Close()
- leader.Close()
- }
- func TestAsyncProducerFlusherRetryCondition(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataResponse := new(MetadataResponse)
- metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
- metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataResponse)
- config := NewConfig()
- config.Producer.Flush.Messages = 5
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- config.Producer.Retry.Max = 1
- config.Producer.Partitioner = NewManualPartitioner
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- // prime partitions
- for p := int32(0); p < 2; p++ {
- for i := 0; i < 5; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage), Partition: p}
- }
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", p, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 5, 0)
- }
- // send more messages on partition 0
- for i := 0; i < 5; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage), Partition: 0}
- }
- prodNotLeader := new(ProduceResponse)
- prodNotLeader.AddTopicPartition("my_topic", 0, ErrNotLeaderForPartition)
- leader.Returns(prodNotLeader)
- time.Sleep(50 * time.Millisecond)
- leader.SetHandlerByMap(map[string]MockResponse{
- "ProduceRequest": newMockProduceResponse(t).
- SetError("my_topic", 0, ErrNoError),
- })
- // tell partition 0 to go to that broker again
- seedBroker.Returns(metadataResponse)
- // succeed this time
- expectResults(t, producer, 5, 0)
- // put five more through
- for i := 0; i < 5; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage), Partition: 0}
- }
- expectResults(t, producer, 5, 0)
- // shutdown
- closeProducer(t, producer)
- seedBroker.Close()
- leader.Close()
- }
- func TestAsyncProducerRetryShutdown(t *testing.T) {
- seedBroker := newMockBroker(t, 1)
- leader := newMockBroker(t, 2)
- metadataLeader := new(MetadataResponse)
- metadataLeader.AddBroker(leader.Addr(), leader.BrokerID())
- metadataLeader.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
- seedBroker.Returns(metadataLeader)
- config := NewConfig()
- config.Producer.Flush.Messages = 10
- config.Producer.Return.Successes = true
- config.Producer.Retry.Backoff = 0
- producer, err := NewAsyncProducer([]string{seedBroker.Addr()}, config)
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder(TestMessage)}
- }
- producer.AsyncClose()
- time.Sleep(5 * time.Millisecond) // let the shutdown goroutine kick in
- producer.Input() <- &ProducerMessage{Topic: "FOO"}
- if err := <-producer.Errors(); err.Err != ErrShuttingDown {
- t.Error(err)
- }
- prodNotLeader := new(ProduceResponse)
- prodNotLeader.AddTopicPartition("my_topic", 0, ErrNotLeaderForPartition)
- leader.Returns(prodNotLeader)
- seedBroker.Returns(metadataLeader)
- prodSuccess := new(ProduceResponse)
- prodSuccess.AddTopicPartition("my_topic", 0, ErrNoError)
- leader.Returns(prodSuccess)
- expectResults(t, producer, 10, 0)
- seedBroker.Close()
- leader.Close()
- // wait for the async-closed producer to shut down fully
- for err := range producer.Errors() {
- t.Error(err)
- }
- }
- // This example shows how to use the producer while simultaneously
- // reading the Errors channel to know about any failures.
- func ExampleAsyncProducer_select() {
- producer, err := NewAsyncProducer([]string{"localhost:9092"}, nil)
- if err != nil {
- panic(err)
- }
- defer func() {
- if err := producer.Close(); err != nil {
- log.Fatalln(err)
- }
- }()
- // Trap SIGINT to trigger a shutdown.
- signals := make(chan os.Signal, 1)
- signal.Notify(signals, os.Interrupt)
- var enqueued, errors int
- ProducerLoop:
- for {
- select {
- case producer.Input() <- &ProducerMessage{Topic: "my_topic", Key: nil, Value: StringEncoder("testing 123")}:
- enqueued++
- case err := <-producer.Errors():
- log.Println("Failed to produce message", err)
- errors++
- case <-signals:
- break ProducerLoop
- }
- }
- log.Printf("Enqueued: %d; errors: %d\n", enqueued, errors)
- }
- // This example shows how to use the producer with separate goroutines
- // reading from the Successes and Errors channels. Note that in order
- // for the Successes channel to be populated, you have to set
- // config.Producer.Return.Successes to true.
- func ExampleAsyncProducer_goroutines() {
- config := NewConfig()
- config.Producer.Return.Successes = true
- producer, err := NewAsyncProducer([]string{"localhost:9092"}, config)
- if err != nil {
- panic(err)
- }
- // Trap SIGINT to trigger a graceful shutdown.
- signals := make(chan os.Signal, 1)
- signal.Notify(signals, os.Interrupt)
- var (
- wg sync.WaitGroup
- enqueued, successes, errors int
- )
- wg.Add(1)
- go func() {
- defer wg.Done()
- for _ = range producer.Successes() {
- successes++
- }
- }()
- wg.Add(1)
- go func() {
- defer wg.Done()
- for err := range producer.Errors() {
- log.Println(err)
- errors++
- }
- }()
- ProducerLoop:
- for {
- message := &ProducerMessage{Topic: "my_topic", Value: StringEncoder("testing 123")}
- select {
- case producer.Input() <- message:
- enqueued++
- case <-signals:
- producer.AsyncClose() // Trigger a shutdown of the producer.
- break ProducerLoop
- }
- }
- wg.Wait()
- log.Printf("Successfully produced: %d; errors: %d\n", successes, errors)
- }
|