Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add internal method ToCollection() to Batch #3219

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/OpenTelemetry/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public Enumerator GetEnumerator()
: new Enumerator(this.items ?? Array.Empty<T>(), this.targetCount);
}

/// <summary>
/// Creates a <see cref="ICollection{T}"/> containing the items of the <see cref="Batch{T}"/>.
/// </summary>
/// <returns>A collection containing the items of the <see cref="Batch{T}"/>.</returns>
internal ICollection<T> ToCollection() => new List<T>(this.items);

/// <summary>
/// Enumerates the elements of a <see cref="Batch{T}"/>.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions test/OpenTelemetry.Tests/Trace/BatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using System.Collections.Generic;

using OpenTelemetry.Internal;
using Xunit;

Expand Down Expand Up @@ -160,6 +162,20 @@ public void DrainIntoNewBatchTest()
ValidateEnumerator(batch.GetEnumerator(), "b");
}

[Fact]
public void VerifyToCollection()
{
var batch = new Batch<string>(items: new string[] { "0", "1", "2", "3" }, count: 4);

var list = (List<string>)batch.ToCollection();

Assert.Equal(4, list.Count);
for (int i = 0; i < list.Count; i++)
{
Assert.Equal(i.ToString(), list[i]);
}
}

private static void ValidateEnumerator(Batch<string>.Enumerator enumerator, string expected)
{
if (enumerator.Current != null)
Expand Down