return null;
}
+ /// <summary>
+ /// Returns the value of the Activity tag mapped to the input key/>.
+ /// Returns null if that key does not exist.
+ /// </summary>
+ /// <param name="key">The tag key string.</param>
+ /// <returns>The tag value mapped to the input key.</returns>
+ public object? GetTagItem(string key) => _tags?.Get(key) ?? null;
+
/* Constructors Builder methods */
/// <summary>
/// Note that Activity has a 'builder' pattern, where you call the constructor, a number of 'Set*' and 'Add*' APIs and then
- /// call <see cref="Start"/> to build the activity. You MUST call <see cref="Start"/> before using it.
+ /// call <see cref="Start"/> to build the activity. You MUST call <see cref="Start"/> before using it.
/// </summary>
/// <param name="operationName">Operation's name <see cref="OperationName"/></param>
public Activity(string operationName)
}
}
+ public object? Get(string key)
+ {
+ // We don't take the lock here so it is possible the Add/Remove operations mutate the list during the Get operation.
+ LinkedListNode<KeyValuePair<string, object?>>? current = _first;
+ while (current != null)
+ {
+ if (current.Value.Key == key)
+ {
+ return current.Value.Value;
+ }
+
+ current = current.Next;
+ }
+
+ return null;
+ }
+
public void Remove(string key)
{
Assert.Equal("5", a.GetBaggageItem("5"));
// Check not added item
- Assert.Null(a.GetBaggageItem("6"));
+ Assert.Null(a.GetBaggageItem("6"));
// Adding none existing key with null value is no-op
a.SetBaggage("6", null);
Assert.Equal(5, a.Baggage.Count());
- Assert.Null(a.GetBaggageItem("6"));
+ Assert.Null(a.GetBaggageItem("6"));
// Check updated item
a.SetBaggage("5", "5.1");
// Now Remove second item
a.SetBaggage("5", null);
Assert.Equal(4, a.Baggage.Count());
- Assert.Null(a.GetBaggageItem("5"));
+ Assert.Null(a.GetBaggageItem("5"));
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
}
}
+ [Fact]
+ public void TestGetTagItem()
+ {
+ Activity a = new Activity("GetTagItem");
+
+ // Test empty tags list
+ Assert.Equal(0, a.TagObjects.Count());
+ Assert.Null(a.GetTagItem("tag1"));
+
+ // Test adding first tag
+ a.AddTag("tag1", "value1");
+ Assert.Equal(1, a.TagObjects.Count());
+ Assert.Equal("value1", a.GetTagItem("tag1"));
+ Assert.Null(a.GetTagItem("tag2"));
+
+ // Test adding one more key
+ a.AddTag("tag2", "value2");
+ Assert.Equal(2, a.TagObjects.Count());
+ Assert.Equal("value1", a.GetTagItem("tag1"));
+ Assert.Equal("value2", a.GetTagItem("tag2"));
+
+ // Test adding duplicate key
+ a.AddTag("tag1", "value1-d");
+ Assert.Equal(3, a.TagObjects.Count());
+ Assert.Equal("value1", a.GetTagItem("tag1"));
+ Assert.Equal("value2", a.GetTagItem("tag2"));
+
+ // Test setting the key (overwrite the value)
+ a.SetTag("tag1", "value1-O");
+ Assert.Equal(3, a.TagObjects.Count());
+ Assert.Equal("value1-O", a.GetTagItem("tag1"));
+ Assert.Equal("value2", a.GetTagItem("tag2"));
+
+ // Test removing the key
+ a.SetTag("tag1", null);
+ Assert.Equal(2, a.TagObjects.Count());
+ Assert.Equal("value1-d", a.GetTagItem("tag1"));
+ Assert.Equal("value2", a.GetTagItem("tag2"));
+
+ a.SetTag("tag1", null);
+ Assert.Equal(1, a.TagObjects.Count());
+ Assert.Null(a.GetTagItem("tag1"));
+ Assert.Equal("value2", a.GetTagItem("tag2"));
+
+ a.SetTag("tag2", null);
+ Assert.Equal(0, a.TagObjects.Count());
+ Assert.Null(a.GetTagItem("tag1"));
+ Assert.Null(a.GetTagItem("tag2"));
+ }
+
+
[Theory]
[InlineData("key1", null, true, 1)]
[InlineData("key2", null, false, 0)]