MCPcopy
hub / github.com/segmentio/kafka-go / testEncodeDecode

Function testEncodeDecode

compress/compress_test.go:84–136  ·  view source on GitHub ↗
(t *testing.T, m kafka.Message, codec pkg.Codec)

Source from the content-addressed store, hash-verified

82}
83
84func testEncodeDecode(t *testing.T, m kafka.Message, codec pkg.Codec) {
85 var r1, r2 []byte
86 var err error
87
88 t.Run("text format of "+codec.Name(), func(t *testing.T) {
89 c := pkg.Compression(codec.Code())
90 a := strconv.Itoa(int(c))
91 x := pkg.Compression(-1)
92 y := pkg.Compression(-1)
93 b, err := c.MarshalText()
94 if err != nil {
95 t.Fatal(err)
96 }
97
98 if err := x.UnmarshalText([]byte(a)); err != nil {
99 t.Fatal(err)
100 }
101 if err := y.UnmarshalText(b); err != nil {
102 t.Fatal(err)
103 }
104
105 if x != c {
106 t.Errorf("compression mismatch after marshal/unmarshal: want=%s got=%s", c, x)
107 }
108 if y != c {
109 t.Errorf("compression mismatch after marshal/unmarshal: want=%s got=%s", c, y)
110 }
111 })
112
113 t.Run("encode with "+codec.Name(), func(t *testing.T) {
114 r1, err = compress(codec, m.Value)
115 if err != nil {
116 t.Fatal(err)
117 }
118 })
119
120 t.Run("decode with "+codec.Name(), func(t *testing.T) {
121 if r1 == nil {
122 if r1, err = compress(codec, m.Value); err != nil {
123 t.Fatal(err)
124 }
125 }
126 r2, err = decompress(codec, r1)
127 if err != nil {
128 t.Fatal(err)
129 }
130 if string(r2) != "message" {
131 t.Error("bad message")
132 t.Logf("expected: %q", string(m.Value))
133 t.Logf("got: %q", string(r2))
134 }
135 })
136}
137
138func TestCompressedMessages(t *testing.T) {
139 testCompressedMessages(t, new(gzip.Codec))

Callers 1

TestCompressionFunction · 0.85

Calls 8

compressFunction · 0.90
decompressFunction · 0.85
CompressionMethod · 0.80
NameMethod · 0.65
CodeMethod · 0.65
MarshalTextMethod · 0.45
UnmarshalTextMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected