Skip to content

Commit

Permalink
Merge branch 'main' into fix-3180
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Sep 17, 2022
2 parents 7003e76 + 30fcf78 commit 4458812
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ func (b Baggage) Member(key string) Member {
key: key,
value: v.Value,
properties: fromInternalProperties(v.Properties),
hasData: true,
}
}

Expand All @@ -484,6 +485,7 @@ func (b Baggage) Members() []Member {
key: k,
value: v.Value,
properties: fromInternalProperties(v.Properties),
hasData: true,
})
}
return members
Expand Down
72 changes: 71 additions & 1 deletion baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,74 @@ func TestBaggageSetMember(t *testing.T) {
assert.Equal(t, 2, len(b4.list))
}

func TestBaggageSetFalseMember(t *testing.T) {
b0 := Baggage{}

key := "k"
m := Member{key: key, hasData: false}
b1, err := b0.SetMember(m)
assert.Error(t, err)
assert.NotContains(t, b0.list, key)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, 0, len(b0.list))
assert.Equal(t, 0, len(b1.list))

m.value = "v"
b2, err := b1.SetMember(m)
assert.Error(t, err)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, baggage.Item{Value: ""}, b2.list[key])
assert.Equal(t, 0, len(b1.list))
assert.Equal(t, 0, len(b2.list))
}

func TestBaggageSetFalseMembers(t *testing.T) {
b0 := Baggage{}

key := "k"
m := Member{key: key, hasData: true}
b1, err := b0.SetMember(m)
assert.NoError(t, err)
assert.NotContains(t, b0.list, key)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, 0, len(b0.list))
assert.Equal(t, 1, len(b1.list))

m.value = "v"
b2, err := b1.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{}, b1.list[key])
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, 1, len(b1.list))
assert.Equal(t, 1, len(b2.list))

p := properties{{key: "p", hasData: false}}
m.properties = p
b3, err := b2.SetMember(m)
assert.NoError(t, err)
assert.Equal(t, baggage.Item{Value: "v"}, b2.list[key])
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b3.list[key])
assert.Equal(t, 1, len(b2.list))
assert.Equal(t, 1, len(b3.list))

// The returned baggage needs to be immutable and should use a copy of the
// properties slice.
p[0] = Property{key: "different", hasData: false}
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b3.list[key])
// Reset for below.
p[0] = Property{key: "p", hasData: false}

m = Member{key: "another", hasData: false}
b4, err := b3.SetMember(m)
assert.Error(t, err)
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b3.list[key])
assert.NotContains(t, b3.list, m.key)
assert.Equal(t, baggage.Item{Value: "v", Properties: []baggage.Property{{Key: "p"}}}, b4.list[key])
assert.Equal(t, baggage.Item{}, b4.list[m.key])
assert.Equal(t, 1, len(b3.list))
assert.Equal(t, 1, len(b4.list))
}

func TestNilBaggageMembers(t *testing.T) {
assert.Nil(t, Baggage{}.Members())
}
Expand All @@ -602,13 +670,15 @@ func TestBaggageMembers(t *testing.T) {
{key: "state", value: "on", hasValue: true},
{key: "red"},
},
hasData: true,
},
{
key: "bar",
value: "2",
properties: properties{
{key: "yellow"},
},
hasData: true,
},
}

Expand All @@ -631,7 +701,7 @@ func TestBaggageMembers(t *testing.T) {

func TestBaggageMember(t *testing.T) {
bag := Baggage{list: baggage.List{"foo": {Value: "1"}}}
assert.Equal(t, Member{key: "foo", value: "1"}, bag.Member("foo"))
assert.Equal(t, Member{key: "foo", value: "1", hasData: true}, bag.Member("foo"))
assert.Equal(t, Member{}, bag.Member("bar"))
}

Expand Down

0 comments on commit 4458812

Please sign in to comment.