|
| 1 | +// <copyright file="AllocationFreeEnumeratorTests.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | +using Datadog.Trace.Activity.Helpers; |
| 9 | +using FluentAssertions; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Datadog.Trace.Tests.Activity.Helpers; |
| 13 | + |
| 14 | +public class AllocationFreeEnumeratorTests |
| 15 | +{ |
| 16 | + [Fact] |
| 17 | + public void CanEnumerateActivityTags() |
| 18 | + { |
| 19 | + var activity = new System.Diagnostics.Activity("operation"); |
| 20 | + activity.AddTag("key", "value"); |
| 21 | + activity.AddTag("key2", "value2"); |
| 22 | + |
| 23 | + var forEach = AllocationFreeEnumerator<IEnumerable<KeyValuePair<string, string>>, KeyValuePair<string, string>, int> |
| 24 | + .BuildAllocationFreeForEachDelegate(activity.Tags.GetType()); |
| 25 | + |
| 26 | + int invocations = 0; |
| 27 | + forEach( |
| 28 | + activity.Tags, |
| 29 | + ref invocations, |
| 30 | + (ref state, item) => |
| 31 | + { |
| 32 | + state++; |
| 33 | + return true; |
| 34 | + }); |
| 35 | + |
| 36 | + invocations.Should().Be(2); |
| 37 | + } |
| 38 | + |
| 39 | +#if NET5_0_OR_GREATER |
| 40 | + [Fact] |
| 41 | + public void CanEnumerateActivityTagObjects() |
| 42 | + { |
| 43 | + var activity = new System.Diagnostics.Activity("operation"); |
| 44 | + activity.AddTag("key", 23); |
| 45 | + activity.AddTag("key2", "value2"); |
| 46 | + |
| 47 | + var forEach = AllocationFreeEnumerator<IEnumerable<KeyValuePair<string, object>>, KeyValuePair<string, object>, int> |
| 48 | + .BuildAllocationFreeForEachDelegate(activity.TagObjects.GetType()); |
| 49 | + |
| 50 | + int invocations = 0; |
| 51 | + forEach( |
| 52 | + activity.TagObjects, |
| 53 | + ref invocations, |
| 54 | + (ref state, item) => |
| 55 | + { |
| 56 | + state++; |
| 57 | + return true; |
| 58 | + }); |
| 59 | + |
| 60 | + invocations.Should().Be(2); |
| 61 | + } |
| 62 | +#endif |
| 63 | +} |
0 commit comments