public OffsetStoredInboundConnectorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber = new TestSubscriber();

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));
            services
            .AddSilverback()
            .WithConnectionTo <TestBroker>()
            .AddSingletonSubscriber(_testSubscriber);

            services.AddScoped <IOffsetStore, InMemoryOffsetStore>();

            IServiceProvider serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions {
                ValidateScopes = true
            });

            _broker    = (TestBroker)serviceProvider.GetService <IBroker>();
            _connector = new OffsetStoredInboundConnector(_broker, serviceProvider, new NullLogger <OffsetStoredInboundConnector>(),
                                                          new MessageLogger());

            _scopedServiceProvider = serviceProvider.CreateScope().ServiceProvider;

            InMemoryOffsetStore.Clear();
        }
        public OutboundRouterBehaviorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber = new TestSubscriber();

            services.AddSilverback()
            .WithConnectionToMessageBroker(
                options => options
                .AddBroker <TestBroker>()
                .AddBroker <TestOtherBroker>())
            .AddSingletonSubscriber(_testSubscriber);

            services
            .AddSingleton(Substitute.For <IHostApplicationLifetime>())
            .AddLoggerSubstitute();

            _serviceProvider = services.BuildServiceProvider();

            _behavior = (OutboundRouterBehavior)_serviceProvider.GetServices <IBehavior>()
                        .First(s => s is OutboundRouterBehavior);
            _routingConfiguration =
                (OutboundRoutingConfiguration)_serviceProvider
                .GetRequiredService <IOutboundRoutingConfiguration>();
            _broker      = _serviceProvider.GetRequiredService <TestBroker>();
            _otherBroker = _serviceProvider.GetRequiredService <TestOtherBroker>();
        }
Exemplo n.º 3
0
        public InboundConnectorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber    = new TestSubscriber();
            _inboundSubscriber = new WrappedInboundMessageSubscriber();
            _someUnhandledMessageSubscriber = new SomeUnhandledMessageSubscriber();

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));

            services
            .AddSilverback()
            .AddSingletonSubscriber(_testSubscriber)
            .AddSingletonSubscriber(_inboundSubscriber)
            .AddSingletonSubscriber(_someUnhandledMessageSubscriber)
            .WithConnectionTo <TestBroker>(options => options
                                           .AddChunkStore <InMemoryChunkStore>());

            var serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions
            {
                ValidateScopes = true
            });

            _broker             = (TestBroker)serviceProvider.GetService <IBroker>();
            _connector          = new InboundConnector(_broker, serviceProvider);
            _errorPolicyBuilder = new ErrorPolicyBuilder(serviceProvider, NullLoggerFactory.Instance);
        }
        public OffsetStoredInboundConnectorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber = new TestSubscriber();
            services.AddSingleton <ISubscriber>(_testSubscriber);

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));
            services.AddBus();

            services.AddBroker <TestBroker>();

            services.AddScoped <IOffsetStore, InMemoryOffsetStore>();

            _serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions {
                ValidateScopes = true
            });
            _broker    = (TestBroker)_serviceProvider.GetService <IBroker>();
            _connector = new OffsetStoredInboundConnector(_broker, _serviceProvider, new NullLogger <OffsetStoredInboundConnector>(),
                                                          new MessageLogger(new MessageKeyProvider(new[] { new DefaultPropertiesMessageKeyProvider() })));

            _scopedServiceProvider = _serviceProvider.CreateScope().ServiceProvider;

            InMemoryOffsetStore.Clear();
        }
Exemplo n.º 5
0
        public OutboundQueueWorkerTests()
        {
            _queue = new InMemoryOutboundQueue();

            var services = new ServiceCollection();

            services
            .AddSingleton <ILoggerFactory, NullLoggerFactory>()
            .AddSingleton(typeof(ILogger <>), typeof(NullLogger <>))
            .AddSingleton <IOutboundQueueConsumer, InMemoryOutboundQueue>()
            .AddBus()
            .AddBroker <TestBroker>(options => options.AddDeferredOutboundConnector(_ => new InMemoryOutboundQueue()));

            var serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions {
                ValidateScopes = true
            });

            serviceProvider.GetRequiredService <IOutboundRoutingConfiguration>()
            .Add <IIntegrationMessage>(TestEndpoint.Default);

            _broker = (TestBroker)serviceProvider.GetRequiredService <IBroker>();
            _broker.Connect();

            _worker = new OutboundQueueWorker(serviceProvider, _broker, new NullLogger <OutboundQueueWorker>(), new MessageLogger(new MessageKeyProvider(new[] { new DefaultPropertiesMessageKeyProvider() })), true, 100); // TODO: Test order not enforced

            InMemoryOutboundQueue.Clear();
        }
        public OutboundQueueWorkerTests()
        {
            _queue = new InMemoryOutboundQueue();

            var services = new ServiceCollection();

            services
            .AddSingleton <ILoggerFactory, NullLoggerFactory>()
            .AddSingleton(typeof(ILogger <>), typeof(NullLogger <>))
            .AddSingleton <IOutboundQueueConsumer, InMemoryOutboundQueue>()
            .AddSilverback().WithConnectionTo <TestBroker>(options => options.AddDeferredOutboundConnector(_ => new InMemoryOutboundQueue()));

            var serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions {
                ValidateScopes = true
            });

            serviceProvider.GetRequiredService <IOutboundRoutingConfiguration>()
            .Add <IIntegrationMessage>(TestEndpoint.GetDefault());

            _broker = (TestBroker)serviceProvider.GetRequiredService <IBroker>();
            _broker.Connect();

            _worker = new OutboundQueueWorker(serviceProvider, _broker, new NullLogger <OutboundQueueWorker>(), new MessageLogger(), true, 100); // TODO: Test order not enforced

            InMemoryOutboundQueue.Clear();

            _sampleOutboundMessage = new OutboundMessage <TestEventOne>(
                new TestEventOne {
                Content = "Test"
            }, null, TestEndpoint.GetDefault());
            _sampleOutboundMessage.RawContent =
                new JsonMessageSerializer().Serialize(_sampleOutboundMessage.Content, _sampleOutboundMessage.Headers);
        }
