Skip to content

Commit 9ae4de1

Browse files
Ashish JaiswalAshish Jaiswal
authored andcommitted
promhttp/zstd: add unit tests for zstd writer registration
Signed-off-by: Ashish Jaiswal <[email protected]>
1 parent 7ba246a commit 9ae4de1

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package zstd
15+
16+
import (
17+
"bytes"
18+
"io"
19+
"testing"
20+
21+
kpzstd "github.com/klauspost/compress/zstd"
22+
23+
prominternal "github.com/prometheus/client_golang/prometheus/promhttp/internal"
24+
)
25+
26+
func TestZstdRegistersWriter(t *testing.T) {
27+
if prominternal.NewZstdWriter == nil {
28+
t.Fatal("internal.NewZstdWriter is nil; zstd support not registered")
29+
}
30+
}
31+
32+
func TestZstdWriterRoundTrip(t *testing.T) {
33+
if prominternal.NewZstdWriter == nil {
34+
t.Fatal("internal.NewZstdWriter is nil; zstd support not registered")
35+
}
36+
37+
var buf bytes.Buffer
38+
w, closeFn, err := prominternal.NewZstdWriter(&buf)
39+
if err != nil {
40+
t.Fatalf("NewZstdWriter returned error: %v", err)
41+
}
42+
if closeFn == nil {
43+
t.Fatal("closeFn is nil")
44+
}
45+
46+
in := []byte("hello zstd")
47+
if _, err := w.Write(in); err != nil {
48+
t.Fatalf("write failed: %v", err)
49+
}
50+
closeFn()
51+
52+
r, err := kpzstd.NewReader(bytes.NewReader(buf.Bytes()))
53+
if err != nil {
54+
t.Fatalf("zstd NewReader failed: %v", err)
55+
}
56+
defer r.Close()
57+
58+
out, err := io.ReadAll(r)
59+
if err != nil {
60+
t.Fatalf("read failed: %v", err)
61+
}
62+
if !bytes.Equal(out, in) {
63+
t.Fatalf("round-trip mismatch: got %q, want %q", out, in)
64+
}
65+
}

0 commit comments

Comments
 (0)