Esempio n. 1
0
        public void Dequeue_ShouldBeThreadSafe_FIFO()
        {
            for (var i = 0; i < 10; i++)
            {
                var queryQueue = new QueryQueue();
                var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);
                var query2     = new TestQuery(2, QueryFunctionType.IntraDay);
                var query3     = new TestQuery(3, QueryFunctionType.Weekly);
                var queryList  = new List <TestQuery>()
                {
                    query1, query2, query3
                };
                var dequeueList = new ConcurrentBag <TestQuery>();

                queryQueue.Enqueue(query1);
                queryQueue.Enqueue(query2);
                queryQueue.Enqueue(query3);

                queryList.AsParallel().ForAll(q =>
                {
                    var dequeued = (TestQuery)queryQueue.Dequeue(1).Single();
                    dequeueList.Add(dequeued);
                });

                Assert.True(queryQueue.IsEmpty());
                Assert.Equal(3, dequeueList.Count);

                Assert.Contains(query1, dequeueList);
                Assert.Contains(query2, dequeueList);
                Assert.Contains(query3, dequeueList);
            }
        }
Esempio n. 2
0
        public void Enqueue_ShouldReturnFalse_WhenQueryIsAddedAgainAfterItWasDequeued()
        {
            var queryQueue = new QueryQueue();
            var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);

            queryQueue.Enqueue(query1);
            queryQueue.Dequeue(1);
            bool isAddSuccessful = queryQueue.Enqueue(query1);

            Assert.False(isAddSuccessful);
        }
Esempio n. 3
0
        public void Dequeue_ShouldBeFIFO_WhenMultipleQueriesAdded_CoupleItems()
        {
            var queryQueue = new QueryQueue();
            var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2     = new TestQuery(2, QueryFunctionType.IntraDay);

            queryQueue.Enqueue(query1);
            queryQueue.Enqueue(query2);

            var queries = queryQueue.Dequeue(1);

            Assert.Equal(query1, queries.Single());
        }
Esempio n. 4
0
        public void GetCurrentIntervalQueriesRanCount_ShouldReturnExecutedQueryCount_WhenAllExecuting()
        {
            var queryQueue = new QueryQueue();
            var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2     = new TestQuery(2, QueryFunctionType.IntraDay);
            var query3     = new TestQuery(3, QueryFunctionType.Weekly);

            queryQueue.Enqueue(query1);
            queryQueue.Enqueue(query2);
            queryQueue.Enqueue(query3);
            queryQueue.Dequeue(3);

            Assert.Equal(3, queryQueue.GetCurrentIntervalQueriesRanCount());
        }
Esempio n. 5
0
        public void Dequeue_ShouldReturnAllItems_WhenDequeueCountExceedsQueueCount()
        {
            var queryQueue = new QueryQueue();
            var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2     = new TestQuery(2, QueryFunctionType.IntraDay);
            var query3     = new TestQuery(3, QueryFunctionType.Weekly);

            queryQueue.Enqueue(query1);
            queryQueue.Enqueue(query2);
            queryQueue.Enqueue(query3);

            var queries = queryQueue.Dequeue(100).ToList();

            Assert.Equal(3, queries.Count);
            Assert.True(queryQueue.IsEmpty());
        }
Esempio n. 6
0
        public void Dequeue_ShouldBeFIFO_WhenMultipleQueriesAdded_ManyItems()
        {
            var queryQueue = new QueryQueue();
            var query1     = new TestQuery(1, QueryFunctionType.SingleQuote);
            var query2     = new TestQuery(2, QueryFunctionType.IntraDay);
            var query3     = new TestQuery(3, QueryFunctionType.Weekly);
            var query4     = new TestQuery(4, QueryFunctionType.Monthly);

            queryQueue.Enqueue(query1);
            queryQueue.Enqueue(query2);
            queryQueue.Enqueue(query3);
            queryQueue.Enqueue(query4);

            var queries = queryQueue.Dequeue(3).ToList();

            Assert.Equal(query1, queries[0]);
            Assert.Equal(query2, queries[1]);
            Assert.Equal(query3, queries[2]);
            Assert.Equal(1, queryQueue.Count);
        }
        public MainWindowViewModel()
        {
            var dispatcher = Dispatcher.CurrentDispatcher;

            ProductManager.OnStockClear += () =>
            {
                ProductSet.Clear();
                _productNames.Clear();
            };

            ProductManager.OnProductAdd += product =>
            {
                if (_productNames.ContainsKey(product.Name))
                {
                    _productNames[product.Name].Number += product.Number;
                }
                else
                {
                    ProductSet.Add(product);
                    _productNames.Add(product.Name, product);
                }
            };

            ProductManager.OnQueryAdd += query =>
            {
                dispatcher.Invoke(() => QueryQueue.Enqueue(query));
            };

            ProductManager.OnQuerySent += () => { dispatcher.Invoke(() => QueryQueue.Dequeue()); };

            ProductManager.OnQueryDbFail += msg =>
            {
                MessageBox.Show(msg, "The Failed Query");
            };

            ProductManager.StartWork();
        }
Esempio n. 8
0
        public async Task GetCurrentIntervalQueriesRanCount_ShouldResetOnNextInterval()
        {
            var queryQueue = new QueryQueue();

            queryQueue.SetIntervalKey(() => DateTime.UtcNow.Millisecond);

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

            queryQueue.Enqueue(query1);
            queryQueue.Enqueue(query2);
            queryQueue.Enqueue(query3);

            queryQueue.Dequeue(2);
            queryQueue.MarkAsExecuted(query1);
            queryQueue.MarkAsExecuted(query2);

            Assert.Equal(2, queryQueue.GetCurrentIntervalQueriesRanCount());

            await Task.Delay(10);

            Assert.Equal(0, queryQueue.GetCurrentIntervalQueriesRanCount());
        }