public async Task PublishAsync_Listener実行前に例外が発生した場合はリスナーが実行されずにログが出力される() { var services = new ServiceCollection(); services.AddMediatR(typeof(MixedRequest).Assembly); services.AddSingleton<MixedTaskHolder>(); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ListenerRegistry(); registry.Add(typeof(MixedRequest)); registry.Add(typeof(MixedNotification)); var holder = provider.GetService<MixedTaskHolder>(); var testLogger = new TestLogger(); var client = new DefaultMediatorClient(new ServiceRegistry(), registry, serviceFactory!, new TestScopedServiceFactoryFactory(), testLogger); await client.PublishAsync("mixed", new { }); // Fire and forgetのため一旦スリープ Thread.Sleep(1000); holder.Messages.Should().HaveCount(0); testLogger.Errors.Should().HaveCount(1); }
public async Task PublishAsync_RequestもNotificationも実行される() { var services = new ServiceCollection(); services.AddMediatR(typeof(MixedRequest).Assembly); services.AddSingleton<MixedTaskHolder>(); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ListenerRegistry(); registry.Add(typeof(MixedRequest)); registry.Add(typeof(MixedNotification)); var holder = provider.GetService<MixedTaskHolder>(); var client = new DefaultMediatorClient(new ServiceRegistry(), registry, serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); await client.PublishAsync("mixed", new { }); // Fire and forgetのため一旦スリープ Thread.Sleep(1000); holder.Messages.Should().HaveCount(2); holder.Messages.Contains("request").Should().BeTrue(); holder.Messages.Contains("notification").Should().BeTrue(); }
public void Add_typeがnullの場合は例外が発生する() { var registry = new ListenerRegistry(); Action act = () => registry.Add(null); act.Should().Throw <ArgumentNullException>(); }
public async Task PublishAsync_Mediatorが実行される() { var services = new ServiceCollection(); services.AddMediatR(typeof(ServicePing).Assembly); services.AddSingleton<TaskHolder>(); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ListenerRegistry(); registry.Add(typeof(MediatorClientTestPang)); var client = new DefaultMediatorClient(new ServiceRegistry(), registry, serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), new TestLogger()); var holder = provider.GetService<TaskHolder>(); var pang = new MediatorClientTestPang(); await client.PublishAsync(nameof(MediatorClientTestPang), pang); Thread.Sleep(1000); //それぞれで10だけまたしているため、1000あれば終わっているはず。 await Task.WhenAll(holder.Tasks); holder.Messages.Should().HaveCount(3).And.Contain("1", "2", "3"); holder.Pangs.Should().HaveCount(3); var paramBang = holder.Pangs[0]; foreach (var bangItem in holder.Pangs) { paramBang.Should().Be(bangItem); } }
public void Add_同じパスが登録された場合は両方とも保持される() { var registry = new ListenerRegistry(); registry.Add(typeof(Pang)); registry.Add(typeof(Pang2)); registry.GetKeys().Should().HaveCount(1); var notifications = registry.GetListeners(registry.GetKeys().First()); notifications.Should().HaveCount(2); var expectedList = new Queue <Type>(new[] { typeof(Pang), typeof(Pang2) }); for (var i = 0; i < expectedList.Count; i++) { var expect = expectedList.Dequeue(); notifications.Should().Contain(n => n.ListenerType == expect); } }
public void Add_AsEventが付与されている場合は追加される() { var registry = new ListenerRegistry(); registry.Add(typeof(Pang)); var ev = registry.GetListeners("pang"); ev.Should().NotBeNull(); registry.GetKeys().Should().HaveCount(1); }
public static IServiceCollection AddMediatRServiceLike(this IServiceCollection services, Assembly assembly, params Type[] filterTypes) { if (!(services.LastOrDefault(s => s.ServiceType == typeof(ServiceRegistry))? .ImplementationInstance is ServiceRegistry serviceRegistry)) { serviceRegistry = new ServiceRegistry(); } services.TryAddSingleton(serviceRegistry); var serviceDescriptions = assembly.GetTypes() .Where(MediatorServiceDescription.CanServicalize); foreach (var serviceDescription in serviceDescriptions) { serviceRegistry.Add(serviceDescription, true, filterTypes); } if (!(services.LastOrDefault(s => s.ServiceType == typeof(ListenerRegistry))? .ImplementationInstance is ListenerRegistry listenerRegistry)) { listenerRegistry = new ListenerRegistry(); } services.TryAddSingleton(listenerRegistry); var listenerDescriptions = assembly.GetTypes() .Where(MediatorListenerDescription.CanListenerize); foreach (var listenerDescription in listenerDescriptions) { listenerRegistry.Add(listenerDescription); } services.TryAddSingleton <IServiceLikeContextAccessor, ServiceLikeContextAccessor>(); services.TryAddTransient(typeof(IMediatorClient), provider => { var servRegistry = provider.GetService <ServiceRegistry>(); var lisRegistry = provider.GetService <ListenerRegistry>(); var serviceFactory = provider.GetService <ServiceFactory>(); var scopedServiceFactoryFactory = new ScopedServiceFactoryFactory(provider.GetService <IServiceScopeFactory>()); var serviceLikeLogger = provider.GetService <IServiceLikeLogger>(); return(new DefaultMediatorClient( servRegistry, lisRegistry, serviceFactory, scopedServiceFactoryFactory, serviceLikeLogger)); }); return(services); }
public void Add_AsEventが複数付与されている場合は複数追加される() { var registry = new ListenerRegistry(); registry.Add(typeof(MultiPang)); var ev1 = registry.GetListeners("pang.1"); ev1.Should().NotBeNull(); var ev2 = registry.GetListeners("pang.2"); ev2.Should().NotBeNull(); registry.GetKeys().Should().HaveCount(2); }
public async Task PublishAsync_Notification内で例外が発生しても続行される() { var services = new ServiceCollection(); services.AddMediatR(typeof(ExceptionPang).Assembly); var provider = services.BuildServiceProvider(); var serviceFactory = provider.GetService<ServiceFactory>(); var registry = new ListenerRegistry(); registry.Add(typeof(ExceptionPang)); var logger = new TestLogger(); var client = new DefaultMediatorClient(new ServiceRegistry(), registry, serviceFactory!, new InternalScopedServiceFactoryFactory(serviceFactory!), logger); await client.PublishAsync(nameof(ExceptionPang), new { }); // Fire and forgetのため一旦スリープ Thread.Sleep(2000); logger.Errors.Should().HaveCount(1); }