Exemple #1
0
        public void Should_publish_price_once_started_and_when_MarketData_is_available()
        {
            // Mock and dependencies setup
            var unitOfExecutionsFactory = new UnitOfExecutionsFactory();

            var marketDataProvider = new MarketDataProvider();

            var pastaPricer = new PastaPricerEngine(unitOfExecutionsFactory.GetPool(),
                                                    new[] { "gnocchi(eggs-potatoes-flour)" },
                                                    marketDataProvider,
                                                    this);

            this.ClearDico();
            Check.That(this.lastPrices).IsEmpty();

            pastaPricer.Start();

            Check.That(this.lastPrices).IsEmpty();

            // Turns on market data (note: make the pasta pricer start its dependencies instead?)
            marketDataProvider.Start();

            // A sleep?!? There should be a better way ;-)
            Thread.Sleep(1000);

            // It has publish a price now!
            Check.That(this.lastPrices.Keys).Contains("gnocchi");
        }
        public void Sequencer_should_process_fairly()
        {
            var factory   = new UnitOfExecutionsFactory();
            var thread    = factory.GetDedicatedThread();
            var sequencer = this.BuildSequencer(thread);
            var context   = new RaceConditionDetector();

            // we inject delay deliberately, to ensure queueing happens
            sequencer.Dispatch(() => context.Delay(20));
            var current = 0;
            var failed  = false;

            for (var i = 0; i < 1000; i++)
            {
                var targetCount = i;
                var executor    = ((i % 2) == 0) ? thread : (IUnitOfExecution)sequencer;
                executor.Dispatch(
                    () =>
                {
                    // we check if the task is executed at the proper rank
                    if (targetCount != current)
                    {
                        failed = true;
                    }

                    current++;
                });
            }

            //thread.Dispose();
            Check.That(context.WaitForTasks(1)).IsFalse();
            Check.That(failed).IsFalse();
        }
Exemple #3
0
        public void Factory_should_create_pool_unit()
        {
            var factory  = new UnitOfExecutionsFactory();
            var poolExec = factory.GetPool();

            Check.That(poolExec).IsNotNull();
        }
Exemple #4
0
        public void Factory_should_create_worker_unit()
        {
            var factory = new UnitOfExecutionsFactory();

            var threadExec = factory.GetDedicatedThread();

            Check.That(threadExec).IsNotNull();
        }
Exemple #5
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main()
        {
            ThreadPool.SetMinThreads(Environment.ProcessorCount * 2, 0);
            Console.WriteLine("Welcome to the pasta pricer (powered by the Michonne library).");
            Console.WriteLine("Conflation Y/N?");
            var  option            = Console.ReadLine();
            bool conflationEnabled = false;

            if (option.ToUpper().StartsWith("Y"))
            {
                conflationEnabled = true;
                Console.WriteLine("Conflation enabled!\n");
            }
            else
            {
                Console.WriteLine("NO Conflation\n");
            }

            Console.WriteLine("         Type 'Enter' to start market data inputs.");
            Console.WriteLine("         Then type 'Enter' again, to stop market data inputs.");

            Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.DarkGray;

            var publisher = new ConsolePastaPricerPublisher();

            var marketDataProvider = new AggresiveMarketDataProvider(aggressionFactor: 10, timerPeriodInMsec: 1);

            var unitOfExecutionsFactory = new UnitOfExecutionsFactory();

            ThreadPool.SetMaxThreads(Environment.ProcessorCount * 2, 0);
            ThreadPool.SetMinThreads(Environment.ProcessorCount * 2, 0);

            var pastaPricer = new PastaPricerEngine(unitOfExecutionsFactory.GetPool(), RecipeHelper.GenerateConfigurations(), marketDataProvider, publisher, conflationEnabled);

            pastaPricer.Start();

            // Turns on market data
            marketDataProvider.Start();

            Console.ReadLine();

            Console.WriteLine("----------------\nMarket data is stopping.\n----------------\n");

            marketDataProvider.Stop();
            publisher.CountPublish();

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("----------------\nMarket data stopped. Wait a while, and type 'Enter' to exit.\n----------------\n");

            Console.ReadLine();

            Console.WriteLine("{0} late prices have been published since we stopped the market data.", publisher.PublicationCounter);

            Console.WriteLine("Type Enter to exit the program.");

            Console.ReadLine();
        }
Exemple #6
0
        public void Factory_should_decrease_core_count()
        {
            var factory      = new UnitOfExecutionsFactory();
            var initialCount = factory.AvailableCore;
            var threadExec   = factory.GetDedicatedThread();

            Check.That(threadExec).IsNotNull();
            Check.That(factory.AvailableCore).IsEqualTo(initialCount - 1);
        }
        public void ShouldHaveANiceAPI()
        {
            var factory   = new UnitOfExecutionsFactory();
            var executor  = factory.GetDedicatedThread();
            var processed = new List <int>();

            var processor = executor.BuildProcessor <int>(processed.Add, false);

            processor.Post(4);
            processor.Post(5);

            Check.That(processed).HasSize(2);
        }
