Пример #1
0
 public static void Publish(Event @event)
 {
     Eventbus.Publish(@event);
     // after each published event you will wait at least the minimum latency time
     // before doing any other action
     Task.Delay(minimumLatency).Wait();
 }
Пример #2
0
    public async Task IfReceivesAndPublishesEvents()
    {
        // Arrange
        _rabbitMQBus.Subscribe <
            ProcessCompletedIntegrationEvent,
            ProcessCompletedIntegrationEventHandler>();

        // You would need to register dependecies if you use DI here
        // or in some central TestFixture class or similar
        // you would do something like

        //Check if the handler is not called yet, since the event has not been published
        Assert.False(testEventHandler.HandlerCalled);

        // Act
        Eventbus.Publish(new ProcessIntegrationEvent(name: "test"));

        // Assert
        //if handler is called the flag should be true
        Assert.True(testEventHandler.HandlerCalled);
    }
Пример #3
0
        public RabbitMQListener()
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                                .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json")
                                .Build();

                //EventBus
                var eventBusConfig = Configuration.GetSection("EventBus");
                _eventBus = new Eventbus
                {
                    Port     = int.Parse(eventBusConfig["Port"]),
                    HostName = eventBusConfig["HotName"]
                };

                this._factory = new ConnectionFactory()
                {
                    HostName = this._eventBus.HostName
                };
                this._connection = _factory.CreateConnection();
                this._channel    = _connection.CreateModel();

                //SignalR
                var  signalRConfig = Configuration.GetSection("SignalR");
                bool enableSignalR = false;
                bool.TryParse(signalRConfig["Enable"], out enableSignalR);
                this._signalR = new SignalR
                {
                    UseSignalR = enableSignalR,
                    HubUrl     = signalRConfig["Url"],
                    EventName  = signalRConfig["EventName"]
                };
            }
            catch (Exception ex)
            {
                LogManager.LogError("Event bus", ex);
            }
        }
    public void test_system_testing()
    {
        try
        {
            Eventbus eb = new Eventbus("127.0.0.1", 7000);

            Headers h = new Headers();
            h.addHeaders("type", "maths");
            JObject body_add = new JObject();
            body_add.Add("message", "add");

            //sending with time out = 5 secs
            eb.send(
                "pcs.status",                                  //address
                body_add,                                      //body
                "pcs.status",                                  //reply address
                h,                                             //headers
                (new ReplyHandlers("pcs.status",               //replyhandler address
                                   new Action <bool, JObject>( //replyhandler function
                                       (err, message) =>
            {
                if (err == false)
                {
                    SystemTest.i += 5;
                }
            }
                                       )
                                   )
                ),
                5);
            Assert.Equal(5, i);

            JObject body_sub = new JObject();
            body_sub.Add("message", "sub");

            //sending with default time out
            eb.send(
                "pcs.status",                                  //address
                body_sub,                                      //body
                "pcs.status",                                  //reply address
                h,                                             //headers
                (new ReplyHandlers("pcs.status",               //replyhandler address
                                   new Action <bool, JObject>( //replyhandler function
                                       (err, message) =>
            {
                if (err == false)
                {
                    SystemTest.i -= 5;
                }
            }
                                       )
                                   )
                ));
            Assert.Equal(0, i);

            Handlers hand = new Handlers(
                "pcs.status",
                new Action <JObject>(
                    message =>
            {
                SystemTest.i += 5;
            }
                    )
                );

            eb.register("pcs.status", hand);

            //send a message without a replyhandler
            eb.send("pcs.status", body_add, "pcs.status", h);

            //waiting for the message
            System.Threading.Thread.Sleep(1000);

            //unregister
            eb.unregister("pcs.status");

            //publish
            JObject body_close = new JObject();
            body_close.Add("message", "close");
            eb.publish("pcs.status", body_close, h);

            //close the socket
            eb.CloseConnection(5);
            Assert.Equal(5, i);
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e);
        }
    }