test(loader): cover 8192-byte EOF boundary for LoadValues

Also drop the extra blank line before the test. Addresses review on
#32525.

Signed-off-by: Dean Chen <862469039@qq.com>
pull/32525/head
Dean Chen 1 month ago
parent 601445e88c
commit fd4ed4952b
No known key found for this signature in database
GPG Key ID: 03656C0AA9B7E279

@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -419,20 +420,22 @@ foo:
} }
} }
func TestLoadValuesEOFBoundary(t *testing.T) { func TestLoadValuesEOFBoundary(t *testing.T) {
// Reproduces #32506: a single logical line whose length is a multiple of // Reproduces #32506: a single logical line whose length is a multiple of
// bufio's default buffer (4096) and has no trailing newline used to be // bufio's default buffer (4096) and has no trailing newline used to be
// dropped entirely by YAMLReader, yielding empty values. // dropped entirely by YAMLReader, yielding empty values.
// Also cover 8192 (2x buffer) so we do not only hit the single-buffer case.
for _, size := range []int{4096, 8192} {
t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
prefix := []byte(`{"foo":"`) prefix := []byte(`{"foo":"`)
suffix := []byte(`"}`) suffix := []byte(`"}`)
pad := 4096 - len(prefix) - len(suffix) pad := size - len(prefix) - len(suffix)
data := make([]byte, 0, 4096) data := make([]byte, 0, size)
data = append(data, prefix...) data = append(data, prefix...)
data = append(data, bytes.Repeat([]byte("x"), pad)...) data = append(data, bytes.Repeat([]byte("x"), pad)...)
data = append(data, suffix...) data = append(data, suffix...)
if len(data) != 4096 { if len(data) != size {
t.Fatalf("test setup: want data length 4096, got %d", len(data)) t.Fatalf("test setup: want data length %d, got %d", size, len(data))
} }
values, err := LoadValues(bytes.NewReader(data)) values, err := LoadValues(bytes.NewReader(data))
@ -440,6 +443,8 @@ func TestLoadValuesEOFBoundary(t *testing.T) {
assert.Equal(t, map[string]any{ assert.Equal(t, map[string]any{
"foo": string(bytes.Repeat([]byte("x"), pad)), "foo": string(bytes.Repeat([]byte("x"), pad)),
}, values) }, values)
})
}
} }
func TestMergeValuesV3(t *testing.T) { func TestMergeValuesV3(t *testing.T) {

@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -463,20 +464,22 @@ foo:
} }
} }
func TestLoadValuesEOFBoundary(t *testing.T) { func TestLoadValuesEOFBoundary(t *testing.T) {
// Reproduces #32506: a single logical line whose length is a multiple of // Reproduces #32506: a single logical line whose length is a multiple of
// bufio's default buffer (4096) and has no trailing newline used to be // bufio's default buffer (4096) and has no trailing newline used to be
// dropped entirely by YAMLReader, yielding empty values. // dropped entirely by YAMLReader, yielding empty values.
// Also cover 8192 (2x buffer) so we do not only hit the single-buffer case.
for _, size := range []int{4096, 8192} {
t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
prefix := []byte(`{"foo":"`) prefix := []byte(`{"foo":"`)
suffix := []byte(`"}`) suffix := []byte(`"}`)
pad := 4096 - len(prefix) - len(suffix) pad := size - len(prefix) - len(suffix)
data := make([]byte, 0, 4096) data := make([]byte, 0, size)
data = append(data, prefix...) data = append(data, prefix...)
data = append(data, bytes.Repeat([]byte("x"), pad)...) data = append(data, bytes.Repeat([]byte("x"), pad)...)
data = append(data, suffix...) data = append(data, suffix...)
if len(data) != 4096 { if len(data) != size {
t.Fatalf("test setup: want data length 4096, got %d", len(data)) t.Fatalf("test setup: want data length %d, got %d", size, len(data))
} }
values, err := LoadValues(bytes.NewReader(data)) values, err := LoadValues(bytes.NewReader(data))
@ -484,6 +487,8 @@ func TestLoadValuesEOFBoundary(t *testing.T) {
assert.Equal(t, map[string]any{ assert.Equal(t, map[string]any{
"foo": string(bytes.Repeat([]byte("x"), pad)), "foo": string(bytes.Repeat([]byte("x"), pad)),
}, values) }, values)
})
}
} }
func TestMergeValuesV2(t *testing.T) { func TestMergeValuesV2(t *testing.T) {

Loading…
Cancel
Save