github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/metadata/metadata_test.go (about) 1 package metadata_test 2 3 import ( 4 "testing" 5 6 "github.com/segmentio/kafka-go/protocol/metadata" 7 "github.com/segmentio/kafka-go/protocol/prototest" 8 ) 9 10 const ( 11 v0 = 0 12 v1 = 1 13 v4 = 4 14 v8 = 8 15 ) 16 17 func TestMetadataRequest(t *testing.T) { 18 prototest.TestRequest(t, v0, &metadata.Request{ 19 TopicNames: nil, 20 }) 21 22 prototest.TestRequest(t, v4, &metadata.Request{ 23 TopicNames: []string{"hello", "world"}, 24 AllowAutoTopicCreation: true, 25 }) 26 27 prototest.TestRequest(t, v8, &metadata.Request{ 28 TopicNames: []string{"hello", "world"}, 29 AllowAutoTopicCreation: true, 30 IncludeClusterAuthorizedOperations: true, 31 IncludeTopicAuthorizedOperations: true, 32 }) 33 } 34 35 func TestMetadataResponse(t *testing.T) { 36 prototest.TestResponse(t, v0, &metadata.Response{ 37 Brokers: []metadata.ResponseBroker{ 38 { 39 NodeID: 0, 40 Host: "127.0.0.1", 41 Port: 9092, 42 }, 43 { 44 NodeID: 1, 45 Host: "127.0.0.1", 46 Port: 9093, 47 }, 48 }, 49 Topics: []metadata.ResponseTopic{ 50 { 51 Name: "topic-1", 52 Partitions: []metadata.ResponsePartition{ 53 { 54 PartitionIndex: 0, 55 LeaderID: 1, 56 ReplicaNodes: []int32{0}, 57 IsrNodes: []int32{1, 0}, 58 }, 59 }, 60 }, 61 }, 62 }) 63 64 prototest.TestResponse(t, v1, &metadata.Response{ 65 ControllerID: 1, 66 Brokers: []metadata.ResponseBroker{ 67 { 68 NodeID: 0, 69 Host: "127.0.0.1", 70 Port: 9092, 71 Rack: "rack-1", 72 }, 73 { 74 NodeID: 1, 75 Host: "127.0.0.1", 76 Port: 9093, 77 Rack: "rack-2", 78 }, 79 }, 80 Topics: []metadata.ResponseTopic{ 81 { 82 Name: "topic-1", 83 IsInternal: true, 84 Partitions: []metadata.ResponsePartition{ 85 { 86 PartitionIndex: 0, 87 LeaderID: 1, 88 ReplicaNodes: []int32{0}, 89 IsrNodes: []int32{1, 0}, 90 }, 91 { 92 PartitionIndex: 1, 93 LeaderID: 0, 94 ReplicaNodes: []int32{1}, 95 IsrNodes: []int32{0, 1}, 96 }, 97 }, 98 }, 99 }, 100 }) 101 102 prototest.TestResponse(t, v8, &metadata.Response{ 103 ThrottleTimeMs: 123, 104 ClusterID: "test", 105 ControllerID: 1, 106 ClusterAuthorizedOperations: 0x01, 107 Brokers: []metadata.ResponseBroker{ 108 { 109 NodeID: 0, 110 Host: "127.0.0.1", 111 Port: 9092, 112 Rack: "rack-1", 113 }, 114 { 115 NodeID: 1, 116 Host: "127.0.0.1", 117 Port: 9093, 118 Rack: "rack-2", 119 }, 120 }, 121 Topics: []metadata.ResponseTopic{ 122 { 123 Name: "topic-1", 124 Partitions: []metadata.ResponsePartition{ 125 { 126 PartitionIndex: 0, 127 LeaderID: 1, 128 LeaderEpoch: 1234567890, 129 ReplicaNodes: []int32{0}, 130 IsrNodes: []int32{0}, 131 OfflineReplicas: []int32{1}, 132 }, 133 { 134 ErrorCode: 1, 135 ReplicaNodes: []int32{}, 136 IsrNodes: []int32{}, 137 OfflineReplicas: []int32{}, 138 }, 139 }, 140 TopicAuthorizedOperations: 0x01, 141 }, 142 }, 143 }) 144 } 145 146 func BenchmarkMetadataRequest(b *testing.B) { 147 prototest.BenchmarkRequest(b, v8, &metadata.Request{ 148 TopicNames: []string{"hello", "world"}, 149 AllowAutoTopicCreation: true, 150 IncludeClusterAuthorizedOperations: true, 151 IncludeTopicAuthorizedOperations: true, 152 }) 153 } 154 155 func BenchmarkMetadataResponse(b *testing.B) { 156 prototest.BenchmarkResponse(b, v8, &metadata.Response{ 157 ThrottleTimeMs: 123, 158 ClusterID: "test", 159 ControllerID: 1, 160 ClusterAuthorizedOperations: 0x01, 161 Brokers: []metadata.ResponseBroker{ 162 { 163 NodeID: 0, 164 Host: "127.0.0.1", 165 Port: 9092, 166 Rack: "rack-1", 167 }, 168 { 169 NodeID: 1, 170 Host: "127.0.0.1", 171 Port: 9093, 172 Rack: "rack-2", 173 }, 174 }, 175 Topics: []metadata.ResponseTopic{ 176 { 177 Name: "topic-1", 178 Partitions: []metadata.ResponsePartition{ 179 { 180 PartitionIndex: 0, 181 LeaderID: 1, 182 LeaderEpoch: 1234567890, 183 ReplicaNodes: []int32{0}, 184 IsrNodes: []int32{0}, 185 OfflineReplicas: []int32{1}, 186 }, 187 { 188 ErrorCode: 1, 189 ReplicaNodes: []int32{}, 190 IsrNodes: []int32{}, 191 OfflineReplicas: []int32{}, 192 }, 193 }, 194 TopicAuthorizedOperations: 0x01, 195 }, 196 }, 197 }) 198 199 }