Fix Activity last tag deletion (#54306)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Thu, 17 Jun 2021 16:23:07 +0000 (09:23 -0700)
committerGitHub <noreply@github.com>
Thu, 17 Jun 2021 16:23:07 +0000 (09:23 -0700)
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs

index ce69515..9a16889 100644 (file)
@@ -1491,7 +1491,6 @@ namespace System.Diagnostics
 
             public void Remove(string key)
             {
-
                 lock (this)
                 {
                     if (_first == null)
@@ -1501,6 +1500,10 @@ namespace System.Diagnostics
                     if (_first.Value.Key == key)
                     {
                         _first = _first.Next;
+                        if (_first is null)
+                        {
+                            _last = null;
+                        }
                         return;
                     }
 
@@ -1510,6 +1513,10 @@ namespace System.Diagnostics
                     {
                         if (previous.Next.Value.Key == key)
                         {
+                            if (object.ReferenceEquals(_last, previous.Next))
+                            {
+                                _last = previous;
+                            }
                             previous.Next = previous.Next.Next;
                             return;
                         }
index 39171df..2ac5efa 100644 (file)
@@ -1604,6 +1604,36 @@ namespace System.Diagnostics.Tests
                 Assert.Equal(tags[i].Key, tagObjects[i].Key);
                 Assert.Equal(tags[i].Value, tagObjects[i].Value);
             }
+
+            // Test Deleting last tag
+            activity = new Activity("LastTagObjects");
+
+            activity.SetTag("hello1", "1");
+            activity.SetTag("hello2", "1");
+            activity.SetTag("hello2", null); // last tag get deleted
+            activity.SetTag("hello3", "2");
+            activity.SetTag("hello4", "3");
+
+            tagObjects = activity.TagObjects.ToArray();
+            Assert.Equal(3, tagObjects.Length);
+            Assert.Equal("hello1", tagObjects[0].Key);
+            Assert.Equal("1", tagObjects[0].Value);
+            Assert.Equal("hello3", tagObjects[1].Key);
+            Assert.Equal("2", tagObjects[1].Value);
+            Assert.Equal("hello4", tagObjects[2].Key);
+            Assert.Equal("3", tagObjects[2].Value);
+
+            activity = new Activity("FirstLastTagObjects");
+            activity.SetTag("hello1", "1");
+            activity.SetTag("hello1", null); // Delete the first and last tag
+            activity.SetTag("hello2", "2");
+            activity.SetTag("hello3", "3");
+            tagObjects = activity.TagObjects.ToArray();
+            Assert.Equal(2, tagObjects.Length);
+            Assert.Equal("hello2", tagObjects[0].Key);
+            Assert.Equal("2", tagObjects[0].Value);
+            Assert.Equal("hello3", tagObjects[1].Key);
+            Assert.Equal("3", tagObjects[1].Value);
         }
 
         [Fact]