Пример #1
0
        public void Initialize()
        {
            fixture = new Fixture();

            testBusEngine = new InMemoryBusEngine();

            mockLoggerFactory = new Mock<ILoggerFactory>();
            mockLogger = new Mock<ILogger>();
            mockCommandContextFactory = new Mock<ICommandContextFactory>();
            mockCommandMessageFactory = new Mock<ICommandMessageFactory>();
            mockEventContextFactory = new Mock<IEventContextFactory>();
            mockEventMessageFactory = new Mock<IEventMessageFactory>();
            mockContainer = new Mock<IContainer>();
            mockScope = new Mock<IScope>();
            mockCorrelationIdGenerator = new Mock<ICorrelationIdGenerator>();

            options = new NybusOptions
            {
                LoggerFactory = mockLoggerFactory.Object,
                CommandContextFactory = mockCommandContextFactory.Object,
                EventContextFactory = mockEventContextFactory.Object,
                CommandMessageFactory = mockCommandMessageFactory.Object,
                EventMessageFactory = mockEventMessageFactory.Object,
                Container = mockContainer.Object,
                CorrelationIdGenerator = mockCorrelationIdGenerator.Object
            };

            mockCommandHandler = new Mock<ICommandHandler<TestCommand>>();
            mockEventHandler = new Mock<IEventHandler<TestEvent>>();

            mockContainer.Setup(p => p.BeginScope()).Returns(mockScope.Object);

            mockLoggerFactory.Setup(p => p.CreateLogger(It.IsAny<string>())).Returns(() => mockLogger.Object);
        }
Пример #2
0
        static void Main(string[] args)
        {
            var logger = new NLogLogger(NLog.LogManager.GetLogger("Test"));

            var options = new NybusOptions
            {
                Logger = logger
            };

            var engine = new InMemoryBusEngine();

            var busBuilder = new NybusBusBuilder(engine, options);

            var consoleLocker = new object();

            IDisposable testEventHandle = busBuilder.ObserveEvent<TestEvent>().Buffer(TimeSpan.FromSeconds(1)).Subscribe(tc =>
            {
                lock (consoleLocker)
                {
                    Console.WriteLine($"Received {tc.Count} events during the last 1 seconds");
                    foreach (var i in tc)
                    {
                        Console.WriteLine($"\t{i.CorrelationId:D} - {i.Message.Id} - '{i.Message.Message}'");
                    }
                    Console.WriteLine();
                }
            });

            IDisposable testCommandHandle = busBuilder.ObserveCommand<TestCommand>().Buffer(10).Subscribe(tc =>
            {
                lock (consoleLocker)
                {
                    Console.WriteLine($"Received {tc.Count} commands");
                    foreach (var i in tc)
                    {
                        Console.WriteLine($"\t{i.CorrelationId:D} - {i.Message.Id} - '{i.Message.Message}'");
                    }
                    Console.WriteLine();
                }
            });


            IBus bus = busBuilder.Build();

            Task.WaitAll(bus.Start());

            Task.WhenAll(InvokeManyEvents(bus, 50), InvokeManyCommands(bus, 50)).WaitAndUnwrapException();

            Console.WriteLine("Press ENTER to exit.");

            Console.ReadLine();

            testEventHandle.Dispose();

            testCommandHandle.Dispose();

            Task.WaitAll(bus.Stop());

        }