github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/tree/serialize_test.go (about) 1 package tree 2 3 import ( 4 "bytes" 5 6 . "github.com/onsi/ginkgo/v2/dsl/core" 7 . "github.com/onsi/gomega" 8 "github.com/pyroscope-io/pyroscope/pkg/storage/dict" 9 ) 10 11 var dictSerializeExample = []byte("\x01\x00\x00\x01\x02\x00\x01\x00\x02\x02\x01\x01\x01\x00\x02\x02\x01\x02\x00") 12 13 var _ = Describe("tree", func() { 14 Describe("Insert", func() { 15 tree := New() 16 tree.Insert([]byte("a;b"), uint64(1)) 17 tree.Insert([]byte("a;c"), uint64(2)) 18 19 It("correctly sets children nodes", func() { 20 Expect(tree.root.ChildrenNodes).To(HaveLen(1)) 21 }) 22 }) 23 24 Describe("SerializeTruncate", func() { 25 d := dict.New() 26 tree := New() 27 tree.Insert([]byte("a;b"), uint64(1)) 28 tree.Insert([]byte("a;c"), uint64(2)) 29 30 It("serializes tree", func() { 31 var buf bytes.Buffer 32 tree.SerializeTruncate(d, 1024, &buf) 33 Expect(buf.Bytes()).To(Equal(dictSerializeExample)) 34 }) 35 36 Context("Ran 1000000 times", func() { 37 var buf1 bytes.Buffer 38 tree.SerializeTruncate(d, 1024, &buf1) 39 It("returns the same result", func() { 40 var buf2 bytes.Buffer 41 tree.SerializeTruncate(d, 1024, &buf2) 42 Expect(buf2).To(Equal(buf1)) 43 }) 44 }) 45 }) 46 47 Describe("Deserialize", func() { 48 // TODO: add a case with a real dictionary 49 It("returns a properly deserialized tree", func() { 50 d := dict.New() 51 r := bytes.NewReader(dictSerializeExample) 52 t, err := Deserialize(d, r) 53 Expect(err).ToNot(HaveOccurred()) 54 55 Expect(string(t.root.Name)).To(Equal("")) 56 Expect(string(t.root.ChildrenNodes[0].Name)).To(Equal("label not found AAE=")) 57 Expect(string(t.root.ChildrenNodes[0].ChildrenNodes[0].Name)).To(Equal("label not found AQE=")) 58 Expect(string(t.root.ChildrenNodes[0].ChildrenNodes[1].Name)).To(Equal("label not found AgE=")) 59 }) 60 }) 61 })