github.com/rbisecke/kafka-go@v0.4.27/protocol/protocol_test.go (about) 1 package protocol 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 ) 8 9 type testType struct { 10 Field1 string `kafka:"min=v0,max=v4,nullable"` 11 Field2 int16 `kafka:"min=v2,max=v4"` 12 Field3 []byte `kafka:"min=v2,max=v4,nullable"` 13 SubTypes []testSubType `kafka:"min=v1,max=v4"` 14 15 TaggedField1 int8 `kafka:"min=v3,max=v4,tag=0"` 16 TaggedField2 string `kafka:"min=v4,max=v4,tag=1"` 17 } 18 19 type testSubType struct { 20 SubField1 int8 `kafka:"min=v1,max=v4"` 21 } 22 23 func TestMakeFlexibleTypes(t *testing.T) { 24 types := makeTypes(reflect.TypeOf(&testType{}).Elem()) 25 if len(types) != 5 { 26 t.Error( 27 "Wrong number of types", 28 "expected", 5, 29 "got", len(types), 30 ) 31 } 32 33 fv := []int16{} 34 35 for _, to := range types { 36 if to.flexible { 37 fv = append(fv, to.version) 38 } 39 } 40 41 if !reflect.DeepEqual([]int16{3, 4}, fv) { 42 t.Error( 43 "Unexpected flexible versions", 44 "expected", []int16{3, 4}, 45 "got", fv, 46 ) 47 } 48 } 49 50 func TestEncodeDecodeFlexibleType(t *testing.T) { 51 f := &testType{ 52 Field1: "value1", 53 Field2: 15, 54 Field3: []byte("hello"), 55 SubTypes: []testSubType{ 56 { 57 SubField1: 2, 58 }, 59 { 60 SubField1: 3, 61 }, 62 }, 63 64 TaggedField1: 34, 65 TaggedField2: "taggedValue2", 66 } 67 68 b := &bytes.Buffer{} 69 e := &encoder{writer: b} 70 71 types := makeTypes(reflect.TypeOf(&testType{}).Elem()) 72 ft := types[4] 73 ft.encode(e, valueOf(f)) 74 if e.err != nil { 75 t.Error( 76 "Error during encoding", 77 "expected", nil, 78 "got", e.err, 79 ) 80 } 81 82 exp := []byte{ 83 // size of "value1" + 1 84 7, 85 // "value1" 86 118, 97, 108, 117, 101, 49, 87 // 15 as 16-bit int 88 0, 15, 89 // size of []byte("hello") + 1 90 6, 91 // []byte("hello") 92 104, 101, 108, 108, 111, 93 // size of []SubTypes + 1 94 3, 95 // 2 as 8-bit int 96 2, 97 // tag buffer for first SubType struct 98 0, 99 // 3 as 8-bit int 100 3, 101 // tag buffer for second SubType struct 102 0, 103 // number of tagged fields 104 2, 105 // id of first tagged field 106 0, 107 // size of first tagged field 108 1, 109 // 34 as 8-bit int 110 34, 111 // id of second tagged field 112 1, 113 // size of second tagged field 114 13, 115 // size of "taggedValue2" + 1 116 13, 117 // "taggedValue2" 118 116, 97, 103, 103, 101, 100, 86, 97, 108, 117, 101, 50, 119 } 120 121 if !reflect.DeepEqual(exp, b.Bytes()) { 122 t.Error( 123 "Wrong encoded output", 124 "expected", exp, 125 "got", b.Bytes(), 126 ) 127 } 128 129 b = &bytes.Buffer{} 130 b.Write(exp) 131 d := &decoder{reader: b, remain: len(exp)} 132 133 f2 := &testType{} 134 ft.decode(d, valueOf(f2)) 135 if d.err != nil { 136 t.Error( 137 "Error during decoding", 138 "expected", nil, 139 "got", e.err, 140 ) 141 } 142 143 if !reflect.DeepEqual(f, f2) { 144 t.Error( 145 "Decoded value does not equal encoded one", 146 "expected", *f, 147 "got", *f2, 148 ) 149 } 150 } 151 152 func TestVarInts(t *testing.T) { 153 type tc struct { 154 input int64 155 expVarInt []byte 156 expUVarInt []byte 157 } 158 159 tcs := []tc{ 160 { 161 input: 12, 162 expVarInt: []byte{24}, 163 expUVarInt: []byte{12}, 164 }, 165 { 166 input: 63, 167 expVarInt: []byte{126}, 168 expUVarInt: []byte{63}, 169 }, 170 { 171 input: -64, 172 expVarInt: []byte{127}, 173 expUVarInt: []byte{192, 255, 255, 255, 255, 255, 255, 255, 255, 1}, 174 }, 175 { 176 input: 64, 177 expVarInt: []byte{128, 1}, 178 expUVarInt: []byte{64}, 179 }, 180 { 181 input: 127, 182 expVarInt: []byte{254, 1}, 183 expUVarInt: []byte{127}, 184 }, 185 { 186 input: 128, 187 expVarInt: []byte{128, 2}, 188 expUVarInt: []byte{128, 1}, 189 }, 190 { 191 input: 129, 192 expVarInt: []byte{130, 2}, 193 expUVarInt: []byte{129, 1}, 194 }, 195 { 196 input: 12345, 197 expVarInt: []byte{242, 192, 1}, 198 expUVarInt: []byte{185, 96}, 199 }, 200 { 201 input: 123456789101112, 202 expVarInt: []byte{240, 232, 249, 224, 144, 146, 56}, 203 expUVarInt: []byte{184, 244, 188, 176, 136, 137, 28}, 204 }, 205 } 206 207 for _, tc := range tcs { 208 b := &bytes.Buffer{} 209 e := &encoder{writer: b} 210 e.writeVarInt(tc.input) 211 if e.err != nil { 212 t.Errorf( 213 "Unexpected error encoding %d as varInt: %+v", 214 tc.input, 215 e.err, 216 ) 217 } 218 if !reflect.DeepEqual(b.Bytes(), tc.expVarInt) { 219 t.Error( 220 "Wrong output encoding value", tc.input, "as varInt", 221 "expected", tc.expVarInt, 222 "got", b.Bytes(), 223 ) 224 } 225 expLen := sizeOfVarInt(tc.input) 226 if expLen != len(b.Bytes()) { 227 t.Error( 228 "Wrong sizeOf for", tc.input, "as varInt", 229 "expected", expLen, 230 "got", len(b.Bytes()), 231 ) 232 } 233 234 d := &decoder{reader: b, remain: len(b.Bytes())} 235 v := d.readVarInt() 236 if v != tc.input { 237 t.Error( 238 "Decoded varInt value does not equal encoded one", 239 "expected", tc.input, 240 "got", v, 241 ) 242 } 243 244 b = &bytes.Buffer{} 245 e = &encoder{writer: b} 246 e.writeUnsignedVarInt(uint64(tc.input)) 247 if e.err != nil { 248 t.Errorf( 249 "Unexpected error encoding %d as unsignedVarInt: %+v", 250 tc.input, 251 e.err, 252 ) 253 } 254 if !reflect.DeepEqual(b.Bytes(), tc.expUVarInt) { 255 t.Error( 256 "Wrong output encoding value", tc.input, "as unsignedVarInt", 257 "expected", tc.expUVarInt, 258 "got", b.Bytes(), 259 ) 260 } 261 expLen = sizeOfUnsignedVarInt(uint64(tc.input)) 262 if expLen != len(b.Bytes()) { 263 t.Error( 264 "Wrong sizeOf for", tc.input, "as unsignedVarInt", 265 "expected", expLen, 266 "got", len(b.Bytes()), 267 ) 268 } 269 270 d = &decoder{reader: b, remain: len(b.Bytes())} 271 v = int64(d.readUnsignedVarInt()) 272 if v != tc.input { 273 t.Error( 274 "Decoded unsignedVarInt value does not equal encoded one", 275 "expected", tc.input, 276 "got", v, 277 ) 278 } 279 280 } 281 }