From 3619a6f2f14621cc26639258550e3944e8c006e1 Mon Sep 17 00:00:00 2001 From: Nils Gruson Date: Wed, 6 Dec 2023 03:25:22 +0100 Subject: [PATCH] Removed Moq dependency from OpenTelemetry.Tests (#5128) --- .../SelfDiagnosticsEventListenerTest.cs | 53 ++++++++----------- .../OpenTelemetry.Tests.csproj | 1 - .../TestSelfDiagnosticsConfigRefresher.cs | 35 ++++++++++++ .../Trace/ParentBasedSamplerTests.cs | 47 +++++----------- 4 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 test/OpenTelemetry.Tests/TestSelfDiagnosticsConfigRefresher.cs diff --git a/test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs b/test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs index f85ffb7a1f7..706d81448a3 100644 --- a/test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs +++ b/test/OpenTelemetry.Tests/Internal/SelfDiagnosticsEventListenerTest.cs @@ -17,7 +17,7 @@ using System.Diagnostics.Tracing; using System.IO.MemoryMappedFiles; using System.Text; -using Moq; +using OpenTelemetry.Tests; using Xunit; namespace OpenTelemetry.Internal.Tests; @@ -41,47 +41,40 @@ public void SelfDiagnosticsEventListener_constructor_Invalid_Input() [Fact] public void SelfDiagnosticsEventListener_EventSourceSetup_LowerSeverity() { - var configRefresherMock = new Mock(); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(); + _ = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Emitting a Verbose event. Or any EventSource event with lower severity than Error. OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1"); - configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny(), out It.Ref.IsAny, out It.Ref.IsAny), Times.Never()); + Assert.False(configRefresher.TryGetLogStreamCalled); } [Fact] public void SelfDiagnosticsEventListener_EventSourceSetup_HigherSeverity() { - var configRefresherMock = new Mock(); - configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(It.IsAny(), out It.Ref.IsAny, out It.Ref.IsAny)) - .Returns(true); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(); + _ = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Emitting an Error event. Or any EventSource event with higher than or equal to to Error severity. OpenTelemetrySdkEventSource.Log.TracerProviderException("TestEvent", "Exception Details"); - configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny(), out It.Ref.IsAny, out It.Ref.IsAny)); + Assert.True(configRefresher.TryGetLogStreamCalled); } [Fact] public void SelfDiagnosticsEventListener_WriteEvent() { // Arrange - var configRefresherMock = new Mock(); var memoryMappedFile = MemoryMappedFile.CreateFromFile(LOGFILEPATH, FileMode.Create, null, 1024); Stream stream = memoryMappedFile.CreateViewStream(); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(stream); string eventMessage = "Event Message"; - int timestampPrefixLength = "2020-08-14T20:33:24.4788109Z:".Length; - byte[] bytes = Encoding.UTF8.GetBytes(eventMessage); - int availableByteCount = 100; - configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(timestampPrefixLength + bytes.Length + 1, out stream, out availableByteCount)) - .Returns(true); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Act: call WriteEvent method directly listener.WriteEvent(eventMessage, null); // Assert - configRefresherMock.Verify(refresher => refresher.TryGetLogStream(timestampPrefixLength + bytes.Length + 1, out stream, out availableByteCount)); + Assert.True(configRefresher.TryGetLogStreamCalled); stream.Dispose(); memoryMappedFile.Dispose(); AssertFileOutput(LOGFILEPATH, eventMessage); @@ -90,18 +83,18 @@ public void SelfDiagnosticsEventListener_WriteEvent() [Fact] public void SelfDiagnosticsEventListener_DateTimeGetBytes() { - var configRefresherMock = new Mock(); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(); + var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Check DateTimeKind of Utc, Local, and Unspecified - DateTime[] datetimes = new DateTime[] - { + DateTime[] datetimes = + [ DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Utc), DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Local), DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Unspecified), DateTime.UtcNow, DateTime.Now, - }; + ]; // Expect to match output string from DateTime.ToString("O") string[] expected = new string[datetimes.Length]; @@ -129,18 +122,16 @@ public void SelfDiagnosticsEventListener_DateTimeGetBytes() public void SelfDiagnosticsEventListener_EmitEvent_OmitAsConfigured() { // Arrange - var configRefresherMock = new Mock(); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(); var memoryMappedFile = MemoryMappedFile.CreateFromFile(LOGFILEPATH, FileMode.Create, null, 1024); Stream stream = memoryMappedFile.CreateViewStream(); - configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(It.IsAny(), out stream, out It.Ref.IsAny)) - .Returns(true); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + _ = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Act: emit an event with severity lower than configured OpenTelemetrySdkEventSource.Log.ActivityStarted("ActivityStart", "123"); // Assert - configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny(), out stream, out It.Ref.IsAny), Times.Never()); + Assert.False(configRefresher.TryGetLogStreamCalled); stream.Dispose(); memoryMappedFile.Dispose(); @@ -154,18 +145,16 @@ public void SelfDiagnosticsEventListener_EmitEvent_OmitAsConfigured() public void SelfDiagnosticsEventListener_EmitEvent_CaptureAsConfigured() { // Arrange - var configRefresherMock = new Mock(); var memoryMappedFile = MemoryMappedFile.CreateFromFile(LOGFILEPATH, FileMode.Create, null, 1024); Stream stream = memoryMappedFile.CreateViewStream(); - configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(It.IsAny(), out stream, out It.Ref.IsAny)) - .Returns(true); - var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object); + var configRefresher = new TestSelfDiagnosticsConfigRefresher(stream); + _ = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresher); // Act: emit an event with severity equal to configured OpenTelemetrySdkEventSource.Log.TracerProviderException("TestEvent", "Exception Details"); // Assert - configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny(), out stream, out It.Ref.IsAny)); + Assert.True(configRefresher.TryGetLogStreamCalled); stream.Dispose(); memoryMappedFile.Dispose(); diff --git a/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj b/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj index e7b5c8a03e7..0679b5d3653 100644 --- a/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj +++ b/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj @@ -31,7 +31,6 @@ - runtime; build; native; contentfiles; analyzers diff --git a/test/OpenTelemetry.Tests/TestSelfDiagnosticsConfigRefresher.cs b/test/OpenTelemetry.Tests/TestSelfDiagnosticsConfigRefresher.cs new file mode 100644 index 00000000000..f8e4170fdcf --- /dev/null +++ b/test/OpenTelemetry.Tests/TestSelfDiagnosticsConfigRefresher.cs @@ -0,0 +1,35 @@ +// +// 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. +// + +using System.Diagnostics.CodeAnalysis; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Tests; + +internal class TestSelfDiagnosticsConfigRefresher(Stream stream = null) : SelfDiagnosticsConfigRefresher +{ + private readonly Stream stream = stream; + + public bool TryGetLogStreamCalled { get; private set; } + + public override bool TryGetLogStream(int byteCount, [NotNullWhen(true)] out Stream stream, out int availableByteCount) + { + this.TryGetLogStreamCalled = true; + stream = this.stream; + availableByteCount = 0; + return true; + } +} diff --git a/test/OpenTelemetry.Tests/Trace/ParentBasedSamplerTests.cs b/test/OpenTelemetry.Tests/Trace/ParentBasedSamplerTests.cs index f6a7d9b7dfb..e7660a7b5bc 100644 --- a/test/OpenTelemetry.Tests/Trace/ParentBasedSamplerTests.cs +++ b/test/OpenTelemetry.Tests/Trace/ParentBasedSamplerTests.cs @@ -15,7 +15,7 @@ // using System.Diagnostics; -using Moq; +using OpenTelemetry.Tests; using Xunit; namespace OpenTelemetry.Trace.Tests; @@ -105,47 +105,28 @@ public void DoNotExamineLinks() [InlineData(false, false)] public void CustomSamplers(bool parentIsRemote, bool parentIsSampled) { - var mockRepository = new MockRepository(MockBehavior.Strict); - var remoteParentSampled = mockRepository.Create(); - var remoteParentNotSampled = mockRepository.Create(); - var localParentSampled = mockRepository.Create(); - var localParentNotSampled = mockRepository.Create(); + var remoteParentSampled = new TestSampler(); + var remoteParentNotSampled = new TestSampler(); + var localParentSampled = new TestSampler(); + var localParentNotSampled = new TestSampler(); var samplerUnderTest = new ParentBasedSampler( new AlwaysOnSampler(), // root - remoteParentSampled.Object, - remoteParentNotSampled.Object, - localParentSampled.Object, - localParentNotSampled.Object); + remoteParentSampled, + remoteParentNotSampled, + localParentSampled, + localParentNotSampled); var samplingParams = MakeTestParameters(parentIsRemote, parentIsSampled); - - Mock invokedSampler; - if (parentIsRemote && parentIsSampled) - { - invokedSampler = remoteParentSampled; - } - else if (parentIsRemote && !parentIsSampled) - { - invokedSampler = remoteParentNotSampled; - } - else if (!parentIsRemote && parentIsSampled) - { - invokedSampler = localParentSampled; - } - else - { - invokedSampler = localParentNotSampled; - } - var expectedResult = new SamplingResult(SamplingDecision.RecordAndSample); - invokedSampler.Setup(sampler => sampler.ShouldSample(samplingParams)).Returns(expectedResult); - var actualResult = samplerUnderTest.ShouldSample(samplingParams); - mockRepository.VerifyAll(); + Assert.Equal(parentIsRemote && parentIsSampled, remoteParentSampled.LatestSamplingParameters.Equals(samplingParams)); + Assert.Equal(parentIsRemote && !parentIsSampled, remoteParentNotSampled.LatestSamplingParameters.Equals(samplingParams)); + Assert.Equal(!parentIsRemote && parentIsSampled, localParentSampled.LatestSamplingParameters.Equals(samplingParams)); + Assert.Equal(!parentIsRemote && !parentIsSampled, localParentNotSampled.LatestSamplingParameters.Equals(samplingParams)); + Assert.Equal(expectedResult, actualResult); - mockRepository.VerifyNoOtherCalls(); } [Fact]