Ejemplo n.º 1
0
        public void PublishAndSubscribe()
        {
            var broker = new DataEventBroker();

            int publishingCallCount                    = 0;
            int publishedCallCount                     = 0;
            int allEventsSubscriberCallCount           = 0;
            int customEventsSubscriberCallCount        = 0;
            EventHandler <EventArgs> publishingHandler = (sender, args) => {
                publishingCallCount++;
            };
            EventHandler <EventArgs> publishedHandler = (sender, args) => {
                publishedCallCount++;
                Assert.AreEqual(publishingCallCount, publishedCallCount);
            };
            EventHandler <EventArgs> allEventsSubscribedHandler = (sender, args) => {
                allEventsSubscriberCallCount++;
                Assert.AreEqual(allEventsSubscriberCallCount, publishingCallCount);
                Assert.AreEqual(allEventsSubscriberCallCount - 1, publishedCallCount);
            };
            EventHandler <CustomEventArgs> customEventsSubscribedHandler = (sender, args) => {
                customEventsSubscriberCallCount++;
            };

            broker.Publishing += publishingHandler;
            broker.Published  += publishedHandler;

            broker.Subscribe(allEventsSubscribedHandler);
            broker.Subscribe(customEventsSubscribedHandler);

            broker.Publish(this, EventArgs.Empty);
            Assert.AreEqual(1, allEventsSubscriberCallCount);
            Assert.AreEqual(0, customEventsSubscriberCallCount);

            broker.Publish(this, new CustomEventArgs());
            Assert.AreEqual(2, allEventsSubscriberCallCount);
            Assert.AreEqual(1, customEventsSubscriberCallCount);
        }
Ejemplo n.º 2
0
        public HealthAction(IFCMAPIConnection apiConnection, ILogger logger, DataEventBroker <ConnectionStatusEvent> connStatusBroker)
        {
            _connStatusBroker = connStatusBroker;
            _conn             = apiConnection;
            _log = logger;
            _connStatusBroker.Subscribe(async x =>
            {
                logger.Debug("[HealthAction()] {ConnectionStatus} : {Message}",
                             x.ConnectionStatus.ToString(),
                             x.Message
                             );
                await Task.Run(async() =>
                {
                    switch (x.ConnectionStatus)
                    {
                    case ConnectionStatus.NotConnected:
                        await apiConnection.Reconnect();
                        break;

                    case ConnectionStatus.Connecting:
                        //await CheckConnectingStatus();
                        await CheckConnectingStatus();
                        break;

                    case ConnectionStatus.ConnectionReady:
                        break;

                    case ConnectionStatus.ConnectionError:
                        await apiConnection.Reconnect();
                        break;

                    default:
                        //throw new ArgumentOutOfRangeException();
                        break;
                    }
                });
            });
            //apiConnection.Connect().Wait();
            _connStatusBroker.Publish(ConnectionStatusEvent.GetEvent(apiConnection.APIStatus, "Ready for Connect()."));
        }
Ejemplo n.º 3
0
        public void Unsubscribe()
        {
            var broker = new DataEventBroker();

            var counterHandler = new CounterHanlder();

            broker.Subscribe(new EventHandler <EventArgs>(counterHandler.Handler));
            broker.Subscribe(new EventHandler <CustomEventArgs>(counterHandler.Handler));

            var anotherHandlerCounter = 0;

            broker.Subscribe <EventArgs>((sender, args) => {
                anotherHandlerCounter++;
            });

            Assert.True(broker.Unsubscribe(new EventHandler <EventArgs>(counterHandler.Handler)));
            Assert.False(broker.Unsubscribe(new EventHandler <CustomEventArgs>(counterHandler.Handler)));

            broker.Publish(this, new CustomEventArgs());
            Assert.AreEqual(0, counterHandler.Counter);
            Assert.AreEqual(1, anotherHandlerCounter);
        }