public void AsyncRequestQueueClearTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            queue.Clear();
            Assert.Equal(0, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos;

            logEventInfos = queue.DequeueBatch(10);
            int result = logEventInfos.Length;

            Assert.Equal(0, result);
            Assert.Equal(0, queue.RequestCount);
        }
        public void AsyncRequestQueueWithDiscardBehaviorTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Discard);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Discard, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(3, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos = queue.DequeueBatch(10);
            Assert.Equal(0, queue.RequestCount);

            // ev1 is lost
            Assert.Same(logEventInfos[0].LogEvent, ev2.LogEvent);
            Assert.Same(logEventInfos[1].LogEvent, ev3.LogEvent);
            Assert.Same(logEventInfos[2].LogEvent, ev4.LogEvent);
            Assert.Same(logEventInfos[0].Continuation, ev2.Continuation);
            Assert.Same(logEventInfos[1].Continuation, ev3.Continuation);
            Assert.Same(logEventInfos[2].Continuation, ev4.Continuation);
        }
        public void AsyncRequestQueueClearTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(new NLog.Config.LoggingConfiguration(), 3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            queue.Clear();
            Assert.Equal(0, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos;
            int actualCount;

            logEventInfos = queue.DequeueBatch(10, out actualCount);


            Assert.Equal(0, actualCount);
            Assert.Equal(0, queue.RequestCount);
        }
Exemplo n.º 4
0
        private void handleQueue(AsyncRequestQueue q, int cnt)
        {
            Stopwatch w = new Stopwatch();

            w.Start();
            Dict <int, int> dict = new Dict <int, int>(cnt);

            for (int i = 0; i < cnt; i++)
            {
                dict.Add(i, -1);

                AsyncElt popped = (AsyncElt)q.PushAndOptionalPop(new AsyncElt(i));
                processPopped(dict, popped);
            }
            while (true)
            {
                AsyncElt popped = (AsyncElt)q.Pop();
                if (popped == null)
                {
                    break;
                }
                processPopped(dict, popped);
            }

            Assert.AreEqual(cnt, dict.Count);

            foreach (var kvp in dict)
            {
                Assert.AreEqual(2 * kvp.Key, kvp.Value, "value for {0} was {1}", kvp.Key, kvp.Value);
            }
            Console.WriteLine("Elapsed: {0}ms for q={1}", w.ElapsedMilliseconds, q);
        }
Exemplo n.º 5
0
        public void TestAsync()
        {
            int cnt = 1047;

            handleQueue(AsyncRequestQueue.Create(0), cnt);
            handleQueue(AsyncRequestQueue.Create(1), cnt);
            handleQueue(AsyncRequestQueue.Create(2), cnt);
            handleQueue(AsyncRequestQueue.Create(3), cnt);
            handleQueue(AsyncRequestQueue.Create(4), cnt);
        }
        public void RaiseEventLogEventDropped_OnLogItems()
        {
            const int RequestsLimit       = 2;
            const int EventsCount         = 5;
            int       discardedItemsCount = 0;

            int ExpectedDiscardedItemsCount = EventsCount - RequestsLimit;
            AsyncRequestQueue requestQueue  = new AsyncRequestQueue(RequestsLimit, AsyncTargetWrapperOverflowAction.Discard);

            requestQueue.LogEventDropped += (o, e) => { discardedItemsCount++; };

            for (int i = 0; i < EventsCount; i++)
            {
                requestQueue.Enqueue(new AsyncLogEventInfo());
            }

            Assert.Equal(ExpectedDiscardedItemsCount, discardedItemsCount);
        }
        public void RaiseEventLogEventQueueGrow_OnLogItems()
        {
            const int RequestsLimit = 2;
            const int EventsCount   = 5;
            const int ExpectedCountOfGrovingTimes = 2;
            const int ExpectedFinalSize           = 8;
            int       grovingItemsCount           = 0;

            AsyncRequestQueue requestQueue = new AsyncRequestQueue(RequestsLimit, AsyncTargetWrapperOverflowAction.Grow);

            requestQueue.LogEventQueueGrow += (o, e) => { grovingItemsCount++; };

            for (int i = 0; i < EventsCount; i++)
            {
                requestQueue.Enqueue(new AsyncLogEventInfo());
            }

            Assert.Equal(ExpectedCountOfGrovingTimes, grovingItemsCount);
            Assert.Equal(ExpectedFinalSize, requestQueue.RequestLimit);
        }
Exemplo n.º 8
0
        public MainWindowViewModel()
        {
            _searchCommand = new RelayCommand(SearchManga);
            _browseCommand = new RelayCommand(BrowseClicked);
            _saveCommand   = new RelayCommand(SaveClicked);

            // load output path from user settings
            _outputPath = Properties.Settings.Default.OutputPath;

            Mangas           = new AsyncObservableCollection <IMangaRecord>();
            Chapters         = new AsyncObservableCollection <ChapterViewModel>();
            SelectedChapters = new AsyncObservableCollection <IChapterRecord>();

            _requestQueue = new AsyncRequestQueue();
            _requestQueue.TasksCompleted += _requestQueue_TasksCompleted;
            _requestQueue.Initialize();

            _downloadManager = new DownloadManagerViewModel();

            // load all enabled scrapers
            _scrapers = ScraperLoader.Instance.EnabledScrapers;

            if (!string.IsNullOrEmpty(Properties.Settings.Default.SelectedScraper))
            {
                CurrentScraper = _scrapers.FirstOrDefault(s => s.Name == Properties.Settings.Default.SelectedScraper);
            }

            if (CurrentScraper == null)
            {
                CurrentScraper = _scrapers.First();
            }

            _downloadFormatProviders      = ScraperLoader.Instance.DownloadFormatProviders;
            CurrentDownloadFormatProvider = ScraperLoader.Instance.GetFirstOrDefaultDownloadFormatProvider(Properties.Settings.Default.DownloadFormatProvider);

            if (Properties.Settings.Default.EnablePreload)
            {
                PreloadMangas();
            }
        }
        public void AsyncRequestQueueWithGrowBehaviorTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(new NLog.Config.LoggingConfiguration(), 3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            int actualCount;

            AsyncLogEventInfo[] logEventInfos = queue.DequeueBatch(10, out actualCount);


            Assert.Equal(4, actualCount);
            Assert.Equal(0, queue.RequestCount);

            // ev1 is lost
            Assert.Same(logEventInfos[0].LogEvent, ev1.LogEvent);
            Assert.Same(logEventInfos[1].LogEvent, ev2.LogEvent);
            Assert.Same(logEventInfos[2].LogEvent, ev3.LogEvent);
            Assert.Same(logEventInfos[3].LogEvent, ev4.LogEvent);
            Assert.Same(logEventInfos[0].Continuation, ev1.Continuation);
            Assert.Same(logEventInfos[1].Continuation, ev2.Continuation);
            Assert.Same(logEventInfos[2].Continuation, ev3.Continuation);
            Assert.Same(logEventInfos[3].Continuation, ev4.Continuation);
        }
Exemplo n.º 10
0
        public void AsyncRequestQueueEventDiscardedEventTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue  = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Discard);
            var raised = false;
            var eh     = new BufferOverflowEventHandler(
                (a, b, c, d) =>
            {
                Assert.Same(a, AsyncTargetWrapperOverflowAction.Discard);
                Assert.Same(ev1, d);
                raised = true;
            });

            queue.Enqueue(ev1);
            queue.Enqueue(ev2);
            queue.Enqueue(ev3);
            Assert.False(raised);
            queue.Enqueue(ev4);
            Assert.True(raised);
        }
        public void AsyncRequestQueueWithBlockBehavior()
        {
            var queue = new AsyncRequestQueue(10, AsyncTargetWrapperOverflowAction.Block);

            ManualResetEvent producerFinished = new ManualResetEvent(false);

            int pushingEvent = 0;

            ThreadPool.QueueUserWorkItem(
                s =>
            {
                // producer thread
                for (int i = 0; i < 1000; ++i)
                {
                    AsyncLogEventInfo logEvent = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
                    logEvent.LogEvent.Message  = "msg" + i;

                    // Console.WriteLine("Pushing event {0}", i);
                    pushingEvent = i;
                    queue.Enqueue(logEvent);
                }

                producerFinished.Set();
            });

            // consumer thread
            AsyncLogEventInfo[] logEventInfos;
            int total = 0;

            while (total < 500)
            {
                int left = 500 - total;

                logEventInfos = queue.DequeueBatch(left);
                int got = logEventInfos.Length;
                Assert.True(got <= queue.RequestLimit);
                total += got;
            }

            Thread.Sleep(500);

            // producer is blocked on trying to push event #510
            Assert.Equal(510, pushingEvent);
            queue.DequeueBatch(1);
            total++;
            Thread.Sleep(500);

            // producer is now blocked on trying to push event #511

            Assert.Equal(511, pushingEvent);
            while (total < 1000)
            {
                int left = 1000 - total;

                logEventInfos = queue.DequeueBatch(left);
                int got = logEventInfos.Length;
                Assert.True(got <= queue.RequestLimit);
                total += got;
            }

            // producer should now finish
            producerFinished.WaitOne();
        }