(t *testing.T, m kafka.Message, codec pkg.Codec)
| 82 | } |
| 83 | |
| 84 | func 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 | |
| 138 | func TestCompressedMessages(t *testing.T) { |
| 139 | testCompressedMessages(t, new(gzip.Codec)) |
no test coverage detected