Esempio n. 1
0
        public StockObserver(StockObservable stockObservable, TextWriter writer)
        {
            _writer = writer;
            var priceChangedEvents = Observable.FromEventPattern <EventHandler <PriceChangedEventData>, PriceChangedEventData>(
                h => stockObservable.PriceChanged += h,
                h => stockObservable.PriceChanged -= h)
                                     .Select(e => e.EventArgs)
                                     .Synchronize();

            var drasticChanges = from priceChange in priceChangedEvents
                                 group priceChange by priceChange.QuoteSymbol
                                 into company
                                 from priceChangePair in company.Buffer(2, 1)
                                 let changeRatio = CalculateChangeRatio(priceChangePair)
                                                   where changeRatio > MaxChangeRatio
                                                   select new DrasticChange
            {
                Symbol      = company.Key,
                ChangeRatio = changeRatio,
                OldPrice    = priceChangePair[0].Price,
                NewPrice    = priceChangePair[1].Price
            };

            _subscriptions = drasticChanges.Subscribe(OnDrasticChange);
        }
        public void should_process_ticks()
        {
            using (TextWriter writer = new StringWriter())
            {
                Console.SetOut(writer);
                var stockTicker = new StockObservable();

                var sut = new StockObserver(stockTicker, writer);

                stockTicker.Notify("yahoo", 100);
                stockTicker.Notify("amazon", 100);
                stockTicker.Notify("amazon", 125);
                stockTicker.Notify("yahoo", 200);

                writer.Flush();
                writer.ToString().Should().Contain("Stock: yahoo has changed price from 100 to 200, that is a ratio of 1");
                writer.ToString().Should().Contain("Stock: amazon has changed price from 100 to 125, that is a ratio of 0.25");
            }
        }