Exemplo n.º 1
0
        public async Task ShouldDelayOnChaos()
        {
            ChaosScenario noChaos    = new ChaosScenario("No Chaos", failureRate: 0.0);
            ChaosScenario delayChaos = new ChaosScenario("Full Chaos", delay: 10);

            var sw = Stopwatch.StartNew();

            // Expect no delays from Chaos that is not configured
            sw.Restart();
            ChaosMonkey.Chaos(noChaos);
            Assert.Less(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");

            sw.Restart();
            await ChaosMonkey.ChaosAsync(noChaos);

            Assert.Less(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");

            // Expect delays from Chaos that is configured
            sw.Restart();
            ChaosMonkey.Chaos(delayChaos);
            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");

            sw.Restart();
            await ChaosMonkey.ChaosAsync(delayChaos);

            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");
        }
Exemplo n.º 2
0
        public async Task ShouldThrowOnExpectedChaos()
        {
            ChaosScenario noChaos   = new ChaosScenario("No Chaos", failureRate: 0.0);
            ChaosScenario fullChaos = new ChaosScenario("Full Chaos", failureRate: 1.0);

            // No exception is expected
            ChaosMonkey.Chaos(noChaos);
            await ChaosMonkey.ChaosAsync(noChaos);

            Assert.Throws <ChaosException>(() => ChaosMonkey.Chaos(fullChaos));
            Assert.ThrowsAsync <ChaosException>(async() => await ChaosMonkey.ChaosAsync(fullChaos));
        }
Exemplo n.º 3
0
 private ScannedImage TransferImage(WiaBackgroundEventLoop eventLoop, int pageNumber, out bool cancel)
 {
     try
     {
         var transfer = ScanParams.NoUI ? backgroundWiaTransfer : foregroundWiaTransfer;
         ChaosMonkey.MaybeError(0, new COMException("Fail", -2147467259));
         using (var stream = transfer.Transfer(pageNumber, eventLoop, WiaApi.Formats.BMP))
         {
             if (stream == null)
             {
                 cancel = true;
                 return(null);
             }
             cancel = false;
             using (Image output = Image.FromStream(stream))
             {
                 using (var result = scannedImageHelper.PostProcessStep1(output, ScanProfile))
                 {
                     if (blankDetector.ExcludePage(result, ScanProfile))
                     {
                         return(null);
                     }
                     ScanBitDepth bitDepth = ScanProfile.UseNativeUI ? ScanBitDepth.C24Bit : ScanProfile.BitDepth;
                     var          image    = new ScannedImage(result, bitDepth, ScanProfile.MaxQuality, ScanProfile.Quality);
                     image.SetThumbnail(thumbnailRenderer.RenderThumbnail(result));
                     scannedImageHelper.PostProcessStep2(image, result, ScanProfile, ScanParams, pageNumber);
                     return(image);
                 }
             }
         }
     }
     catch (NoPagesException)
     {
         if (ScanProfile.PaperSource != ScanSource.Glass && pageNumber == 1)
         {
             // No pages were in the feeder, so show the user an error
             throw new NoPagesException();
         }
         // At least one page was scanned but now the feeder is empty, so exit normally
         cancel = true;
         return(null);
     }
     catch (ScanDriverException)
     {
         throw;
     }
     catch (Exception e)
     {
         throw new ScanDriverUnknownException(e);
     }
 }
        static void Main(string[] args)
        {
            var bus          = new TopicBasedPubSub();
            var orderPrinter = new OrderPrinter();

            var cashier         = new Cashier(bus);
            var assitantManager = new QueuedHandle <PriceOrder>("AssistantManager", new AssistantManager(bus));
            var db    = new ConcurrentDictionary <Guid, OrderDocument>();
            var jesse = new QueuedHandle <CookFood>("Cook:Jesse", new Cook("Jesse", 600, db, bus));
            var walt  = new QueuedHandle <CookFood>("Cook:Walt", new Cook("Walt", 700, db, bus));
            var gus   = new QueuedHandle <CookFood>("Cook:Gus", new Cook("Gus", 1500, db, bus));

            var dispatcher = new MoreFairDispatcher <CookFood>("FairDispatcher", new[] { jesse, walt, gus });

            var chaosMonkey = new ChaosMonkey <CookFood>(25, 25, dispatcher);
            var alarmClock  = new AlarmClock(bus);

            var startables = new List <IStartable> {
                assitantManager, jesse, walt, gus, dispatcher, alarmClock
            };
            var waiter = new Waiter("Heisenberg", bus);

            var house = new MinionHouse(bus);

            bus.Subscribe(chaosMonkey);
            bus.Subscribe(assitantManager);
            bus.Subscribe(cashier);
            bus.Subscribe <OrderPlaced>(house);
            bus.Subscribe <OrderCompleted>(house);
            bus.Subscribe(orderPrinter);
            bus.Subscribe(alarmClock);

            startables.ForEach(x => x.Start());

            for (var i = 0; i < 50; i++)
            {
                waiter.PlaceOrder(new LineItem("Crystal Meth", 3));
                Console.WriteLine("Order placed");
            }

            Console.WriteLine("Getting outstanding orders");

            Task.Run(() => GetOutstandingOrdersAndPay(cashier));

            Task.Run(() => StartMonitoringQueueDepthsAsync(startables));
            StartMonitoringMinionHouseAsync(house).Wait();
        }
Exemplo n.º 5
0
        private async Task <(ScannedImage, bool)> TransferImage(WiaBackgroundEventLoop eventLoop, int pageNumber)
        {
            return(await Task.Factory.StartNew(() =>
            {
                try
                {
                    ChaosMonkey.MaybeError(0, new COMException("Fail", -2147467259));
                    using (var stream = DoTransfer(pageNumber, eventLoop, WiaApi.Formats.BMP))
                    {
                        if (stream == null)
                        {
                            return (null, true);
                        }

                        using (Image output = Image.FromStream(stream))
                        {
                            using (var result = scannedImageHelper.PostProcessStep1(output, ScanProfile))
                            {
                                if (blankDetector.ExcludePage(result, ScanProfile))
                                {
                                    return (null, false);
                                }

                                ScanBitDepth bitDepth = ScanProfile.UseNativeUI ? ScanBitDepth.C24Bit : ScanProfile.BitDepth;
                                var image = new ScannedImage(result, bitDepth, ScanProfile.MaxQuality, ScanProfile.Quality);
                                scannedImageHelper.PostProcessStep2(image, result, ScanProfile, ScanParams, pageNumber);
                                string tempPath = scannedImageHelper.SaveForBackgroundOcr(result, ScanParams);
                                scannedImageHelper.RunBackgroundOcr(image, ScanParams, tempPath);
                                return (image, false);
                            }
                        }
                    }
                }
                catch (NoPagesException)
                {
                    if (ScanProfile.PaperSource != ScanSource.Glass && pageNumber == 1)
                    {
                        // No pages were in the feeder, so show the user an error
                        throw new NoPagesException();
                    }

                    // At least one page was scanned but now the feeder is empty, so exit normally
                    return (null, true);
                }
            }, TaskCreationOptions.LongRunning));
        }
Exemplo n.º 6
0
        public async Task ShouldNotThrowOrDelayWhenNotEnabled()
        {
            // Disable the monkey, it will be restored to its original state no matter what in TearDown.
            ChaosMonkey.IsEnabled = false;

            ChaosScenario fullChaos = new ChaosScenario("Full Chaos", failureRate: 1.0, delay: 1000);

            var sw = Stopwatch.StartNew();

            // Expect no delays or exceptions when disabled
            sw.Restart();
            ChaosMonkey.Chaos(fullChaos);
            Assert.Less(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");

            sw.Restart();
            await ChaosMonkey.ChaosAsync(fullChaos);

            Assert.Less(sw.ElapsedMilliseconds, 10, "No chaos resulted in an unexpected delay.");
        }