mirror of https://github.com/abpframework/abp.git
3 changed files with 354 additions and 24 deletions
@ -0,0 +1,142 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Autofac; |
|||
using Autofac.Core; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Microsoft.Extensions.Hosting; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.Internal.Telemetry; |
|||
using Volo.Abp.Internal.Telemetry.Activity; |
|||
using Volo.Abp.Internal.Telemetry.Activity.Contracts; |
|||
using Volo.Abp.Internal.Telemetry.Constants; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore; |
|||
|
|||
public class TelemetryApplicationLifetime_Tests |
|||
{ |
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public void Should_Release_Applications_And_Autofac_Containers_After_Telemetry_Completes(bool useIntegratedTest) |
|||
{ |
|||
var references = new List<ApplicationReferences>(); |
|||
for (var i = 0; i < 10; i++) |
|||
{ |
|||
references.Add(CreateAndDisposeApplication(useIntegratedTest)); |
|||
} |
|||
|
|||
var collected = SpinWait.SpinUntil(() => |
|||
{ |
|||
GC.Collect(); |
|||
GC.WaitForPendingFinalizers(); |
|||
return references.All(reference => !reference.Application.IsAlive && !reference.Container.IsAlive); |
|||
}, TimeSpan.FromSeconds(10)); |
|||
|
|||
collected.ShouldBeTrue("Disposed applications and their root Autofac containers must be collectible."); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.NoInlining)] |
|||
private static ApplicationReferences CreateAndDisposeApplication(bool useIntegratedTest) |
|||
{ |
|||
if (useIntegratedTest) |
|||
{ |
|||
using var test = new TelemetryIntegratedTest(); |
|||
return test.GetReferences(); |
|||
} |
|||
|
|||
using var application = AbpApplicationFactory.Create<TelemetryTestModule>(options => options.UseAutofac()); |
|||
application.Initialize(); |
|||
try |
|||
{ |
|||
return ObserveApplication(application, application.ServiceProvider); |
|||
} |
|||
finally |
|||
{ |
|||
application.Shutdown(); |
|||
} |
|||
} |
|||
|
|||
private static ApplicationReferences ObserveApplication(IAbpApplication application, IServiceProvider serviceProvider) |
|||
{ |
|||
serviceProvider.GetService<IWebHostEnvironment>().ShouldBeNull(); |
|||
serviceProvider.GetRequiredService<IAbpHostEnvironment>().EnvironmentName.ShouldBe(Environments.Development); |
|||
serviceProvider.GetRequiredService<IConfiguration>().GetValue<bool?>("Abp:Telemetry:IsEnabled").ShouldBeNull(); |
|||
|
|||
var completion = serviceProvider.GetRequiredService<TelemetryCompletion>(); |
|||
completion.ScopeDisposed.Task.Wait(TimeSpan.FromSeconds(10)).ShouldBeTrue(); |
|||
completion.ActivityCount.ShouldBe(1); |
|||
completion.ActivityName.ShouldBe(ActivityNameConsts.ApplicationRun); |
|||
|
|||
var lifetimeScope = (ISharingLifetimeScope)serviceProvider.GetRequiredService<ILifetimeScope>(); |
|||
return new ApplicationReferences(new WeakReference(application), new WeakReference(lifetimeScope.RootLifetimeScope)); |
|||
} |
|||
|
|||
private sealed record ApplicationReferences(WeakReference Application, WeakReference Container); |
|||
|
|||
private sealed class TelemetryIntegratedTest : AbpIntegratedTest<TelemetryTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
public ApplicationReferences GetReferences() |
|||
{ |
|||
return ObserveApplication(Application, ServiceProvider); |
|||
} |
|||
} |
|||
|
|||
[DependsOn(typeof(AbpAspNetCoreModule), typeof(AbpAutofacModule))] |
|||
public class TelemetryTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.Replace(ServiceDescriptor.Singleton<IConfiguration>(new ConfigurationBuilder().Build())); |
|||
context.Services.AddSingleton<TelemetryCompletion>(); |
|||
context.Services.Replace(ServiceDescriptor.Scoped<ITelemetryActivityEventBuilder, TestActivityEventBuilder>()); |
|||
context.Services.Replace(ServiceDescriptor.Singleton(Substitute.For<ITelemetryActivityStorage>())); |
|||
context.Services.Replace(ServiceDescriptor.Singleton(Substitute.For<ITelemetryActivitySender>())); |
|||
} |
|||
} |
|||
|
|||
public class TelemetryCompletion |
|||
{ |
|||
public TaskCompletionSource<bool> ScopeDisposed { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); |
|||
public int ActivityCount; |
|||
public string ActivityName { get; set; } |
|||
} |
|||
|
|||
public class TestActivityEventBuilder : ITelemetryActivityEventBuilder, IDisposable |
|||
{ |
|||
private readonly TelemetryCompletion _completion; |
|||
|
|||
public TestActivityEventBuilder(TelemetryCompletion completion) |
|||
{ |
|||
_completion = completion; |
|||
} |
|||
|
|||
public Task<ActivityEvent> BuildAsync(ActivityContext context) |
|||
{ |
|||
Interlocked.Increment(ref _completion.ActivityCount); |
|||
_completion.ActivityName = context.Current.Get<string>(ActivityPropertyNames.ActivityName); |
|||
return Task.FromResult<ActivityEvent>(null); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
_completion.ScopeDisposed.TrySetResult(true); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,182 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Internal.Telemetry; |
|||
using Volo.Abp.Internal.Telemetry.Activity; |
|||
using Volo.Abp.Internal.Telemetry.Activity.Contracts; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Telemetry; |
|||
|
|||
public class TelemetryService_Tests |
|||
{ |
|||
[Theory] |
|||
[InlineData(TaskStatus.RanToCompletion)] |
|||
[InlineData(TaskStatus.Faulted)] |
|||
[InlineData(TaskStatus.Canceled)] |
|||
public void Should_Unsubscribe_And_Release_Service_When_Activity_Completes(TaskStatus status) |
|||
{ |
|||
using var started = new ManualResetEventSlim(); |
|||
using var release = new ManualResetEventSlim(); |
|||
var serviceProvider = Substitute.For<IServiceProvider>(); |
|||
var builder = Substitute.For<ITelemetryActivityEventBuilder>(); |
|||
builder.BuildAsync(Arg.Any<ActivityContext>()).Returns(Task.FromResult<ActivityEvent?>(null)); |
|||
serviceProvider.GetService(typeof(ITelemetryActivityEventBuilder)).Returns(builder); |
|||
serviceProvider.GetService(typeof(ITelemetryActivityStorage)).Returns(Substitute.For<ITelemetryActivityStorage>()); |
|||
serviceProvider.GetService(typeof(ITelemetryActivitySender)).Returns(Substitute.For<ITelemetryActivitySender>()); |
|||
var scope = Substitute.For<IServiceScope>(); |
|||
scope.ServiceProvider.Returns(serviceProvider); |
|||
var scopeFactory = Substitute.For<IServiceScopeFactory>(); |
|||
scopeFactory.CreateScope().Returns(_ => |
|||
{ |
|||
started.Set(); |
|||
if (!release.Wait(TimeSpan.FromSeconds(10))) |
|||
{ |
|||
throw new TimeoutException(); |
|||
} |
|||
|
|||
if (status == TaskStatus.Faulted) |
|||
{ |
|||
throw new InvalidOperationException(); |
|||
} |
|||
|
|||
if (status == TaskStatus.Canceled) |
|||
{ |
|||
throw new OperationCanceledException(); |
|||
} |
|||
|
|||
return scope; |
|||
}); |
|||
|
|||
var service = StartActivity(scopeFactory); |
|||
try |
|||
{ |
|||
started.Wait(TimeSpan.FromSeconds(10)).ShouldBeTrue(); |
|||
GetProcessExitHandlers().Length.ShouldBe(1); |
|||
GetCancelKeyPressHandlers().Length.ShouldBe(1); |
|||
} |
|||
finally |
|||
{ |
|||
release.Set(); |
|||
} |
|||
|
|||
WaitForHandlersToBeRemoved(); |
|||
for (var i = 0; i < 3; i++) |
|||
{ |
|||
GC.Collect(); |
|||
GC.WaitForPendingFinalizers(); |
|||
} |
|||
|
|||
service.IsAlive.ShouldBeFalse(); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(TaskStatus.RanToCompletion)] |
|||
[InlineData(TaskStatus.Faulted)] |
|||
[InlineData(TaskStatus.Canceled)] |
|||
public void Should_Not_Leave_Handlers_For_Already_Completed_Tasks(TaskStatus status) |
|||
{ |
|||
var task = status switch |
|||
{ |
|||
TaskStatus.Faulted => Task.FromException(new InvalidOperationException()), |
|||
TaskStatus.Canceled => Task.FromCanceled(new CancellationToken(true)), |
|||
_ => Task.CompletedTask |
|||
}; |
|||
|
|||
RegisterExitHandlers(task); |
|||
|
|||
GetProcessExitHandlers().ShouldBeEmpty(); |
|||
GetCancelKeyPressHandlers().ShouldBeEmpty(); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task Should_Wait_For_Pending_Task_On_Exit(bool cancelKeyPress) |
|||
{ |
|||
var completion = new TaskCompletionSource<bool>(); |
|||
var handlerStarted = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
|||
RegisterExitHandlers(completion.Task); |
|||
var handler = cancelKeyPress ? GetCancelKeyPressHandlers().Single() : GetProcessExitHandlers().Single(); |
|||
var handlerTask = Task.Run(() => |
|||
{ |
|||
handlerStarted.SetResult(true); |
|||
if (handler is ConsoleCancelEventHandler consoleHandler) |
|||
{ |
|||
consoleHandler(null, null!); |
|||
} |
|||
else |
|||
{ |
|||
((EventHandler)handler)(null, EventArgs.Empty); |
|||
} |
|||
}); |
|||
|
|||
try |
|||
{ |
|||
await handlerStarted.Task.WaitAsync(TimeSpan.FromSeconds(10)); |
|||
await Task.Delay(100); |
|||
handlerTask.IsCompleted.ShouldBeFalse(); |
|||
} |
|||
finally |
|||
{ |
|||
completion.TrySetResult(true); |
|||
} |
|||
|
|||
await handlerTask.WaitAsync(TimeSpan.FromSeconds(10)); |
|||
WaitForHandlersToBeRemoved(); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.NoInlining)] |
|||
private static WeakReference StartActivity(IServiceScopeFactory scopeFactory) |
|||
{ |
|||
var service = new TelemetryService(scopeFactory); |
|||
service.AddActivityAsync("TestActivity").GetAwaiter().GetResult(); |
|||
return new WeakReference(service); |
|||
} |
|||
|
|||
private static void RegisterExitHandlers(Task task) |
|||
{ |
|||
typeof(TelemetryService).GetMethod("WaitForTelemetryTaskOnExit", BindingFlags.NonPublic | BindingFlags.Static)! |
|||
.Invoke(null, new object[] { task }); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.NoInlining)] |
|||
private static void WaitForHandlersToBeRemoved() |
|||
{ |
|||
SpinWait.SpinUntil(() => GetProcessExitHandlers().Length == 0 && GetCancelKeyPressHandlers().Length == 0, |
|||
TimeSpan.FromSeconds(10)).ShouldBeTrue(); |
|||
} |
|||
|
|||
private static Delegate[] GetProcessExitHandlers() |
|||
{ |
|||
return typeof(AppDomain).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) |
|||
.Where(field => typeof(Delegate).IsAssignableFrom(field.FieldType)) |
|||
.Select(field => field.GetValue(field.IsStatic ? null : AppDomain.CurrentDomain)) |
|||
.OfType<Delegate>() |
|||
.SelectMany(handler => handler.GetInvocationList()) |
|||
.Where(IsTelemetryHandler) |
|||
.ToArray(); |
|||
} |
|||
|
|||
private static Delegate[] GetCancelKeyPressHandlers() |
|||
{ |
|||
return typeof(Console).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) |
|||
.Where(field => typeof(Delegate).IsAssignableFrom(field.FieldType)) |
|||
.Select(field => field.GetValue(null)) |
|||
.OfType<Delegate>() |
|||
.SelectMany(handler => handler.GetInvocationList()) |
|||
.Where(IsTelemetryHandler) |
|||
.ToArray(); |
|||
} |
|||
|
|||
private static bool IsTelemetryHandler(Delegate handler) |
|||
{ |
|||
return handler.Target?.GetType().DeclaringType == typeof(TelemetryService); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue