github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/symdb/functions_test.go (about) 1 package symdb 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 v1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1" 10 ) 11 12 func Test_FunctionsEncoding(t *testing.T) { 13 type testCase struct { 14 description string 15 funcs []v1.InMemoryFunction 16 } 17 18 testCases := []testCase{ 19 { 20 description: "empty", 21 funcs: []v1.InMemoryFunction{}, 22 }, 23 { 24 description: "zero", 25 funcs: []v1.InMemoryFunction{{}}, 26 }, 27 { 28 description: "single function", 29 funcs: []v1.InMemoryFunction{ 30 {Name: 1, SystemName: 2, Filename: 3, StartLine: 4}, 31 }, 32 }, 33 { 34 description: "multiline blocks", 35 funcs: []v1.InMemoryFunction{ 36 {Name: 1, SystemName: 2, Filename: 3, StartLine: 4}, 37 {Name: 5, SystemName: 6, Filename: 7, StartLine: 8}, 38 {Name: 9, SystemName: 10, Filename: 11}, 39 {}, 40 {Name: 13, SystemName: 14, Filename: 15, StartLine: 16}, 41 }, 42 }, 43 } 44 45 for _, tc := range testCases { 46 tc := tc 47 t.Run(tc.description, func(t *testing.T) { 48 var buf bytes.Buffer 49 w := newTestFileWriter(&buf) 50 e := newFunctionsEncoder() 51 e.blockSize = 3 52 h, err := writeSymbolsBlock(w, tc.funcs, e) 53 require.NoError(t, err) 54 55 d, err := newFunctionsDecoder(h) 56 require.NoError(t, err) 57 out := make([]v1.InMemoryFunction, h.Length) 58 require.NoError(t, d.decode(out, &buf)) 59 require.Equal(t, tc.funcs, out) 60 }) 61 } 62 }