Skip to content

Commit 387d033

Browse files
[release/5.0] Fix Activity.Baggage items Order (#42712)
Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
1 parent 197a77e commit 387d033

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public Activity AddBaggage(string key, string? value)
434434

435435
if (_baggage != null || Interlocked.CompareExchange(ref _baggage, new LinkedList<KeyValuePair<string, string?>>(kvp), null) != null)
436436
{
437-
_baggage.Add(kvp);
437+
_baggage.AddFront(kvp);
438438
}
439439

440440
return this;
@@ -1288,13 +1288,24 @@ public void Add(T value)
12881288
{
12891289
LinkedListNode<T> newNode = new LinkedListNode<T>(value);
12901290

1291-
lock (_first)
1291+
lock (this)
12921292
{
12931293
_last.Next = newNode;
12941294
_last = newNode;
12951295
}
12961296
}
12971297

1298+
public void AddFront(T value)
1299+
{
1300+
LinkedListNode<T> newNode = new LinkedListNode<T>(value);
1301+
1302+
lock (this)
1303+
{
1304+
newNode.Next = _first;
1305+
_first = newNode;
1306+
}
1307+
}
1308+
12981309
// Note: Some customers use this GetEnumerator dynamically to avoid allocations.
12991310
public Enumerator<T> GetEnumerator() => new Enumerator<T>(_first);
13001311
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();

src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public void Baggage()
8484
Assert.Equal("world", anotherActivity.GetBaggageItem("hello"));
8585
Assert.Equal(4, anotherActivity.Baggage.Count());
8686
Assert.Equal(new KeyValuePair<string, string>("hello", "world"), anotherActivity.Baggage.First());
87-
Assert.Equal(new KeyValuePair<string, string>(Key + 0, Value + 0), anotherActivity.Baggage.Skip(1).First());
87+
Assert.Equal(new KeyValuePair<string, string>(Key + 2, Value + 2), anotherActivity.Baggage.Skip(1).First());
8888
Assert.Equal(new KeyValuePair<string, string>(Key + 1, Value + 1), anotherActivity.Baggage.Skip(2).First());
89-
Assert.Equal(new KeyValuePair<string, string>(Key + 2, Value + 2), anotherActivity.Baggage.Skip(3).First());
89+
Assert.Equal(new KeyValuePair<string, string>(Key + 0, Value + 0), anotherActivity.Baggage.Skip(3).First());
9090
}
9191
finally
9292
{
@@ -99,6 +99,69 @@ public void Baggage()
9999
}
100100
}
101101

102+
[Fact]
103+
public void TestBaggageOrderAndDuplicateKeys()
104+
{
105+
Activity a = new Activity("Baggage");
106+
a.AddBaggage("1", "1");
107+
a.AddBaggage("1", "2");
108+
a.AddBaggage("1", "3");
109+
a.AddBaggage("1", "4");
110+
111+
int value = 4;
112+
113+
foreach (KeyValuePair<string, string> kvp in a.Baggage)
114+
{
115+
Assert.Equal("1", kvp.Key);
116+
Assert.Equal(value.ToString(), kvp.Value);
117+
value--;
118+
}
119+
120+
Assert.Equal("4", a.GetBaggageItem("1"));
121+
}
122+
123+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
124+
public void TestBaggageWithChainedActivities()
125+
{
126+
RemoteExecutor.Invoke(() => {
127+
Activity a1 = new Activity("a1");
128+
a1.Start();
129+
130+
a1.AddBaggage("1", "1");
131+
a1.AddBaggage("2", "2");
132+
133+
IEnumerable<KeyValuePair<string, string>> baggages = a1.Baggage;
134+
Assert.Equal(2, baggages.Count());
135+
Assert.Equal(new KeyValuePair<string, string>("2", "2"), baggages.ElementAt(0));
136+
Assert.Equal(new KeyValuePair<string, string>("1", "1"), baggages.ElementAt(1));
137+
138+
Activity a2 = new Activity("a2");
139+
a2.Start();
140+
141+
a2.AddBaggage("3", "3");
142+
baggages = a2.Baggage;
143+
Assert.Equal(3, baggages.Count());
144+
Assert.Equal(new KeyValuePair<string, string>("3", "3"), baggages.ElementAt(0));
145+
Assert.Equal(new KeyValuePair<string, string>("2", "2"), baggages.ElementAt(1));
146+
Assert.Equal(new KeyValuePair<string, string>("1", "1"), baggages.ElementAt(2));
147+
148+
Activity a3 = new Activity("a3");
149+
a3.Start();
150+
151+
a3.AddBaggage("4", "4");
152+
baggages = a3.Baggage;
153+
Assert.Equal(4, baggages.Count());
154+
Assert.Equal(new KeyValuePair<string, string>("4", "4"), baggages.ElementAt(0));
155+
Assert.Equal(new KeyValuePair<string, string>("3", "3"), baggages.ElementAt(1));
156+
Assert.Equal(new KeyValuePair<string, string>("2", "2"), baggages.ElementAt(2));
157+
Assert.Equal(new KeyValuePair<string, string>("1", "1"), baggages.ElementAt(3));
158+
159+
a3.Dispose();
160+
a2.Dispose();
161+
a1.Dispose();
162+
}).Dispose();
163+
}
164+
102165
/// <summary>
103166
/// Tests Tags operations
104167
/// </summary>

0 commit comments

Comments
 (0)