Exemplo n.º 7
0
        public void SyncFailedCancelOrderRequestShouldUpdateOrderStatusCorrectlyWithIntermediateUpdate()
        {
            // Initializes the transaction handler
            var transactionHandler = new TestBrokerageTransactionHandler();
            var broker             = new TestBroker(_algorithm, false);

            transactionHandler.Initialize(_algorithm, broker, new BacktestingResultHandler());

            var security = _algorithm.Securities[Ticker];
            var price    = 1.12m;

            security.SetMarketPrice(new Tick(DateTime.Now, security.Symbol, price, price, price));
            var orderRequest = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, 1000, 0, 1.11m, DateTime.Now, "");

            // Mock the the order processor
            var orderProcessorMock = new Mock <IOrderProcessor>();

            orderProcessorMock.Setup(m => m.GetOrderTicket(It.IsAny <int>())).Returns(new OrderTicket(_algorithm.Transactions, orderRequest));
            _algorithm.Transactions.SetOrderProcessor(orderProcessorMock.Object);

            var orderTicket = transactionHandler.Process(orderRequest);

            transactionHandler.HandleOrderRequest(orderRequest);
            Assert.AreEqual(orderTicket.Status, OrderStatus.Submitted);

            var cancelRequest = new CancelOrderRequest(DateTime.Now, orderTicket.OrderId, "");

            Assert.AreEqual(transactionHandler.CancelPendingOrdersSize, 0);
            transactionHandler.Process(cancelRequest);
            Assert.AreEqual(transactionHandler.CancelPendingOrdersSize, 1);
            Assert.AreEqual(cancelRequest.Status, OrderRequestStatus.Processing);
            Assert.IsTrue(cancelRequest.Response.IsSuccess);
            Assert.AreEqual(orderTicket.Status, OrderStatus.CancelPending);

            broker.Scan();
            Assert.AreEqual(orderTicket.Status, OrderStatus.Filled);

            transactionHandler.HandleOrderRequest(cancelRequest);
            Assert.AreEqual(transactionHandler.CancelPendingOrdersSize, 0);
            Assert.AreEqual(orderTicket.Status, OrderStatus.Filled);
            Assert.AreEqual(cancelRequest.Status, OrderRequestStatus.Error);
            Assert.IsTrue(cancelRequest.Response.IsProcessed);
            Assert.IsTrue(cancelRequest.Response.IsError);
            Assert.AreEqual(cancelRequest.Response.ErrorCode, OrderResponseErrorCode.InvalidOrderStatus);

            Assert.AreEqual(_algorithm.OrderEvents.Count, 3);
            Assert.AreEqual(_algorithm.OrderEvents.Count(orderEvent => orderEvent.Status == OrderStatus.CancelPending), 1);
            Assert.AreEqual(_algorithm.OrderEvents.Count(orderEvent => orderEvent.Status == OrderStatus.Submitted), 1);
            Assert.AreEqual(_algorithm.OrderEvents.Count(orderEvent => orderEvent.Status == OrderStatus.Filled), 1);
        }
