github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/sstable/compression_test.go (about) 1 // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package sstable 6 7 import ( 8 "encoding/binary" 9 "math/rand" 10 "testing" 11 "time" 12 13 "github.com/cockroachdb/pebble/internal/cache" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestCompressionRoundtrip(t *testing.T) { 18 seed := time.Now().UnixNano() 19 t.Logf("seed %d", seed) 20 rng := rand.New(rand.NewSource(seed)) 21 22 for compression := DefaultCompression + 1; compression < NCompression; compression++ { 23 t.Run(compression.String(), func(t *testing.T) { 24 payload := make([]byte, rng.Intn(10<<10 /* 10 KiB */)) 25 rng.Read(payload) 26 // Create a randomly-sized buffer to house the compressed output. If it's 27 // not sufficient, compressBlock should allocate one that is. 28 compressedBuf := make([]byte, rng.Intn(1<<10 /* 1 KiB */)) 29 30 btyp, compressed := compressBlock(compression, payload, compressedBuf) 31 v, err := decompressBlock(btyp, compressed) 32 require.NoError(t, err) 33 got := payload 34 if v != nil { 35 got = v.Buf() 36 require.Equal(t, payload, got) 37 cache.Free(v) 38 } 39 }) 40 } 41 } 42 43 // TestDecompressionError tests that a decompressing a value that does not 44 // decompress returns an error. 45 func TestDecompressionError(t *testing.T) { 46 rng := rand.New(rand.NewSource(1 /* fixed seed */)) 47 48 // Create a buffer to represent a faux zstd compressed block. It's prefixed 49 // with a uvarint of the appropriate length, followed by garabge. 50 fauxCompressed := make([]byte, rng.Intn(10<<10 /* 10 KiB */)) 51 compressedPayloadLen := len(fauxCompressed) - binary.MaxVarintLen64 52 n := binary.PutUvarint(fauxCompressed, uint64(compressedPayloadLen)) 53 fauxCompressed = fauxCompressed[:n+compressedPayloadLen] 54 rng.Read(fauxCompressed[n:]) 55 56 v, err := decompressBlock(zstdCompressionBlockType, fauxCompressed) 57 t.Log(err) 58 require.Error(t, err) 59 require.Nil(t, v) 60 }