예제 #1
0
        public void GetSubscriptionInfos_ReturnsSubscriberCountEqualsTimesAdded_WhenMultipleQueries()
        {
            var querySubscription = new QuerySubscriptions();

            var queryCount = 2;
            var query      = new SingleQuoteQuery(1);

            var queryCount2 = 3;
            var query2      = new SingleQuoteQuery(2);

            for (var i = 0; i < queryCount; i++)
            {
                querySubscription.AddSubscriber(query);
            }

            for (var i = 0; i < queryCount2; i++)
            {
                querySubscription.AddSubscriber(query2);
            }

            var querySubscriptionInfos = querySubscription.GetSubscriptionInfos();

            Assert.Equal(2, querySubscriptionInfos.Count());

            var subcribedQuery  = querySubscriptionInfos.First(x => x.Query == query);
            var subcribedQuery2 = querySubscriptionInfos.First(x => x.Query == query2);

            Assert.Equal(queryCount, subcribedQuery.SubscriberCount);
            Assert.Equal(queryCount2, subcribedQuery2.SubscriberCount);
        }
예제 #2
0
        public void GetSubscriberCount_ShouldReturnSubcriberCount_ForQuery()
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            querySubscription.AddSubscriber(query);
            querySubscription.AddSubscriber(query);

            long subscriberCount = querySubscription.GetSubscriberCount(query);

            Assert.Equal(2, subscriberCount);
        }
예제 #3
0
        public void GetSubscriptionInfos_ReturnsZeroSubscriberCount_WhenQueryCleared()
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            querySubscription.AddSubscriber(query);
            querySubscription.ClearQuery(query);
            var querySubscriptionInfos = querySubscription.GetSubscriptionInfos();

            Assert.Empty(querySubscriptionInfos);
        }
        public async Task GetPrioritizedQueries_ShouldGetPrioritizedQueriesSubscriptionsBySubscriberCountDescending()
        {
            var querySubscriptions = new QuerySubscriptions();

            var query1 = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2 = new TestQuery(2, QueryFunctionType.IntraDay);
            var query3 = new TestQuery(3, QueryFunctionType.IntraDay);

            querySubscriptions.AddSubscriber(query1);
            querySubscriptions.AddSubscriber(query2);
            querySubscriptions.AddSubscriber(query2);
            querySubscriptions.AddSubscriber(query2);
            querySubscriptions.AddSubscriber(query3);
            querySubscriptions.AddSubscriber(query3);

            query1.SetMockQueryContext(_mockContext);
            query2.SetMockQueryContext(_mockContext);
            query3.SetMockQueryContext(_mockContext);

            var priorityRule = new QuerySubscriptionCountRule(_mockContextFactory.Object, querySubscriptions, (new Mock <ILogger <QuerySubscriptionCountRule> >()).Object);

            var prioritizedQueries = (await priorityRule.GetPrioritizedQueries()).ToList();

            Assert.Equal(3, prioritizedQueries.Count);
            Assert.Equal(prioritizedQueries[0], query2);
            Assert.Equal(prioritizedQueries[1], query3);
            Assert.Equal(prioritizedQueries[1], query3);
        }
        public async Task GetPrioritizedQueries_ShouldOnlyIncludeSubscriptionsWithStaleData_Null()
        {
            var querySubscriptions = new QuerySubscriptions();

            var query1 = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2 = new TestQuery(2, QueryFunctionType.SingleQuote);

            querySubscriptions.AddSubscriber(query1);
            querySubscriptions.AddSubscriber(query2);

            query1.SetMockQueryContext(_mockContext);
            query2.SetMockQueryContext(_mockContext);

            var stockData = new SingleQuoteData(
                symbolId: 1,
                ticker: "MSFT",
                high: 1,
                low: 2,
                open: 3,
                previousClose: 4,
                volume: 5,
                change: 6,
                price: 7,
                changePercent: 0.9m,
                lastUpdated: DateTime.UtcNow,
                lastTradingDay: DateTime.UtcNow
                );

            _mockContext.Setup(c => c.GetCachedQueryResult(2)).Returns(Task.FromResult <StockQueryResult>(new SingleQuoteQueryResult("MSFT", stockData)));

            var priorityRule = new QuerySubscriptionCountRule(_mockContextFactory.Object, querySubscriptions, (new Mock <ILogger <QuerySubscriptionCountRule> >()).Object);

            var prioritizedQueries = (await priorityRule.GetPrioritizedQueries()).ToList();

            Assert.Single(prioritizedQueries);
            Assert.Equal(prioritizedQueries[0], query1);
        }
예제 #6
0
        public void GetSubscriptionInfos_ReturnsSubscriberCountEqualTimesAdded(int timesAdded)
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            for (var i = 0; i < timesAdded; i++)
            {
                querySubscription.AddSubscriber(query);
            }

            var querySubscriptionInfos = querySubscription.GetSubscriptionInfos();

            Assert.Single(querySubscriptionInfos);
            Assert.Equal(timesAdded, querySubscriptionInfos.Single().SubscriberCount);
            Assert.Equal(query, querySubscriptionInfos.Single().Query);
        }
예제 #7
0
        public void GetSubscriptionInfos_SubscriberCountNeverLessThanZero_WhenSubscribersRemoved()
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            querySubscription.AddSubscriber(query);

            querySubscription.RemoveSubscriber(query);
            querySubscription.RemoveSubscriber(query);
            querySubscription.RemoveSubscriber(query);

            var querySubscriptionInfos = querySubscription.GetSubscriptionInfos();

            Assert.Single(querySubscriptionInfos);
            Assert.Equal(0, querySubscriptionInfos.Single().SubscriberCount);
            Assert.Equal(query, querySubscriptionInfos.Single().Query);
        }
예제 #8
0
        public void GetSubscriptionInfos_ReturnsSubscriberCountEqualTimesAddedAndSubtracted_WhenSingleQuery(int timesAdded, int timesRemoved)
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            for (var i = 0; i < timesAdded; i++)
            {
                querySubscription.AddSubscriber(query);
            }

            for (var i = 0; i < timesRemoved; i++)
            {
                querySubscription.RemoveSubscriber(query);
            }

            var querySubscriptionInfos = querySubscription.GetSubscriptionInfos();
            var expectedCount          = Math.Abs(timesAdded - timesRemoved);

            Assert.Single(querySubscriptionInfos);
            Assert.Equal(expectedCount, querySubscriptionInfos.Single().SubscriberCount);
            Assert.Equal(query, querySubscriptionInfos.Single().Query);
        }
예제 #9
0
        public void AddSubscriberToSingleQuote(int symbolId)
        {
            var query = new SingleQuoteQuery(symbolId);

            _querySubscriptions.AddSubscriber(query);
        }
예제 #10
0
        public async Task Should_ExecuteQueries()
        {
            var query1 = new TestQuery(1);
            var query2 = new TestQuery(2);
            var query3 = new TestQuery(3);

            _querySubscriptions.AddSubscriber(query1);
            _querySubscriptions.AddSubscriber(query2);
            _querySubscriptions.AddSubscriber(query3);

            await _queryEventBus.ExecutePendingQueries();

            query1.MockQueryContext.Verify(c => c.SaveQueryResultToStore(), Times.Once);
            query2.MockQueryContext.Verify(c => c.SaveQueryResultToStore(), Times.Once);
            query3.MockQueryContext.Verify(c => c.SaveQueryResultToStore(), Times.Once);


            Assert.Equal(0, _querySubscriptions.Count);
        }