Exemplo n.º 8
0
        public ProduceBehaviorTests()
        {
            var services = new ServiceCollection();

            services.AddSilverback()
            .WithConnectionToMessageBroker(
                options => options
                .AddBroker <TestBroker>()
                .AddOutbox <InMemoryOutbox>());

            services.AddLoggerSubstitute();

            var serviceProvider = services.BuildServiceProvider();

            _behavior = (ProduceBehavior)serviceProvider.GetServices <IBehavior>()
                        .First(behavior => behavior is ProduceBehavior);
            _broker = serviceProvider.GetRequiredService <TestBroker>();
            _outbox = (InMemoryOutbox)serviceProvider.GetRequiredService <IOutboxWriter>();
        }
        public OutboundProducingBehaviorTests()
        {
            var services = new ServiceCollection();

            _outboundQueue = new InMemoryOutboundQueue();

            services.AddSilverback()
            .WithConnectionTo <TestBroker>(options => options
                                           .AddOutboundConnector()
                                           .AddDeferredOutboundConnector(_ => _outboundQueue));

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));

            var serviceProvider = services.BuildServiceProvider();

            _behavior = (OutboundProducingBehavior)serviceProvider.GetServices <IBehavior>()
                        .First(s => s is OutboundProducingBehavior);
            _broker = (TestBroker)serviceProvider.GetRequiredService <IBroker>();

            InMemoryOutboundQueue.Clear();
        }
        public LoggedInboundConnectorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber = new TestSubscriber();

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));
            services
            .AddSilverback()
            .WithConnectionTo <TestBroker>()
            .AddSingletonSubscriber(_testSubscriber);

            services.AddScoped <IInboundLog, InMemoryInboundLog>();

            _serviceProvider = services.BuildServiceProvider();
            _broker          = (TestBroker)_serviceProvider.GetService <IBroker>();
            _connector       = new LoggedInboundConnector(_broker, _serviceProvider, new NullLogger <LoggedInboundConnector>(),
                                                          new MessageLogger());

            InMemoryInboundLog.Clear();
        }
Exemplo n.º 11
0
        public LoggedInboundConnectorTests()
        {
            var services = new ServiceCollection();

            _testSubscriber = new TestSubscriber();
            services.AddSingleton <ISubscriber>(_testSubscriber);

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));
            services.AddBus();

            services.AddBroker <TestBroker>();

            services.AddScoped <IInboundLog, InMemoryInboundLog>();

            _serviceProvider = services.BuildServiceProvider();
            _broker          = (TestBroker)_serviceProvider.GetService <IBroker>();
            _connector       = new LoggedInboundConnector(_broker, _serviceProvider, new NullLogger <LoggedInboundConnector>(),
                                                          new MessageLogger(new MessageKeyProvider(new[] { new DefaultPropertiesMessageKeyProvider() })));

            InMemoryInboundLog.Clear();
        }
        public OutboundConnectorRouterTests()
        {
            var services = new ServiceCollection();

            _outboundQueue = new InMemoryOutboundQueue();

            services
            .AddBus()
            .AddBroker <TestBroker>(options => options
                                    .AddDeferredOutboundConnector(_ => _outboundQueue)
                                    .AddOutboundConnector());

            services.AddSingleton <ILoggerFactory, NullLoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(NullLogger <>));

            var serviceProvider = services.BuildServiceProvider();

            _connectorRouter      = (OutboundConnectorRouter)serviceProvider.GetServices <ISubscriber>().First(s => s is OutboundConnectorRouter);
            _routingConfiguration = (OutboundRoutingConfiguration)serviceProvider.GetRequiredService <IOutboundRoutingConfiguration>();
            _broker = (TestBroker)serviceProvider.GetRequiredService <IBroker>();

            InMemoryOutboundQueue.Clear();
        }
Exemplo n.º 13
0
        public OutboxWorkerTests()
        {
            var serviceProvider = ServiceProviderHelper.GetServiceProvider(
                services => services
                .AddFakeLogger()
                .AddSilverback()
                .WithConnectionToMessageBroker(
                    options => options
                    .AddBroker <TestBroker>()
                    .AddOutbox <InMemoryOutbox>()
                    .AddOutboxWorker())
                .AddEndpoints(
                    endpoints => endpoints
                    .AddOutbound <TestEventOne>(new TestProducerEndpoint("topic1"))
                    .AddOutbound <TestEventTwo>(new TestProducerEndpoint("topic2"))
                    .AddOutbound <TestEventThree>(new TestProducerEndpoint("topic3a"))
                    .AddOutbound <TestEventThree>(new TestProducerEndpoint("topic3b"))));

            _broker = serviceProvider.GetRequiredService <TestBroker>();
            _broker.ConnectAsync().Wait();

            _worker       = serviceProvider.GetRequiredService <IOutboxWorker>();
            _outboxWriter = serviceProvider.GetRequiredService <IOutboxWriter>();

            _sampleOutboundEnvelope = new OutboundEnvelope <TestEventOne>(
                new TestEventOne {
                Content = "Test"
            },
                null,
                new TestProducerEndpoint("topic1"));
            _sampleOutboundEnvelope.RawMessage =
                AsyncHelper.RunValueTaskSynchronously(
                    () => new JsonMessageSerializer().SerializeAsync(
                        _sampleOutboundEnvelope.Message,
                        _sampleOutboundEnvelope.Headers,
                        MessageSerializationContext.Empty));
        }
Exemplo n.º 14
0
 public OutboundConnectorTests()
 {
     _broker    = new TestBroker();
     _connector = new OutboundConnector(_broker);
 }