github.com/rbisecke/kafka-go@v0.4.27/metadata_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  )
     7  
     8  func TestClientMetadata(t *testing.T) {
     9  	client, topic, shutdown := newLocalClientAndTopic()
    10  	defer shutdown()
    11  
    12  	metadata, err := client.Metadata(context.Background(), &MetadataRequest{
    13  		Topics: []string{topic},
    14  	})
    15  
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	if len(metadata.Brokers) == 0 {
    21  		t.Error("no brokers were returned in the metadata response")
    22  	}
    23  
    24  	for _, b := range metadata.Brokers {
    25  		if b == (Broker{}) {
    26  			t.Error("unexpected broker with zero-value in metadata response")
    27  		}
    28  	}
    29  
    30  	if len(metadata.Topics) == 0 {
    31  		t.Error("no topics were returned in the metadata response")
    32  	} else {
    33  		topicMetadata := metadata.Topics[0]
    34  
    35  		if topicMetadata.Name != topic {
    36  			t.Error("invalid topic name:", topicMetadata.Name)
    37  		}
    38  
    39  		if len(topicMetadata.Partitions) == 0 {
    40  			t.Error("no partitions were returned in the topic metadata response")
    41  		} else {
    42  			partitionMetadata := topicMetadata.Partitions[0]
    43  
    44  			if partitionMetadata.Topic != topic {
    45  				t.Error("invalid partition topic name:", partitionMetadata.Topic)
    46  			}
    47  
    48  			if partitionMetadata.ID != 0 {
    49  				t.Error("invalid partition index:", partitionMetadata.ID)
    50  			}
    51  
    52  			if partitionMetadata.Leader == (Broker{}) {
    53  				t.Error("no partition leader was returned in the partition metadata response")
    54  			}
    55  
    56  			if partitionMetadata.Error != nil {
    57  				t.Error("unexpected error found in the partition metadata response:", partitionMetadata.Error)
    58  			}
    59  
    60  			// assume newLocalClientAndTopic creates the topic with one
    61  			// partition
    62  			if len(topicMetadata.Partitions) > 1 {
    63  				t.Error("too many partitions were returned in the topic metadata response")
    64  			}
    65  		}
    66  
    67  		if topicMetadata.Error != nil {
    68  			t.Error("unexpected error found in the topic metadata response:", topicMetadata.Error)
    69  		}
    70  
    71  		if len(metadata.Topics) > 1 {
    72  			t.Error("too many topics were returned in the metadata response")
    73  		}
    74  	}
    75  }