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

improve code coverage: BaseExporter, BaseProcessor, PooledList, SamplingResult, SimpleExportProcessor, TracerProviderExtensions, MathHelper, and WildcardHelper #3476

Merged
merged 17 commits into from
Aug 4, 2022
Merged
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
62 changes: 62 additions & 0 deletions test/OpenTelemetry.Tests/BaseExporterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// <copyright file="BaseExporterTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;

using Xunit;

namespace OpenTelemetry.Tests
{
public class BaseExporterTest
{
[Fact]
public void Verify_ForceFlush_HandlesException()
{
// By default, ForceFlush should return true.
var testExporter = new DelegatingExporter<object>();
Copy link
Contributor

@utpilla utpilla Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually testing the "default" return value of BaseExporter<T>.ForceFlush()? This would test what DelegatingExporter.OnForceFlushFunc returns.

Assert.True(testExporter.ForceFlush());

// BaseExporter should catch any exceptions and return false.
var exceptionTestExporter = new DelegatingExporter<object>
{
OnForceFlushFunc = (timeout) => throw new Exception("test exception"),
};
Assert.False(exceptionTestExporter.ForceFlush());
}

[Fact]
public void Verify_Shutdown_HandlesSecond()
{
// By default, ForceFlush should return true.
var testExporter = new DelegatingExporter<object>();
Assert.True(testExporter.Shutdown());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as the previous comment.


// A second Shutdown should return false.
Assert.False(testExporter.Shutdown());
}

[Fact]
public void Verify_Shutdown_HandlesException()
{
// BaseExporter should catch any exceptions and return false.
var exceptionTestExporter = new DelegatingExporter<object>
{
OnShutdownFunc = (timeout) => throw new Exception("test exception"),
};
Assert.False(exceptionTestExporter.Shutdown());
}
}
}
69 changes: 69 additions & 0 deletions test/OpenTelemetry.Tests/BaseProcessorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// <copyright file="BaseProcessorTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;

using Xunit;

namespace OpenTelemetry.Tests
{
public class BaseProcessorTest
{
[Fact]
public void Verify_ForceFlush_HandlesException()
{
// By default, ForceFlush should return true.
var testProcessor = new DelegatingProcessor<object>();
Assert.True(testProcessor.ForceFlush());

// BaseExporter should catch any exceptions and return false.
testProcessor.OnForceFlushFunc = (timeout) => throw new Exception("test exception");
Assert.False(testProcessor.ForceFlush());
}

[Fact]
public void Verify_Shutdown_HandlesSecond()
{
// By default, Shutdown should return true.
var testProcessor = new DelegatingProcessor<object>();
Assert.True(testProcessor.Shutdown());

// A second Shutdown should return false.
Assert.False(testProcessor.Shutdown());
}

[Fact]
public void Verify_Shutdown_HandlesException()
{
// BaseExporter should catch any exceptions and return false.
var exceptionTestProcessor = new DelegatingProcessor<object>
{
OnShutdownFunc = (timeout) => throw new Exception("test exception"),
};
Assert.False(exceptionTestProcessor.Shutdown());
}

[Fact]
public void NoOp()
{
var testProcessor = new DelegatingProcessor<object>();

// These two methods are no-op, but account for 7% of the test coverage.
testProcessor.OnStart(new object());
testProcessor.OnEnd(new object());
}
}
}
24 changes: 24 additions & 0 deletions test/OpenTelemetry.Tests/Internal/MathHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,28 @@ public void LeadingZero64(long value, int numberOfLeaderZeros)
{
Assert.Equal(numberOfLeaderZeros, MathHelper.LeadingZero64(value));
}

[Theory]
[InlineData(14, 10, 4)]
[InlineData(10, 10, 0)]
[InlineData(4, 10, 4)]
[InlineData(0, 10, 0)]
[InlineData(-1, 10, 9)]
[InlineData(-10, 10, 0)]
public void PositiveModulo32(int value, int divisor, int expectedRemainder)
{
Assert.Equal(expectedRemainder, MathHelper.PositiveModulo32(value: value, divisor: divisor));
}

[Theory]
[InlineData(14, 10, 4)]
[InlineData(10, 10, 0)]
[InlineData(4, 10, 4)]
[InlineData(0, 10, 0)]
[InlineData(-1, 10, 9)]
[InlineData(-10, 10, 0)]
public void PositiveModulo64(long value, long divisor, long expectedRemainder)
{
Assert.Equal(expectedRemainder, MathHelper.PositiveModulo64(value: value, divisor: divisor));
}
}
112 changes: 112 additions & 0 deletions test/OpenTelemetry.Tests/Internal/PooledListTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// <copyright file="PooledListTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Collections;
using System.Reflection;

using Xunit;

