Ejemplo n.º 1
0
        public async Task <bool> StartupAsync(BatchHandler <TRequest, TResponse> batchHandler, TimeSpan minimumTimeInterval, TimeSpan maximumTimeInterval, int maximumCount)
        {
            await ShutdownAsync();

            if (minimumTimeInterval > maximumTimeInterval)
            {
                throw new ArgumentException("Maximum time interval cannot be less than the minimum time interval");
            }

            if (minimumTimeInterval <= TimeSpan.Zero)
            {
                throw new ArgumentException("Minimum time interval must be greater than zero");
            }

            this.userBatchProcessorHandler = batchHandler;
            this.MinimumTimeInterval       = minimumTimeInterval;
            this.MaximumTimeInterval       = maximumTimeInterval;
            this.MaximumCount = maximumCount;
            IsRunning         = true;

            batchProcessor = new AsyncListProcessor <List <InternalBatchProcessorItem> >(batchProcessor_DoWork, () => IsRunning);
            if (!await batchProcessor.StartupAsync())
            {
                TraceQueue.Trace(this, TracingLevel.Warning, "Failed to startup batch processor.  Shutting down...");
                await ShutdownAsync();

                return(false);
            }

            return(true);
        }
        public async Task StartupShutdownTest()
        {
            var processor = new AsyncListProcessor <AsyncListTestObject>(processItem);

            Assert.IsTrue(await processor.StartupAsync(), "Failed to startup list processor");
            Assert.IsTrue(processor.IsRunning, "IsRunning was false after startup");

            //Process some items
            var items = new List <AsyncListTestObject>();

            for (int i = 0; i < 10; i++)
            {
                var item = new AsyncListTestObject();
                items.Add(item);
                processor.Add(item);
            }
            Assert.IsTrue(Task.WaitAll(items.Select(item => item.Task).ToArray(), 10000), "Failed to process task lists");

            await processor.ShutdownAsync();

            Assert.IsFalse(processor.IsRunning, "IsRunning still true after shutdown");
        }