Skip to content

Commit

Permalink
- Remove event listener. rely on Debug.Fail
Browse files Browse the repository at this point in the history
    - Use existing mock types
  • Loading branch information
maryamariyan committed Aug 11, 2021
1 parent 25fb241 commit d2cf158
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ public class EventSourceTests : ICollectionFixture<EventSourceTests>
{
}

internal class TestEventListener : EventListener
{
private volatile bool _disposed;
private ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();

public IEnumerable<EventWrittenEventArgs> EventData => _events;

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (!_disposed)
{
_events.Enqueue(eventData);
}
}

public override void Dispose()
{
_disposed = true;
base.Dispose();
}
}

[Collection(nameof(EventSourceTests))]
public class DependencyInjectionEventSourceTests: IDisposable
{
Expand Down Expand Up @@ -248,6 +226,28 @@ public void EmitsServiceRealizationFailedEvent()
private T GetProperty<T>(EventWrittenEventArgs data, string propName)
=> (T)data.Payload[data.PayloadNames.IndexOf(propName)];

private class TestEventListener : EventListener
{
private volatile bool _disposed;
private ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();

public IEnumerable<EventWrittenEventArgs> EventData => _events;

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (!_disposed)
{
_events.Enqueue(eventData);
}
}

public override void Dispose()
{
_disposed = true;
base.Dispose();
}
}

public void Dispose()
{
_listener.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
using Microsoft.Extensions.DependencyInjection.Tests;
using Xunit;

namespace Microsoft.Extensions.DependencyInjection
{
public class ABC { }

public class ServiceCollectionDescriptorExtensionsTest
{
[Fact]
internal void GetService_FactoryCallSite_Transient_DoesNotFail()
{
TestEventListener listener = new TestEventListener();
listener.EnableEvents(DependencyInjectionEventSource.Log, EventLevel.Verbose);
IServiceProvider serviceProvider = null;

try
{
IServiceCollection services = new ServiceCollection();
services.Add(ServiceDescriptor.Describe(typeof(ABC), (sp) => new ABC(), ServiceLifetime.Transient));

var sp = services.BuildServiceProvider(ServiceProviderMode.Dynamic);
serviceProvider = sp
.CreateScope()
.ServiceProvider;

serviceProvider.GetService<ABC>();

for (int i = 0; i < 50; i++)
{
serviceProvider.GetService<ABC>();
System.Threading.Thread.Sleep(10); // Give the background thread time to compile
}

Assert.Null(listener.EventData.FirstOrDefault(e => e.EventName == nameof(DependencyInjectionEventSource.Log.ServiceRealizationFailed)));
}
finally
{
((IDisposable)serviceProvider).Dispose();
}
}

[Fact]
public void Add_AddsDescriptorToServiceDescriptors()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,51 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
using Microsoft.Extensions.DependencyInjection.Tests;
using Xunit;

namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
{
public class CallSiteFactoryTest
{
[Fact]
public void GetService_FactoryCallSite_Transient_DoesNotFail()
{
IServiceProvider serviceProvider = null;
try
{
var collection = new ServiceCollection();
collection.Add(ServiceDescriptor.Describe(typeof(FakeService), (sp) => new FakeService(), ServiceLifetime.Transient));
collection.Add(ServiceDescriptor.Describe(typeof(IFakeService), (sp) => new FakeService(), ServiceLifetime.Transient));

serviceProvider = collection
.BuildServiceProvider(ServiceProviderMode.Dynamic)
.CreateScope()
.ServiceProvider;

Type expectedType = typeof(FakeService);

Assert.Equal(expectedType, serviceProvider.GetService(typeof(IFakeService)).GetType());
Assert.Equal(expectedType, serviceProvider.GetService(typeof(FakeService)).GetType());

for (int i = 0; i < 50; i++)
{
serviceProvider.GetService(typeof(IFakeService));
serviceProvider.GetService(typeof(FakeService));
Thread.Sleep(10); // Give the background thread time to compile
}
}
finally
{
((IDisposable)serviceProvider).Dispose();
}
}

[Fact]
public void CreateCallSite_Throws_IfTypeHasNoPublicConstructors()
{
Expand Down

0 comments on commit d2cf158

Please sign in to comment.