Exemple #8
0
        public void ShouldExecuteSynchro()
        {
            var factory = new UnitOfExecutionsFactory();
            var synchronousUnitOfExec = factory.GetSynchronousUnitOfExecution();
            var synchro = new object();

            lock (synchro)
            {
                synchronousUnitOfExec.Dispatch(
                    () =>
                {
                    Check.That(Monitor.TryEnter(synchro)).IsTrue();
                    Monitor.Exit(synchro);
                });
            }
        }
        public void Should_Be_Fast()
        {
            var factory    = new UnitOfExecutionsFactory();
            var unitOfExec = factory.GetSynchronousUnitOfExecution();
            var sequencer  = this.BuildSequencer(unitOfExec);

            var    chrono = new Stopwatch();
            Action action = () => { };

            Check.ThatCode(() =>
            {
                for (var i = 0; i < 100000; i++)
                {
                    sequencer.Dispatch(action);
                }
            }).LastsLessThan(400, TimeUnit.Milliseconds);
        }
Exemple #10
0
        public void Should_publish_price_for_every_registered_pasta()
        {
            this.ClearDico();
            // Mock and dependencies setup
            var marketDataProvider  = new MarketDataProvider();
            var pastasConfiguration = new[]
            {
                "gnocchi(eggs-potatoes-flour)",
                "spaghetti(eggs-flour)",
                "organic spaghetti(organic eggs-flour)",
                "spinach farfalle(eggs-flour-spinach)",
                "tagliatelle(eggs-flour)",
            };

            var unitOfExecutionsFactory = new UnitOfExecutionsFactory();

            var pastaPricer = new PastaPricerEngine(unitOfExecutionsFactory.GetPool(), pastasConfiguration, marketDataProvider, this);

            pastaPricer.Start();

            // Turns on market data (note: make the pasta pricer start its dependencies instead?)
            marketDataProvider.Start();

            // A sleep?!? There should be a better way ;-)
            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (timer.ElapsedMilliseconds < 20000)
            {
                lock (this.lastPrices)
                {
                    if (pastasConfiguration.Length == this.lastPrices.Count)
                    {
                        // all price received
                        break;
                    }
                    Monitor.Wait(this.lastPrices, 100);
                }
            }

            Check.That(this.lastPrices.Keys).Contains("gnocchi");
            Check.That(this.lastPrices.Keys).Contains("spaghetti");
            Check.That(this.lastPrices.Keys).Contains("organic spaghetti");
            Check.That(this.lastPrices.Keys).Contains("spinach farfalle");
            Check.That(this.lastPrices.Keys).Contains("tagliatelle");
        }
Exemple #11
0
        public void ShouldHaveANiceApi()
        {
            // conflating is more about data than about calls
            // the API should reflect that
            var          factory    = new UnitOfExecutionsFactory();
            var          dedicated  = factory.GetDedicatedThread();
            var          act        = 0;
            Action <int> processing = _ => act++;

            var conflatedProcessor = dedicated.BuildConflator(processing);

            dedicated.Dispatch(() => Thread.Sleep(10));
            conflatedProcessor(1);
            conflatedProcessor(2);
            conflatedProcessor(3);
            conflatedProcessor(4);
            Thread.Sleep(30);

            Check.That(act).IsEqualTo(1);
        }
Exemple #12
0
        public void ShouldConflateActions()
        {
            var factory     = new UnitOfExecutionsFactory();
            var dedicated   = factory.GetDedicatedThread();
            var synchroRoot = new object();
            var go          = true;
            var ranTasks    = 0;

            var conflator = new ActionConflator(dedicated);

            dedicated.Dispatch(
                () =>
            {
                lock (synchroRoot)
                {
                    while (go)
                    {
                        Monitor.Wait(synchroRoot, 1000);
                    }
                }
            });

            conflator.Conflate(() => Interlocked.Increment(ref ranTasks));
            conflator.Conflate(() => Interlocked.Increment(ref ranTasks));
            conflator.Conflate(() => Interlocked.Increment(ref ranTasks));

            lock (synchroRoot)
            {
                go = false;
                Monitor.Pulse(synchroRoot);
            }

            Thread.Sleep(100);

            // tasks should be conflated
            Check.That(ranTasks).IsEqualTo(1);
        }
Exemple #13
0
        public void Factory_should_detect_number_of_cores()
        {
            var factory = new UnitOfExecutionsFactory();

            Check.That(factory.CoreCount).IsEqualTo(Environment.ProcessorCount);
        }