mirror of https://github.com/abpframework/abp.git
committed by
GitHub
20 changed files with 617 additions and 46 deletions
@ -1,17 +1,38 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.EventBus; |
|||
|
|||
/// <summary>
|
|||
/// Wraps arbitrary event data with a string-based event name for dynamic (type-less) event handling.
|
|||
/// </summary>
|
|||
public class DynamicEventData |
|||
public class DynamicEventData : IEventDataMayHaveTenantId |
|||
{ |
|||
public string EventName { get; } |
|||
|
|||
public object Data { get; } |
|||
|
|||
private Guid? _tenantId; |
|||
private bool _hasTenantId; |
|||
|
|||
public DynamicEventData(string eventName, object data) |
|||
{ |
|||
EventName = Check.NotNullOrWhiteSpace(eventName, nameof(eventName)); |
|||
Data = Check.NotNull(data, nameof(data)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// <see cref="Data"/> is user data, so the tenant id can only come from the transport.
|
|||
/// </summary>
|
|||
public DynamicEventData SetTenantId(Guid? tenantId) |
|||
{ |
|||
_tenantId = tenantId; |
|||
_hasTenantId = true; |
|||
return this; |
|||
} |
|||
|
|||
public bool IsMultiTenant(out Guid? tenantId) |
|||
{ |
|||
tenantId = _tenantId; |
|||
return _hasTenantId; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.EventBus; |
|||
|
|||
public static class EventBusTenantIdHelper |
|||
{ |
|||
/// <summary>
|
|||
/// Throws for an unparsable value so the provider can fail the message
|
|||
/// instead of silently handling it in the host context.
|
|||
/// </summary>
|
|||
public static Guid? Parse(string? value) |
|||
{ |
|||
if (value.IsNullOrWhiteSpace()) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (!Guid.TryParse(value, out var tenantId)) |
|||
{ |
|||
throw new AbpException( |
|||
$"'{EventBusConsts.TenantIdHeaderName}' header of the distributed event was not a valid GUID: {value}"); |
|||
} |
|||
|
|||
return tenantId; |
|||
} |
|||
} |
|||
@ -0,0 +1,72 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs; |
|||
|
|||
public class DynamicBackgroundJob_MultiTenancy_Tests : BackgroundJobsTestBase |
|||
{ |
|||
private readonly IDynamicBackgroundJobManager _dynamicBackgroundJobManager; |
|||
private readonly IBackgroundJobStore _backgroundJobStore; |
|||
private readonly IBackgroundJobExecuter _backgroundJobExecuter; |
|||
private readonly IBackgroundJobSerializer _backgroundJobSerializer; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
public DynamicBackgroundJob_MultiTenancy_Tests() |
|||
{ |
|||
_dynamicBackgroundJobManager = GetRequiredService<IDynamicBackgroundJobManager>(); |
|||
_backgroundJobStore = GetRequiredService<IBackgroundJobStore>(); |
|||
_backgroundJobExecuter = GetRequiredService<IBackgroundJobExecuter>(); |
|||
_backgroundJobSerializer = GetRequiredService<IBackgroundJobSerializer>(); |
|||
_currentTenant = GetRequiredService<ICurrentTenant>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Dynamic_Handler_Job_Should_Run_Under_The_Enqueue_Tenant() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
Guid? observedTenantId = null; |
|||
var executed = false; |
|||
|
|||
_dynamicBackgroundJobManager.RegisterHandler("TenantProbeJob", (ctx, _) => |
|||
{ |
|||
executed = true; |
|||
observedTenantId = ctx.ServiceProvider.GetRequiredService<ICurrentTenant>().Id; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
try |
|||
{ |
|||
string jobIdAsString; |
|||
using (_currentTenant.Change(tenantId)) |
|||
{ |
|||
jobIdAsString = await _dynamicBackgroundJobManager.EnqueueAsync( |
|||
"TenantProbeJob", |
|||
new { CustomerId = "C-1" }); |
|||
} |
|||
|
|||
var jobInfo = await _backgroundJobStore.FindAsync(Guid.Parse(jobIdAsString)); |
|||
jobInfo.ShouldNotBeNull(); |
|||
|
|||
var jobArgs = _backgroundJobSerializer.Deserialize(jobInfo.JobArgs, typeof(DynamicBackgroundJobArgs)); |
|||
|
|||
_currentTenant.Id.ShouldBeNull(); |
|||
|
|||
await _backgroundJobExecuter.ExecuteAsync( |
|||
new JobExecutionContext( |
|||
ServiceProvider, |
|||
typeof(DynamicBackgroundJobExecutorJob), |
|||
jobArgs)); |
|||
|
|||
executed.ShouldBeTrue(); |
|||
observedTenantId.ShouldBe(tenantId); |
|||
} |
|||
finally |
|||
{ |
|||
_dynamicBackgroundJobManager.UnregisterHandler("TenantProbeJob"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed; |
|||
|
|||
[DependsOn(typeof(EventBusTestModule))] |
|||
public class EventBusOutboxTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpDistributedEventBusOptions>(options => |
|||
{ |
|||
options.Outboxes.Configure(config => |
|||
{ |
|||
config.ImplementationType = typeof(FakeEventOutbox); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed; |
|||
|
|||
public class FakeEventOutbox : IEventOutbox, ISingletonDependency |
|||
{ |
|||
public List<OutgoingEventInfo> Events { get; } = new(); |
|||
|
|||
public Task EnqueueAsync(OutgoingEventInfo outgoingEvent) |
|||
{ |
|||
Events.Add(outgoingEvent); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task<List<OutgoingEventInfo>> GetWaitingEventsAsync( |
|||
int maxCount, |
|||
Expression<Func<IOutgoingEventInfo, bool>>? filter = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.FromResult(Events.Take(maxCount).ToList()); |
|||
} |
|||
|
|||
public Task DeleteAsync(Guid id) |
|||
{ |
|||
Events.RemoveAll(x => x.Id == id); |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task DeleteManyAsync(IEnumerable<Guid> ids) |
|||
{ |
|||
var idList = ids.ToList(); |
|||
Events.RemoveAll(x => idList.Contains(x.Id)); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
@ -0,0 +1,113 @@ |
|||
using System; |
|||
using System.Text.Json; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Shouldly; |
|||
using Volo.Abp.EventBus.Local; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.Timing; |
|||
using Volo.Abp.Tracing; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed; |
|||
|
|||
public class LocalDistributedEventBus_MultiTenancy_Test : LocalDistributedEventBusTestBase |
|||
{ |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
public LocalDistributedEventBus_MultiTenancy_Test() |
|||
{ |
|||
_currentTenant = GetRequiredService<ICurrentTenant>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Only_Propagate_The_Tenant_Of_Dynamic_Events() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
var eventBus = ActivatorUtilities.CreateInstance<TestableLocalDistributedEventBus>(ServiceProvider); |
|||
|
|||
using (_currentTenant.Change(tenantId)) |
|||
{ |
|||
eventBus.GetTenantIdToPropagatePublic(typeof(DynamicEventData), new DynamicEventData("test", new { Value = 1 })).ShouldBe(tenantId); |
|||
eventBus.GetTenantIdToPropagatePublic(typeof(MySimpleEventData), new MySimpleEventData(1)).ShouldBeNull(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Restore_The_Publish_Tenant_When_A_Dynamic_Event_Is_Sent_From_The_Outbox() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); |
|||
Guid? observedTenantId = null; |
|||
var handled = false; |
|||
|
|||
using var subscription = DistributedEventBus.Subscribe(eventName, |
|||
new SingleInstanceHandlerFactory(new ActionEventHandler<DynamicEventData>(_ => |
|||
{ |
|||
handled = true; |
|||
observedTenantId = _currentTenant.Id; |
|||
return Task.CompletedTask; |
|||
}))); |
|||
|
|||
var outgoingEvent = new OutgoingEventInfo( |
|||
Guid.NewGuid(), |
|||
eventName, |
|||
JsonSerializer.SerializeToUtf8Bytes(new { Value = 1 }), |
|||
DateTime.Now); |
|||
|
|||
using (_currentTenant.Change(tenantId)) |
|||
{ |
|||
outgoingEvent.SetTenantId(_currentTenant.Id); |
|||
} |
|||
|
|||
_currentTenant.Id.ShouldBeNull(); |
|||
|
|||
await GetRequiredService<LocalDistributedEventBus>().PublishFromOutboxAsync(outgoingEvent, new OutboxConfig("Default")); |
|||
|
|||
handled.ShouldBeTrue(); |
|||
observedTenantId.ShouldBe(tenantId); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Fail_When_The_Tenant_Id_Header_Is_Not_A_Guid() |
|||
{ |
|||
EventBusTenantIdHelper.Parse(null).ShouldBeNull(); |
|||
EventBusTenantIdHelper.Parse(" ").ShouldBeNull(); |
|||
|
|||
Should.Throw<AbpException>(() => EventBusTenantIdHelper.Parse("not-a-guid")); |
|||
} |
|||
|
|||
public class TestableLocalDistributedEventBus : LocalDistributedEventBus |
|||
{ |
|||
public TestableLocalDistributedEventBus( |
|||
IServiceScopeFactory serviceScopeFactory, |
|||
ICurrentTenant currentTenant, |
|||
IUnitOfWorkManager unitOfWorkManager, |
|||
IOptions<AbpDistributedEventBusOptions> abpDistributedEventBusOptions, |
|||
IGuidGenerator guidGenerator, |
|||
IClock clock, |
|||
IEventHandlerInvoker eventHandlerInvoker, |
|||
ILocalEventBus localEventBus, |
|||
ICorrelationIdProvider correlationIdProvider) |
|||
: base( |
|||
serviceScopeFactory, |
|||
currentTenant, |
|||
unitOfWorkManager, |
|||
abpDistributedEventBusOptions, |
|||
guidGenerator, |
|||
clock, |
|||
eventHandlerInvoker, |
|||
localEventBus, |
|||
correlationIdProvider) |
|||
{ |
|||
} |
|||
|
|||
public Guid? GetTenantIdToPropagatePublic(Type eventType, object eventData) |
|||
{ |
|||
return GetTenantIdToPropagate(eventType, eventData); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.Testing; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed; |
|||
|
|||
public class LocalDistributedEventBus_Outbox_MultiTenancy_Test : AbpIntegratedTest<EventBusOutboxTestModule> |
|||
{ |
|||
private readonly IDistributedEventBus _distributedEventBus; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
private readonly FakeEventOutbox _outbox; |
|||
|
|||
public LocalDistributedEventBus_Outbox_MultiTenancy_Test() |
|||
{ |
|||
_distributedEventBus = GetRequiredService<LocalDistributedEventBus>(); |
|||
_currentTenant = GetRequiredService<ICurrentTenant>(); |
|||
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
_outbox = GetRequiredService<FakeEventOutbox>(); |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Write_The_Publish_Tenant_To_The_Outbox_Record() |
|||
{ |
|||
var tenantId = Guid.NewGuid(); |
|||
var eventName = "TestEvent-" + Guid.NewGuid().ToString("N"); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) |
|||
{ |
|||
using (_currentTenant.Change(tenantId)) |
|||
{ |
|||
await _distributedEventBus.PublishAsync(eventName, new { Value = 1 }); |
|||
} |
|||
|
|||
_currentTenant.Id.ShouldBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
var outgoingEvent = _outbox.Events.ShouldHaveSingleItem(); |
|||
outgoingEvent.EventName.ShouldBe(eventName); |
|||
outgoingEvent.GetTenantId().ShouldBe(tenantId); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Write_A_Tenant_To_The_Outbox_Record_Of_A_Typed_Event() |
|||
{ |
|||
using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) |
|||
{ |
|||
using (_currentTenant.Change(Guid.NewGuid())) |
|||
{ |
|||
await _distributedEventBus.PublishAsync(new MySimpleEventData(1)); |
|||
} |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
var outgoingEvent = _outbox.Events.ShouldHaveSingleItem(); |
|||
outgoingEvent.GetTenantId().ShouldBeNull(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue