Esempio n. 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);
        }
Esempio n. 2
0
        public void GetSubscriptionInfos_ReturnsCountZero_WhenZeroQueries()
        {
            var querySubscription      = new QuerySubscriptions();
            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);
        }
Esempio n. 4
0
 public StockDataService(IStockDataRepository stockDataRepository,
                         SingleQuoteCache singleQuoteCache,
                         QuerySubscriptions querySubscriptions,
                         ILogger <StockDataService> logger)
 {
     _stockDataRepository = stockDataRepository;
     _singleQuoteCache    = singleQuoteCache;
     _querySubscriptions  = querySubscriptions;
     _logger = logger;
 }
Esempio n. 5
0
        public void GetSubscriberCount_ShouldReturnZero_WhenQueryNotFound()
        {
            var querySubscription = new QuerySubscriptions();

            var query = new SingleQuoteQuery(1);

            long subscriberCount = querySubscription.GetSubscriberCount(query);

            Assert.Equal(0, subscriberCount);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
        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);
        }
Esempio n. 12
0
        public QueryEventBusTests()
        {
            _querySubscriptions = new QuerySubscriptions();

            _queryQueue = new QueryQueue();

            // Will allow same queries to be queued again after a  millisecond
            _queryQueue.SetIntervalKey(() => DateTime.UtcNow.Millisecond);

            _mockContextFactory = new Mock <IQueryContextFactory>();

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

            _queryEventBus = new QueryEventBus(
                queryQueue: _queryQueue,
                queryContextFactory: _mockContextFactory.Object,
                queuePriorityRule: priorityRule,
                logger: (new Mock <ILogger <QueryEventBus> >()).Object
                );
        }
 public QuerySubscriptionCountRule(IQueryContextFactory contextFactory, QuerySubscriptions querySubscriptions, ILogger <QuerySubscriptionCountRule> logger)
 {
     _contextFactory     = contextFactory;
     _querySubscriptions = querySubscriptions;
     _logger             = logger;
 }