Commit b0b87424 authored by Jonathan Mace's avatar Jonathan Mace

Add some more tests of BDL generated stuff

parent d0251cfc
......@@ -80,6 +80,7 @@ func (w *Writer) WriteSorted(datas ...[]byte) {
atoms = append(atoms, MakeDataAtom(data))
}
sort.Sort(LexicographicAtomSorter(atoms))
w.atoms = append(w.atoms, atoms...)
}
func (w *Writer) MarkOverflow() {
......
package baggageprotocol
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tracingplane/tracingplane-go/atomlayer"
)
//func header(level int, index uint64) atomlayer.Atom {
// return MakeIndexedHeader(level, index)
//}
//
//func keyed(level int, key string) atomlayer.Atom {
// return MakeKeyedHeader(level, []byte(key))
//}
//
//func data(bytes ...byte) atomlayer.Atom {
// return append([]byte{0}, bytes...)
//}
//
//func atoms(atoms ...atomlayer.Atom) []atomlayer.Atom {
// return []atomlayer.Atom(atoms)
//}
func TestWriteNothing(t *testing.T) {
w := NewWriter()
atoms, err := w.Atoms()
assert.Nil(t, err)
assert.Empty(t, atoms)
}
func TestBadExit(t *testing.T) {
w := NewWriter()
w.Exit()
atoms, err := w.Atoms()
assert.NotNil(t, err)
assert.Empty(t, atoms)
}
func TestWriteData(t *testing.T) {
w := NewWriter()
w.Write([]byte{5, 10, 15})
as, err := w.Atoms()
assert.Nil(t, err)
expect := atoms(
data(5,10,15),
)
assert.Equal(t, expect, as)
}
func TestEmptyBagIsIgnored(t *testing.T) {
w := NewWriter()
w.Enter(22)
w.Exit()
as, err := w.Atoms()
assert.Nil(t, err)
assert.Equal(t, []atomlayer.Atom{}, as)
}
func TestBadExit2(t *testing.T) {
w := NewWriter()
w.Enter(22)
w.Exit()
w.Exit()
_, err := w.Atoms()
assert.NotNil(t, err)
}
func TestWriteIndexedBagData(t *testing.T) {
w := NewWriter()
w.Enter(22)
w.Write([]byte{5, 10, 15})
as, err := w.Atoms()
assert.Nil(t, err)
expect := atoms(
header(0,22),
data(5,10,15),
)
assert.Equal(t, expect, as)
}
func TestWriteNestedBag(t *testing.T) {
w := NewWriter()
w.Enter(22)
w.Enter(4)
w.Write([]byte{5, 10, 15})
w.Exit()
w.Exit()
as, err := w.Atoms()
assert.Nil(t, err)
expect := atoms(
header(0,22),
header(1, 4),
data(5,10,15),
)
assert.Equal(t, expect, as)
}
func TestWriteKeyedAndIndexedBag(t *testing.T) {
w := NewWriter()
w.Enter(22)
w.Write([]byte{5, 10, 15})
w.Exit()
w.EnterKey([]byte("hello"))
w.Write([]byte{23})
w.Exit()
as, err := w.Atoms()
assert.Nil(t, err)
expect := atoms(
header(0,22),
data(5,10,15),
keyed(0, "hello"),
data(23),
)
assert.Equal(t, expect, as)
}
func TestOutOfOrderWrite(t *testing.T) {
w := NewWriter()
w.Enter(2)
w.Write([]byte{5, 10, 15})
w.Exit()
w.Enter(1)
w.Write([]byte{33})
w.Exit()
as, err := w.Atoms()
assert.NotNil(t, err)
expect := atoms(
header(0,2),
data(5,10,15),
header(0,1),
data(33),
)
assert.Equal(t, expect, as)
}
func TestWriteOverflow(t *testing.T) {
w := NewWriter()
w.Enter(2)
w.Write([]byte{5, 10, 15})
w.MarkOverflow()
w.Exit()
as, err := w.Atoms()
assert.Nil(t, err)
expect := atoms(
header(0,2),
data(5,10,15),
atomlayer.TrimMarker,
)
assert.Equal(t, expect, as)
}
\ No newline at end of file
......@@ -9,4 +9,5 @@ type Bag interface {
Read(r *baggageprotocol.Reader)
Write(w *baggageprotocol.Writer)
SetUnprocessedAtoms(atoms []atomlayer.Atom)
GetUnprocessedAtoms() []atomlayer.Atom
}
\ No newline at end of file
......@@ -9,48 +9,90 @@ import (
// An example of a class that would be generated by BDL for XTrace
type XTraceMetadata struct {
TaskID *int64 // fixed64 taskID = 0
ParentEventIDs map[int64](struct{}) // set<fixed64> parentEventIDs = 1
Overflowed bool
Unknown []atomlayer.Atom // Atoms that aren't part of the XTraceMetadata spec, but were present
taskID *int64 // fixed64 taskID = 0
parentEventIDs map[int64](struct{}) // set<fixed64> parentEventIDs = 1
overflowed bool
unknown []atomlayer.Atom // Atoms that aren't part of the XTraceMetadata spec, but were present
}
func (xTraceMetadata *XTraceMetadata) HasTaskID() bool {
return xTraceMetadata.taskID != nil
}
func (xTraceMetadata *XTraceMetadata) GetTaskID() int64 {
return *xTraceMetadata.taskID
}
func (xTraceMetadata *XTraceMetadata) SetTaskID(taskID int64) {
xTraceMetadata.taskID = &taskID
}
func (xTraceMetadata *XTraceMetadata) ParentEventIDsCount() int {
return len(xTraceMetadata.parentEventIDs)
}
func (xTraceMetadata *XTraceMetadata) AddParentEventID(parentEventIDs ...int64) {
if xTraceMetadata.parentEventIDs == nil { xTraceMetadata.parentEventIDs = make(map[int64](struct{})) }
for _,parentEventID := range(parentEventIDs) {
xTraceMetadata.parentEventIDs[parentEventID] = struct{}{}
}
}
func (xTraceMetadata *XTraceMetadata) RemoveParentEventID(parentEventID int64) {
delete(xTraceMetadata.parentEventIDs, parentEventID)
}
func (xTraceMetadata *XTraceMetadata) ClearParentEventIDs() {
xTraceMetadata.parentEventIDs = nil
}
func (xTraceMetadata *XTraceMetadata) GetParentEventIDs() (parentEventIDs []int64) {
for parentEventID := range(xTraceMetadata.parentEventIDs) {
parentEventIDs = append(parentEventIDs, parentEventID)
}
return
}
func (xTraceMetadata *XTraceMetadata) Overflowed() bool {
return xTraceMetadata.overflowed
}
func (xTraceMetadata *XTraceMetadata) Read(r *baggageprotocol.Reader) {
// TaskID
// taskID
if r.EnterIndexed(0) {
xTraceMetadata.TaskID = bdl.ReadInt64Fixed(r.Next());
xTraceMetadata.taskID = bdl.ReadInt64Fixed(r.Next());
r.Exit()
}
// ParentEventIDs
// parentEventIDs
if r.EnterIndexed(1) {
xTraceMetadata.ParentEventIDs = make(map[int64](struct{}))
xTraceMetadata.parentEventIDs = make(map[int64](struct{}))
for {
next := r.Next()
if next == nil { break }
parentEventID := bdl.ReadInt64Fixed(next)
if parentEventID != nil { xTraceMetadata.ParentEventIDs[*parentEventID] = struct{}{} }
if parentEventID != nil { xTraceMetadata.parentEventIDs[*parentEventID] = struct{}{} }
}
r.Exit()
}
// Overflow
xTraceMetadata.Overflowed = r.Overflowed
xTraceMetadata.overflowed = r.Overflowed
}
func (xTraceMetadata *XTraceMetadata) Write(w *baggageprotocol.Writer) {
// TaskID
if xTraceMetadata.TaskID != nil {
// taskID
if xTraceMetadata.taskID != nil {
w.Enter(0)
w.Write(bdl.WriteInt64Fixed(*xTraceMetadata.TaskID))
w.Write(bdl.WriteInt64Fixed(*xTraceMetadata.taskID))
w.Exit()
}
// ParentEventIDs
if len(xTraceMetadata.ParentEventIDs) > 0 {
// parentEventIDs
if len(xTraceMetadata.parentEventIDs) > 0 {
var parentEventIDs [][]byte
for parentEventID := range(xTraceMetadata.ParentEventIDs) {
for parentEventID := range(xTraceMetadata.parentEventIDs) {
parentEventIDs = append(parentEventIDs, bdl.WriteInt64Fixed(parentEventID))
}
......@@ -60,11 +102,15 @@ func (xTraceMetadata *XTraceMetadata) Write(w *baggageprotocol.Writer) {
}
// Overflow
if xTraceMetadata.Overflowed {
if xTraceMetadata.overflowed {
w.MarkOverflow()
}
}
func (xTraceMetadata *XTraceMetadata) SetUnprocessedAtoms(atoms []atomlayer.Atom) {
xTraceMetadata.Unknown = atoms
xTraceMetadata.unknown = atoms
}
func (xTraceMetadata *XTraceMetadata) GetUnprocessedAtoms() []atomlayer.Atom {
return xTraceMetadata.unknown
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ func atoms(atoms ...atomlayer.Atom) []atomlayer.Atom {
func TestXTrace(t *testing.T) {
var xtrace XTraceMetadata
assert.Empty(t, xtrace.ParentEventIDs)
assert.Empty(t, xtrace.parentEventIDs)
var baggage tracingplane.BaggageContext
......@@ -38,7 +38,7 @@ func TestXTrace(t *testing.T) {
baggage.Drop(5)
baggage.WriteBag(5, &xtrace)
baggage.Set(5, &xtrace)
}
......@@ -63,18 +63,126 @@ func TestXTrace2(t *testing.T) {
err = baggage.ReadBag(5, &xtrace)
assert.Nil(t, err)
assert.NotNil(t, xtrace.TaskID)
assert.Equal(t, int64(-8089140025500181713), *xtrace.TaskID)
assert.NotNil(t, xtrace.taskID)
assert.Equal(t, int64(-8089140025500181713), *xtrace.taskID)
assert.Equal(t, len(xtrace.ParentEventIDs), 3)
assert.Equal(t, len(xtrace.parentEventIDs), 3)
expectParentIds := make(map[int64](struct{}))
expectParentIds[int64(-990513252474593225)] = struct{}{}
expectParentIds[int64(161603163048568192)] = struct{}{}
expectParentIds[int64(9050080335756692728)] = struct{}{}
assert.Equal(t, expectParentIds, xtrace.ParentEventIDs)
assert.Equal(t, expectParentIds, xtrace.parentEventIDs)
assert.False(t, xtrace.Overflowed)
assert.False(t, xtrace.overflowed)
assert.Equal(t, []atomlayer.Atom{baggageprotocol.MakeIndexedHeader(1, 3), baggageprotocol.MakeDataAtom([]byte{3})}, xtrace.Unknown)
assert.Equal(t, []atomlayer.Atom{baggageprotocol.MakeIndexedHeader(1, 3), baggageprotocol.MakeDataAtom([]byte{3})}, xtrace.unknown)
}
func TestXTraceTaskID(t *testing.T) {
var xtrace XTraceMetadata
taskID := int64(55)
xtrace.taskID = &taskID
var baggage tracingplane.BaggageContext
baggage.Set(5, &xtrace)
expect := atoms(
header(0, 5),
header(1, 0),
data(0,0,0,0,0,0,0,55),
)
assert.Equal(t, expect, baggage.Atoms)
}
func TestXTraceTaskIDAndParents(t *testing.T) {
var xtrace XTraceMetadata
xtrace.SetTaskID(55)
xtrace.AddParentEventID(70, 71)
var baggage tracingplane.BaggageContext
err := baggage.Set(5, &xtrace)
assert.Nil(t, err)
expect := atoms(
header(0, 5),
header(1, 0),
data(0,0,0,0,0,0,0,55),
header(1, 1),
data(0,0,0,0,0,0,0,70),
data(0,0,0,0,0,0,0,71),
)
assert.Equal(t, expect, baggage.Atoms)
}
func TestUpdateXTraceParents(t *testing.T) {
var xtrace XTraceMetadata
xtrace.SetTaskID(55)
xtrace.AddParentEventID(70)
var baggage tracingplane.BaggageContext
err := baggage.Set(5, &xtrace)
assert.Nil(t, err)
expect := atoms(
header(0, 5),
header(1, 0),
data(0,0,0,0,0,0,0,55),
header(1, 1),
data(0,0,0,0,0,0,0,70),
)
assert.Equal(t, expect, baggage.Atoms)
xtrace = XTraceMetadata{}
err = baggage.ReadBag(5, &xtrace)
assert.Nil(t, err)
assert.Equal(t, int64(55), xtrace.GetTaskID())
assert.Equal(t, 1, xtrace.ParentEventIDsCount())
assert.Equal(t, []int64{70}, xtrace.GetParentEventIDs())
xtrace.ClearParentEventIDs()
xtrace.AddParentEventID(50)
err = baggage.Set(5, &xtrace)
assert.Nil(t, err)
expect = atoms(
header(0, 5),
header(1, 0),
data(0,0,0,0,0,0,0,55),
header(1, 1),
data(0,0,0,0,0,0,0,50),
)
assert.Equal(t, expect, baggage.Atoms)
xtrace = XTraceMetadata{}
err = baggage.ReadBag(5, &xtrace)
assert.Equal(t, int64(55), xtrace.GetTaskID())
assert.Equal(t, 1, xtrace.ParentEventIDsCount())
assert.Equal(t, []int64{50}, xtrace.GetParentEventIDs())
}
func TestUpdateXTrace(t *testing.T) {
var baggage tracingplane.BaggageContext
baggage.Atoms = atoms(
header(0, 3),
data(5),
header(0, 5),
header(1, 0),
data(143, 189, 154, 1, 65, 170, 219, 47),
header(1, 1),
data(242, 64, 253, 113, 224, 239, 96, 55),
data(2, 62, 33, 56, 120, 22, 229, 128),
data(125, 152, 88, 29, 177, 134, 140, 248),
header(1, 3),
data(3),
header(0, 10),
data(100),
)
}
\ No newline at end of file
......@@ -22,13 +22,14 @@ func (baggage *BaggageContext) Drop(bagIndex uint64) {
baggage.Atoms = baggageprotocol.Drop(baggage.Atoms, bagIndex, baggageprotocol.PushMarkerDown)
}
func (baggage *BaggageContext) WriteBag(bagIndex uint64, bag bdl.Bag) error {
func (baggage *BaggageContext) Set(bagIndex uint64, bag bdl.Bag) error {
// Remove the bag from the baggage
baggage.Atoms = baggageprotocol.Drop(baggage.Atoms, bagIndex, baggageprotocol.DropMarker)
// Write the new bag
writer := baggageprotocol.WriteBag(bagIndex)
bag.Write(writer)
// TODO: merge back in unprocessed atoms
newAtoms, err := writer.Atoms()
// Merge it back in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment