Testing EventSource (#944)

Validating EventSource

adding more tests, updating wrong namespace

updating spaces

merging invariantString to prevent duplication

removing extra space

merging exception extensions

Adding comments

updating project and tests

updating eventsourcetest

breaking tests

Revert "breaking tests"

This reverts commit 7c23b46316c9dc84d5b66c1e0498498a4b0b3ac7.
This commit is contained in:
Eddy Nakamura 2020-07-30 10:38:41 -03:00 committed by GitHub
parent 052a09bebc
commit 21d18e1662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 649 additions and 2 deletions

View File

@ -17,6 +17,7 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2" + AssemblyInfo.MoqPublicKey)] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2" + AssemblyInfo.MoqPublicKey)]
#if SIGNED #if SIGNED

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Exporter.Jaeger.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Exporter.Jaeger.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_JaegerExporterEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(JaegerExporterEventSource.Log);
}
}
}

View File

@ -27,6 +27,9 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Jaeger\OpenTelemetry.Exporter.Jaeger.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Jaeger\OpenTelemetry.Exporter.Jaeger.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_OpenTelemetryProtocolExporterEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetryProtocolExporterEventSource.Log);
}
}
}

View File

@ -20,6 +20,8 @@
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="..\..\src\OpenTelemetry\Internal\DateTimeOffsetExtensions.net452.cs" Link="Implementation\DateTimeOffsetExtensions.net452.cs" /> <Compile Include="..\..\src\OpenTelemetry\Internal\DateTimeOffsetExtensions.net452.cs" Link="Implementation\DateTimeOffsetExtensions.net452.cs" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Exporter.Prometheus.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Exporter.Prometheus.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_PrometheusExporterEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(PrometheusExporterEventSource.Log);
}
}
}

View File

@ -30,6 +30,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Prometheus\OpenTelemetry.Exporter.Prometheus.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Exporter.Prometheus\OpenTelemetry.Exporter.Prometheus.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Exporter.ZPages.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Exporter.ZPages.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_ZPagesExporterEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(ZPagesExporterEventSource.Log);
}
}
}

View File

@ -21,6 +21,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Exporter.ZPages\OpenTelemetry.Exporter.ZPages.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Exporter.ZPages\OpenTelemetry.Exporter.ZPages.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Exporter.Zipkin.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Exporter.Zipkin.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_ZipkinExporterEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(ZipkinExporterEventSource.Log);
}
}
}

View File

@ -8,6 +8,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\TestHttpServer.cs" Link="TestHttpServer.cs" /> <Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\TestHttpServer.cs" Link="TestHttpServer.cs" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,32 @@
// <copyright file="EventSourceTest.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.Diagnostics.Tracing;
using OpenTelemetry.Extensions.Hosting.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Extensions.Hosting.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_HostingExtensionsEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(HostingExtensionsEventSource.Log);
}
}
}

View File

@ -8,6 +8,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation.AspNet.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Instrumentation.AspNet.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_AspNetInstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(AspNetInstrumentationEventSource.Log);
}
}
}

View File

@ -28,6 +28,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.AspNet\OpenTelemetry.Instrumentation.AspNet.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.AspNet\OpenTelemetry.Instrumentation.AspNet.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_AspNetCoreInstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(AspNetCoreInstrumentationEventSource.Log);
}
}
}

View File

@ -28,6 +28,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
@ -40,9 +42,8 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<ProjectReference Include="..\TestApp.AspNetCore.2.1\TestApp.AspNetCore.2.1.csproj" /> <ProjectReference Include="..\TestApp.AspNetCore.2.1\TestApp.AspNetCore.2.1.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation.Grpc.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Instrumentation.Grpc.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_GrpcInstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(GrpcInstrumentationEventSource.Log);
}
}
}

View File

@ -28,5 +28,7 @@
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.Grpc\OpenTelemetry.Instrumentation.Grpc.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.Grpc\OpenTelemetry.Instrumentation.Grpc.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.Http\OpenTelemetry.Instrumentation.Http.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.Http\OpenTelemetry.Instrumentation.Http.csproj" />
<ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" /> <ProjectReference Include="..\..\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Instrumentation.Http.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_HttpInstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(HttpInstrumentationEventSource.Log);
}
}
}

View File

@ -18,6 +18,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="SkipUnlessEnvVarFoundTheoryAttribute.cs" /> <Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\TestHttpServer.cs" Link="TestHttpServer.cs" /> <Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\TestHttpServer.cs" Link="TestHttpServer.cs" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,31 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation.SqlClient.Implementation;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Instrumentation.SqlClient.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_SqlClientInstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(SqlClientInstrumentationEventSource.Log);
}
}
}

View File

@ -17,6 +17,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="SkipUnlessEnvVarFoundTheoryAttribute.cs" /> <Compile Include="..\OpenTelemetry.Tests\Implementation\Internal\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="..\OpenTelemetry.Tests\BaseEventSourceTest.cs" Link="BaseEventSourceTest.cs" />
<Compile Include="..\OpenTelemetry.Tests\TestEventListener.cs" Link="TestEventListener.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,138 @@
// <copyright file="BaseEventSourceTest.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 System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace OpenTelemetry.Tests
{
internal static class BaseEventSourceTest
{
public static void MethodsAreImplementedConsistentlyWithTheirAttributes(EventSource eventSource)
{
foreach (MethodInfo publicMethod in GetEventMethods(eventSource))
{
VerifyMethodImplementation(eventSource, publicMethod);
}
}
private static void VerifyMethodImplementation(EventSource eventSource, MethodInfo eventMethod)
{
using (var listener = new TestEventListener())
{
const long AllKeywords = -1;
listener.EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)AllKeywords);
try
{
object[] eventArguments = GenerateEventArguments(eventMethod);
eventMethod.Invoke(eventSource, eventArguments);
EventWrittenEventArgs actualEvent = listener.Messages.First();
VerifyEventId(eventMethod, actualEvent);
VerifyEventLevel(eventMethod, actualEvent);
if (eventMethod.Name != "ExporterErrorResult")
{
VerifyEventMessage(eventMethod, actualEvent, eventArguments);
}
}
catch (Exception e)
{
var name = eventMethod.DeclaringType.Name + "." + eventMethod.Name;
throw new Exception("Method '" + name + "' is implemented incorrectly.", e);
}
}
}
private static object[] GenerateEventArguments(MethodInfo eventMethod)
{
ParameterInfo[] parameters = eventMethod.GetParameters();
var arguments = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
arguments[i] = GenerateArgument(parameters[i]);
}
return arguments;
}
private static object GenerateArgument(ParameterInfo parameter)
{
if (parameter.ParameterType == typeof(string))
{
return "Test String";
}
if (parameter.ParameterType.IsValueType)
{
return Activator.CreateInstance(parameter.ParameterType);
}
throw new NotSupportedException("Complex types are not supported");
}
private static void VerifyEventId(MethodInfo eventMethod, EventWrittenEventArgs actualEvent)
{
int expectedEventId = GetEventAttribute(eventMethod).EventId;
AssertEqual(nameof(VerifyEventId), expectedEventId, actualEvent.EventId);
}
private static void VerifyEventLevel(MethodInfo eventMethod, EventWrittenEventArgs actualEvent)
{
EventLevel expectedLevel = GetEventAttribute(eventMethod).Level;
AssertEqual(nameof(VerifyEventLevel), expectedLevel, actualEvent.Level);
}
private static void VerifyEventMessage(MethodInfo eventMethod, EventWrittenEventArgs actualEvent, object[] eventArguments)
{
string expectedMessage = eventArguments.Length == 0
? GetEventAttribute(eventMethod).Message
: string.Format(CultureInfo.InvariantCulture, GetEventAttribute(eventMethod).Message, eventArguments);
string actualMessage = string.Format(CultureInfo.InvariantCulture, actualEvent.Message, actualEvent.Payload.ToArray());
AssertEqual(nameof(VerifyEventMessage), expectedMessage, actualMessage);
}
private static void AssertEqual<T>(string methodName, T expected, T actual)
{
if (!expected.Equals(actual))
{
var errorMessage = string.Format(
CultureInfo.InvariantCulture,
"{0} Failed: expected: '{1}' actual: '{2}'",
methodName,
expected,
actual);
throw new Exception(errorMessage);
}
}
private static EventAttribute GetEventAttribute(MethodInfo eventMethod)
{
return (EventAttribute)eventMethod.GetCustomAttributes(typeof(EventAttribute), false).Single();
}
private static IEnumerable<MethodInfo> GetEventMethods(EventSource eventSource)
{
MethodInfo[] methods = eventSource.GetType().GetMethods();
return methods.Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Any());
}
}
}

View File

@ -0,0 +1,43 @@
// <copyright file="EventSourceTest.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 OpenTelemetry.Instrumentation;
using OpenTelemetry.Internal;
using Xunit;
namespace OpenTelemetry.Tests
{
public class EventSourceTest
{
[Fact]
public void EventSourceTest_InstrumentationEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(InstrumentationEventSource.Log);
}
[Fact]
public void EventSourceTest_OpenTelemetryApiEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetryApiEventSource.Log);
}
[Fact]
public void EventSourceTest_OpenTelemetrySdkEventSource()
{
BaseEventSourceTest.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetrySdkEventSource.Log);
}
}
}

View File

@ -0,0 +1,101 @@
// <copyright file="TestEventListener.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 System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Threading;
namespace OpenTelemetry.Tests
{
/// <summary>
/// Event listener for testing event sources.
/// </summary>
internal class TestEventListener : EventListener
{
/// <summary>A queue of events that have been logged.</summary>
private readonly Queue<EventWrittenEventArgs> events;
/// <summary>
/// Lock for event writing tracking.
/// </summary>
private readonly AutoResetEvent eventWritten;
/// <summary>
/// Initializes a new instance of the <see cref="TestEventListener"/> class.
/// </summary>
public TestEventListener()
{
this.events = new Queue<EventWrittenEventArgs>();
this.eventWritten = new AutoResetEvent(false);
this.OnOnEventWritten = e =>
{
this.events.Enqueue(e);
this.eventWritten.Set();
};
}
/// <summary>Gets or sets the handler for event source creation.</summary>
public Action<EventSource> OnOnEventSourceCreated { get; set; }
/// <summary>Gets or sets the handler for event source writes.</summary>
public Action<EventWrittenEventArgs> OnOnEventWritten { get; set; }
/// <summary>Gets the events that have been written.</summary>
public IEnumerable<EventWrittenEventArgs> Messages
{
get
{
if (this.events.Count == 0)
{
this.eventWritten.WaitOne(TimeSpan.FromSeconds(5));
}
while (this.events.Count != 0)
{
yield return this.events.Dequeue();
}
}
}
/// <summary>
/// Clears all event messages so that testing can assert expected counts.
/// </summary>
public void ClearMessages()
{
this.events.Clear();
}
/// <summary>Handler for event source writes.</summary>
/// <param name="eventData">The event data that was written.</param>
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
this.OnOnEventWritten(eventData);
}
/// <summary>Handler for event source creation.</summary>
/// <param name="eventSource">The event source that was created.</param>
protected override void OnEventSourceCreated(EventSource eventSource)
{
// Check for null because this method is called by the base class constror before we can initialize it
Action<EventSource> callback = this.OnOnEventSourceCreated;
if (callback != null)
{
callback(eventSource);
}
}
}
}