public void ConnectEventBus() { var factory = new ConnectionFactory() { HostName = "localhost", DispatchConsumersAsync = true }; _connectionLogger = new Mock <ILogger <DefaultRabbitMQPersistentConnection> >().Object; var retryCount = 5; _connection = new DefaultRabbitMQPersistentConnection(factory, _connectionLogger, retryCount); var builder = new ContainerBuilder(); var container = builder.Build(); _autofac = container.BeginLifetimeScope(builder => { builder.RegisterType <Button1ClickEventHandler>().UsingConstructor(typeof(ILogger)); builder.RegisterType <Button2ClickEventHandler>().UsingConstructor(typeof(ILogger)); builder.RegisterInstance(this).As <ILogger>(); }); _eventBusLogger = new Mock <ILogger <EventBusRabbitMQ.EventBusRabbitMQ> >().Object; var eventBusSubcriptionsManager = new InMemoryEventBusSubscriptionsManager(); _eventBus = new EventBusRabbitMQ.EventBusRabbitMQ(_connection, _eventBusLogger, _autofac, eventBusSubcriptionsManager, SubscriptionClientName, retryCount); IsConnected = true; AppendLog("Connected"); }
public static void AddIntegrationEventHandlers(this IServiceCollection services, IEnumerable <Assembly> assemblies) { List <Type> integrationEventHandlerTypes = assemblies.SelectMany(assembly => assembly.GetTypes()) .Where(x => x.GetInterfaces().Any(y => IsHandlerInterface(y))) .Where(x => x.Name.EndsWith("Handler")) .ToList(); foreach (Type integrationEventHandlerType in integrationEventHandlerTypes) { AddHandlerAsService(services, integrationEventHandlerType); } services.AddSingleton <IEventBusSubscriptionsManager>(sp => { var subManager = new InMemoryEventBusSubscriptionsManager(); foreach (Type integrationEventHandlerType in integrationEventHandlerTypes.Where(x => x.GetInterfaces().Any(y => IsIntegrationEventHandlerInterface(y)))) { IEnumerable <Type> interfaceTypes = integrationEventHandlerType.GetInterfaces().Where(y => IsHandlerInterface(y)); foreach (var interfaceType in interfaceTypes) { Type eventType = interfaceType.GetGenericArguments()[0]; subManager.AddSubscription(eventType, integrationEventHandlerType); } } return(subManager); }); }
public void After_One_Event_Subscription_Should_Contain_The_Event() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); Assert.True(manager.HasSubscriptionsForEvent <TestIntegrationEvent>()); }
public void GetEventKeyReturnsCorrectValue() { var manager = new InMemoryEventBusSubscriptionsManager(); string key = manager.GetEventKey <TestEvent>(); Assert.Equal("TestEvent", key); }
public void HasSubscriptionsForEventByNameReturnsTrueIfItHasSubscription() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); Assert.True(manager.HasSubscriptionsForEvent("TestEvent")); }
public void ThrowsIfHandlerIsAlreadyRegistered() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); Assert.Throws <ArgumentException>(() => manager.AddSubscription <TestEvent, TestEventHandler>()); }
public void IsEmptyReturnsFalseWhenNotEmpty() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); Assert.False(manager.IsEmpty); }
public void IsEmptyAfterClear() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); manager.Clear(); Assert.True(manager.IsEmpty); }
public void After_All_Subscriptions_Are_Deleted_Event_Should_No_Longer_Exists() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); manager.RemoveSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); Assert.False(manager.HasSubscriptionsForEvent <TestIntegrationEvent>()); }
public void GetEventTypeByNameReturnsCorrectType() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); var eventType = manager.GetEventTypeByName("TestEvent"); Assert.Equal(typeof(TestEvent), eventType); }
public void CanGetHandlersForEventByName() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestEvent, TestEventHandler>(); var handlerTypes = manager.GetHandlersForEvent("TestEvent"); Assert.Equal(typeof(TestEventHandler), handlerTypes.First()); }
public void Deleting_Last_Subscription_Should_Raise_On_Deleted_Event() { bool raised = false; var manager = new InMemoryEventBusSubscriptionsManager(); manager.OnEventRemoved += (o, e) => raised = true; manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); manager.RemoveSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); Assert.True(raised); }
public void Get_Handlers_For_Event_Should_Return_All_Handlers() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); manager.AddSubscription <TestIntegrationEvent, TestIntegrationOtherEventHandler>(); var handlers = manager.GetHandlersForEvent <TestIntegrationEvent>(); Assert.Equal(2, handlers.Count()); }
public void After_Deleting_Last_Subscription_Should_Raise_OnEventRemoved() { var manager = new InMemoryEventBusSubscriptionsManager(); var raised = false; manager.OnEventRemoved += (sender, eventArgs) => raised = true; manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); manager.RemoveSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); Assert.True(raised); }
public void IsEmpty_NoHandlersRegistered_ReturnsTrue( InMemoryEventBusSubscriptionsManager sut ) { //Arrange //Act var result = sut.IsEmpty; //Assert result.Should().BeTrue(); }
public void After_One_Subscription_Are_Removed_Event_Shoule_No_Longer_Exists() { var manager = new InMemoryEventBusSubscriptionsManager(); manager.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); manager.RemoveSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); Assert.False(manager.HasSubscriptionsForEvent <TestIntegrationEvent>()); manager.AddDynamicSubscription <TestDynamicIntegrationEventHandler>(nameof(TestIntegrationEvent)); manager.RemoveDynamicSubscription <TestDynamicIntegrationEventHandler>(nameof(TestIntegrationEvent)); Assert.False(manager.HasSubscriptionsForEvent(nameof(TestIntegrationEvent))); }
public void Clear_NoHandlersRegistered_IsEmptyTrue( InMemoryEventBusSubscriptionsManager sut ) { //Arrange //Act sut.Clear(); //Assert sut.IsEmpty.Should().BeTrue(); }
public void AddDynamicSubscription_NoHandlersRegistered_HandlerIsRegistered( InMemoryEventBusSubscriptionsManager sut ) { //Arrange //Act sut.AddDynamicSubscription <TestDynamicIntegrationEventHandler>("eventName"); //Assert sut.GetHandlersForEvent("eventName").ToList().Count.Should().Be(1); }
public void AddDynamicSubscription_HandlerTypeAlreadyRegistered_ThrowArgumentException( InMemoryEventBusSubscriptionsManager sut ) { //Arrange sut.AddDynamicSubscription <TestDynamicIntegrationEventHandler>("eventName"); //Act Action act = () => sut.AddDynamicSubscription <TestDynamicIntegrationEventHandler>("eventName"); //Assert act.Should().Throw <ArgumentException>(); }
public void Clear_HandlersRegistered_IsEmptyTrue( InMemoryEventBusSubscriptionsManager sut ) { //Arrange sut.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); //Act sut.Clear(); //Assert sut.IsEmpty.Should().BeTrue(); }
public void IsEmpty_HandlersRegistered_ReturnsFalse( InMemoryEventBusSubscriptionsManager sut ) { //Arrange sut.AddSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); //Act var result = sut.IsEmpty; //Assert result.Should().BeFalse(); }
public void RemoveDynamicSubscription_HandlerNotRegistered_HandlerIsNotRemoved( InMemoryEventBusSubscriptionsManager sut ) { //Arrange bool eventRemoved = false; sut.OnEventRemoved += new EventHandler <string>(delegate { eventRemoved = true; }); //Act sut.RemoveSubscription <TestIntegrationEvent, TestIntegrationEventHandler>(); //Assert eventRemoved.Should().BeFalse(); }
/// <summary> /// 入口 /// </summary> /// <param name="args">参数</param> static void Main(string[] args) { var service = new ServiceCollection(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType <HelloIntegrationEventHandler>(); containerBuilder.RegisterType <HelloDynamicIntegrationEventHandler>(); service.AddLogging(log => log.AddConsole()); var provider = service.BuildServiceProvider(); var container = containerBuilder.Build(); var factory = new ConnectionFactory(); var manager = new InMemoryEventBusSubscriptionsManager(); var connection = new DefaultIRabbitMQPersisterConnection(factory, provider.GetRequiredService <ILogger <DefaultIRabbitMQPersisterConnection> >()); var eventbus = new EventBusRabbitMQ.EventBusRabbitMQ(connection, provider.GetRequiredService <ILogger <EventBusRabbitMQ.EventBusRabbitMQ> >(), container.BeginLifetimeScope(), manager, "EventBusSubscribTest"); eventbus.Subscribe <HelloIntegrationEvent, HelloIntegrationEventHandler>(); eventbus.SubscrbieDynamic <HelloDynamicIntegrationEventHandler>("HelloIntegrationEvent"); }
protected virtual async Task InitEventBusClient(ServiceBusOption serviceBusOption) { try { //retrieve from servicebus option var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusOption.ConnectionString); var serviceBusPersisterConnection = new DefaultServiceBusPersisterConnection(serviceBusConnection); var eventBusSubcriptionsManager = new InMemoryEventBusSubscriptionsManager(); //resolve if needed using the scope //careful consider create child scope instead of passing container EventBus = new EventBusServiceBus(serviceBusPersisterConnection, eventBusSubcriptionsManager, serviceBusOption.SubscriptionName, CoreDependencyResolver.Container, serviceBusOption, CoreDependencyResolver.Container.Resolve <ILoggerFactory>()); await((EventBusServiceBus)EventBus).InitializeAsync(serviceBusPersisterConnection, eventBusSubcriptionsManager, serviceBusOption.SubscriptionName, RetryPolicy.Default); await Subscribe(); } catch (Exception ex) { Logger.LogError($"{CurrentActor} failed to init service bus client with provided service bus option subscriptionName = {serviceBusOption.SubscriptionName}", ex); throw; } }
public void Test2() { var manager = new InMemoryEventBusSubscriptionsManager(); var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); ILogger <DefaultRabbitMQPersistentConnection> logger = loggerFactory.CreateLogger <DefaultRabbitMQPersistentConnection>(); IConnectionFactory connectionFactory = new ConnectionFactory() { HostName = "localhost" }; IRabbitMQPersistentConnection rabbitMqPersistentConnection = new DefaultRabbitMQPersistentConnection(connectionFactory, logger); var result = manager.HasSubscriptionsForEvent("TestEvent"); ILogger <global::EventBusRabbitMQ.EventBusRabbitMQ> loggers = loggerFactory.CreateLogger <global::EventBusRabbitMQ.EventBusRabbitMQ>(); var builder = new ContainerBuilder(); var container = builder.Build(); var scope = container.BeginLifetimeScope(); IEventBus eventBus = new global::EventBusRabbitMQ.EventBusRabbitMQ(rabbitMqPersistentConnection, loggers, scope, manager, "TestEvent"); TestEvent testEvent = new TestEvent { Msg = "hello world" }; eventBus.Subscribe <TestEvent, TestIntegrationEventHandler>(); }
public void IsEmptyReturnsTrueWhenEmpty() { var manager = new InMemoryEventBusSubscriptionsManager(); Assert.True(manager.IsEmpty); }
public void HasSubscriptionsForEventReturnsFalseIfItHasNoSubscription() { var manager = new InMemoryEventBusSubscriptionsManager(); Assert.False(manager.HasSubscriptionsForEvent <TestEvent>()); }
public static void Main(string[] args) { #region Message And CarDemo // SendMessage mailSend = new SendMessage (new Program ().MailSendMessage); // Console.WriteLine (mailSend ("Robert", "Mike")); // Action delActions = OneVoke; // delActions += TwoVoke; // try { // delActions.Invoke (); // } catch (Exception ex) { // Console.WriteLine ("I Caught you,", ex); // } // Action delActions = OneVoke; // delActions += TwoVoke; // Delegate[] delegates = delActions.GetInvocationList (); // foreach (Action item in delegates) { // try { // item.Invoke (); // } catch (Exception ex) { // Console.WriteLine ($"I Caught you,{ex}"); // } // } // MyButton myButton = new MyButton (); // myButton.OnClick += btn_OnClick; // myButton.OnClick += btn_DoubleClick; // myButton.Click (); #endregion #region CarDemo // CarManager car = new CarManager (); // Driver driver = new Driver (car) { Name = "lao si ji" }; // Passenger passenger = new Passenger (car) { Name = "cheng ke xiaobai" }; // CarNotificationEventData carNotificationEvent = new CarNotificationEventData (driver.Name, passenger.Name); // car.OnCarToRun (driver, passenger); //纯委托版本 // CarNotificationEventHandler carHandler = null; // carHandler += driver.DriverHandle; // carHandler += passenger.PassengerHandle; // carHandler.Invoke (carNotificationEvent); //避免因为委托的某个回调方法调用失败,阻塞 // Delegate[] arrayDelegate = carHandler.GetInvocationList (); // foreach (CarNotificationEventHandler item in arrayDelegate) { // item.Invoke (carNotificationEvent); // } // Console.WriteLine (carHandler.Target + "\n" + carHandler.Method + "\n" + carHandler.GetInvocationList ()); // carHandler.Invoke (carNotificationEvent); //司机和乘客分别订阅发车通知事件 // car.CarNumberNotification += driver.DriveCar; // car.CarNumberNotification += passenger.BoardCar; // car.RunCar (); #endregion #region FishDemo // FishingMan fm = new FishingMan { Name = "Robert" }; // #region general event and delegte demo // // FishingPole fp = new FishingPole (); // // fm.FishingPole = fp; // // // fp.FishingEvent += fm.Update; // // // fp.FishingEvent += new FishingEventHandler ().EventHandle; // // while (fm.Count < 5) { // // fm.Fishing (); // // System.Console.WriteLine ("------"); // // } // #endregion // FishingEventData fishingEventData = new FishingEventData { FishingMan = fm }; // EventBusManager eventBusManager = EventBusManager.Default; // eventBusManager.Trigger<FishingEventData> (fishingEventData); #endregion #region EventBusRegister Demo var serviceProvider = new ServiceCollection() .AddSingleton <IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager> () .AddTransient <DriverHandler> (); var eventBus = new InMemoryEventBusSubscriptionsManager(serviceProvider); RegisterEventBus(eventBus); CarNotificationEventData carNotificationEventData = new CarNotificationEventData("Robert 1", "Passenger 1"); eventBus.BeiginProcess(carNotificationEventData); #endregion }
public void After_Creation_Should_Be_Empty() { var manager = new InMemoryEventBusSubscriptionsManager(); Assert.True(manager.IsEmpty); }