namespace OpenTelemetry.Internal.Tests
{
public class PooledListTest
{
[Fact]
public void Verify_ICollectionExplicitProperties()
{
var pooledList = PooledList<int>.Create();
var icollection = (ICollection)pooledList;
Assert.False(icollection.IsSynchronized);
Assert.Equal(icollection, icollection.SyncRoot);
}

[Fact]
public void Verify_CreateAddClear()
{
var pooledList = PooledList<int>.Create();
Assert.Empty(pooledList);
Assert.True(pooledList.IsEmpty);

PooledList<int>.Add(ref pooledList, 1);
Assert.Single(pooledList);
Assert.False(pooledList.IsEmpty);

PooledList<int>.Add(ref pooledList, 2);
Assert.Equal(2, pooledList.Count);

Assert.Equal(1, pooledList[0]);
Assert.Equal(2, pooledList[1]);

PooledList<int>.Clear(ref pooledList);
Assert.Empty(pooledList);
Assert.True(pooledList.IsEmpty);
}

[Fact]
public void Verify_AllocatedSize()
{
int GetLastAllocatedSize(PooledList<int> pooledList)
{
var value = typeof(PooledList<int>)
.GetField("lastAllocatedSize", BindingFlags.NonPublic | BindingFlags.Static)
.GetValue(pooledList);
return (int)value;
}

var pooledList = PooledList<int>.Create();

var size = GetLastAllocatedSize(pooledList);
Assert.Equal(64, size);

// The Add() method has a condition to double the size of the buffer
// when the Count exceeds the buffer size.
// This for loop is meant to trigger that condition.
for (int i = 0; i <= size; i++)
{
PooledList<int>.Add(ref pooledList, i);
}

size = GetLastAllocatedSize(pooledList);
Assert.Equal(128, size);
}

[Fact]
public void Verify_Enumerator()
{
var pooledList = PooledList<int>.Create();
PooledList<int>.Add(ref pooledList, 1);
PooledList<int>.Add(ref pooledList, 2);
PooledList<int>.Add(ref pooledList, 3);

var enumerator = pooledList.GetEnumerator();

Assert.Equal(default, enumerator.Current);

Assert.True(enumerator.MoveNext());
Assert.Equal(1, enumerator.Current);
Assert.True(enumerator.MoveNext());
Assert.Equal(2, enumerator.Current);
Assert.True(enumerator.MoveNext());
Assert.Equal(3, enumerator.Current);

Assert.False(enumerator.MoveNext());
Assert.Equal(default, enumerator.Current);

var ienumerator = (IEnumerator)enumerator;
ienumerator.Reset();
Assert.Equal(default, enumerator.Current);
}
}
}
10 changes: 10 additions & 0 deletions test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ public void WildcardRegex_ShouldMatch(string[] patterns, string matchWith, bool

Assert.True(result == isMatch);
}

[Theory]
[InlineData(null, false)]
[InlineData("a", false)]
[InlineData("a.*", true)]
[InlineData("a.?", true)]
public void Verify_ContainsWildcard(string pattern, bool expected)
{
Assert.Equal(expected, WildcardHelper.ContainsWildcard(pattern));
}
}
}
8 changes: 8 additions & 0 deletions test/OpenTelemetry.Tests/Shared/DelegatingExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ internal sealed class DelegatingExporter<T> : BaseExporter<T>
{
public Func<Batch<T>, ExportResult> OnExportFunc { get; set; } = (batch) => default;

public Func<int, bool> OnForceFlushFunc { get; set; } = (timeout) => true;

public Func<int, bool> OnShutdownFunc { get; set; } = (timeout) => true;

public override ExportResult Export(in Batch<T> batch) => this.OnExportFunc(batch);

protected override bool OnForceFlush(int timeoutMilliseconds) => this.OnForceFlushFunc(timeoutMilliseconds);

protected override bool OnShutdown(int timeoutMilliseconds) => this.OnShutdownFunc(timeoutMilliseconds);
}
31 changes: 31 additions & 0 deletions test/OpenTelemetry.Tests/Shared/DelegatingProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="DelegatingProcessor.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry.Tests;

public class DelegatingProcessor<T> : BaseProcessor<T>
where T : class
{
public Func<int, bool> OnForceFlushFunc { get; set; } = (timeout) => true;

public Func<int, bool> OnShutdownFunc { get; set; } = (timeout) => true;

protected override bool OnForceFlush(int timeoutMilliseconds) => this.OnForceFlushFunc(timeoutMilliseconds);

protected override bool OnShutdown(int timeoutMilliseconds) => this.OnShutdownFunc(timeoutMilliseconds);
}
60 changes: 60 additions & 0 deletions test/OpenTelemetry.Tests/SimpleExportProcessorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// <copyright file="SimpleExportProcessorTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;

using Xunit;

namespace OpenTelemetry.Tests
{
public class SimpleExportProcessorTest
{
[Fact]
public void Verify_SimpleExportProcessor_HandlesException()
{
int counter = 0;

// here our exporter will throw an exception.
var testExporter = new DelegatingExporter<object>
{
OnExportFunc = (batch) =>
{
counter++;
throw new Exception("test exception");
},
};

var testSimpleExportProcessor = new TestSimpleExportProcessor(testExporter);

// Verify that the Processor catches and suppresses the exception.
testSimpleExportProcessor.OnEnd(new object());

// verify Exporter OnExport wall called.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// verify Exporter OnExport wall called.
// verify Exporter OnExport was called.

Assert.Equal(1, counter);
}

/// <summary>
/// Testable class for abstract <see cref="SimpleExportProcessor{T}"/>.
/// </summary>
public class TestSimpleExportProcessor : SimpleExportProcessor<object>
{
public TestSimpleExportProcessor(BaseExporter<object> exporter)
: base(exporter)
{
}
}
}
}
Loading