-
Notifications
You must be signed in to change notification settings - Fork 792
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
cijothomas
merged 17 commits into
open-telemetry:main
from
TimothyMothra:tilee/3353_OpenTelemetry_misc
Aug 4, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ebb51f
improve code coverage
TimothyMothra 0ac0e49
cleanup
TimothyMothra 585dce5
code review comments
TimothyMothra 79033a2
Merge branch 'main' into tilee/3353_OpenTelemetry_misc
TimothyMothra 7addf82
TracerProviderExtensions
TimothyMothra d928355
fix header
TimothyMothra 1f52bee
Merge branch 'main' into tilee/3353_OpenTelemetry_misc
TimothyMothra 9defa3a
merge main
TimothyMothra 1e02621
merge conflicts
TimothyMothra 967e9e9
cleanup
TimothyMothra 23a06de
fix
TimothyMothra 1b2a543
MathHelper
TimothyMothra 28fb790
additional test cases
TimothyMothra 4d04f95
cleanup
TimothyMothra 5ff9028
Merge branch 'main' into tilee/3353_OpenTelemetry_misc
TimothyMothra dc7f47b
Merge branch 'main' into tilee/3353_OpenTelemetry_misc
TimothyMothra 0b01280
Merge branch 'main' into tilee/3353_OpenTelemetry_misc
TimothyMothra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
{ | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 whatDelegatingExporter.OnForceFlushFunc
returns.