Added unit tests for SuppressInstrumentationScope (#1909)

* Added unit tests for SuppressInstrumentationScope

* Trigger CI
This commit is contained in:
Utkarsh Umesan Pillai 2021-03-15 18:26:44 -07:00 committed by GitHub
parent b021b63a28
commit 9ea9f03569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 69 additions and 0 deletions

View File

@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>
using System.Threading.Tasks;
using Xunit;
namespace OpenTelemetry.Tests
@ -43,5 +44,73 @@ namespace OpenTelemetry.Tests
Assert.False(Sdk.SuppressInstrumentation);
}
[Theory]
[InlineData(null)]
[InlineData(false)]
[InlineData(true)]
public void SuppressInstrumentationBeginTest(bool? shouldBegin)
{
Assert.False(Sdk.SuppressInstrumentation);
using var scope = shouldBegin.HasValue ? SuppressInstrumentationScope.Begin(shouldBegin.Value) : SuppressInstrumentationScope.Begin();
if (shouldBegin.HasValue)
{
Assert.Equal(shouldBegin.Value, Sdk.SuppressInstrumentation);
}
else
{
Assert.True(Sdk.SuppressInstrumentation); // Default behavior is to pass true and suppress the instrumentation
}
}
[Fact]
public async void SuppressInstrumentationScopeEnterIsLocalToAsyncFlow()
{
Assert.False(Sdk.SuppressInstrumentation);
// SuppressInstrumentationScope.Enter called inside the task is only applicable to this async flow
await Task.Factory.StartNew(() =>
{
Assert.False(Sdk.SuppressInstrumentation);
SuppressInstrumentationScope.Enter();
Assert.True(Sdk.SuppressInstrumentation);
});
Assert.False(Sdk.SuppressInstrumentation); // Changes made by SuppressInstrumentationScope.Enter in the task above are not reflected here as it's not part of the same async flow
}
[Fact]
public void DecrementIfTriggeredOnlyWorksInReferenceCountingMode()
{
// Instrumentation is not suppressed, DecrementIfTriggered is a no op
Assert.False(Sdk.SuppressInstrumentation);
SuppressInstrumentationScope.DecrementIfTriggered();
Assert.False(Sdk.SuppressInstrumentation);
// Instrumentation is suppressed in reference counting mode, DecrementIfTriggered should work
SuppressInstrumentationScope.Enter();
Assert.True(Sdk.SuppressInstrumentation);
SuppressInstrumentationScope.DecrementIfTriggered();
Assert.False(Sdk.SuppressInstrumentation); // Instrumentation is not suppressed anymore
}
[Fact]
public void IncrementIfTriggeredOnlyWorksInReferenceCountingMode()
{
// Instrumentation is not suppressed, IncrementIfTriggered is a no op
Assert.False(Sdk.SuppressInstrumentation);
SuppressInstrumentationScope.IncrementIfTriggered();
Assert.False(Sdk.SuppressInstrumentation);
// Instrumentation is suppressed in reference counting mode, IncrementIfTriggered should work
SuppressInstrumentationScope.Enter();
SuppressInstrumentationScope.IncrementIfTriggered();
Assert.True(Sdk.SuppressInstrumentation);
SuppressInstrumentationScope.DecrementIfTriggered();
Assert.True(Sdk.SuppressInstrumentation); // Instrumentation is still suppressed as IncrementIfTriggered incremented the slot count after Enter, need to decrement the slot count again to enable instrumentation
SuppressInstrumentationScope.DecrementIfTriggered();
Assert.False(Sdk.SuppressInstrumentation); // Instrumentation is not suppressed anymore
}
}
}