# HG changeset patch # User stevenh7776 stevenhollidge@hotmail.com # Date 1329750290 -25200 # Node ID 29ed98d659e9fb46542faa817bc78d15b85c4cdb # Parent bd448bb6e0babbe8caf3edb92d95a56b7a5613b3 Adding WebClientShim files diff -r bd448bb6e0ba -r 29ed98d659e9 .hgignore --- a/.hgignore Mon Feb 20 21:53:40 2012 +0700 +++ b/.hgignore Mon Feb 20 22:04:50 2012 +0700 @@ -1,5 +1,6 @@ syntax: glob -*.csproj.user */obj/* -*/bin/* -*.ncb +*/bin/* +*.ncb *.suo +*.csproj.user +*/obj/* diff -r bd448bb6e0ba -r 29ed98d659e9 Stocks/Stocks.Common/IWebClientShim.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Stocks/Stocks.Common/IWebClientShim.cs Mon Feb 20 22:04:50 2012 +0700 @@ -0,0 +1,7 @@ +namespace Stocks.Common +{ + public interface IWebClientShim + { + string DownloadString(string address); + } +} diff -r bd448bb6e0ba -r 29ed98d659e9 Stocks/Stocks.Common/Stocks.Common.csproj --- a/Stocks/Stocks.Common/Stocks.Common.csproj Mon Feb 20 21:53:40 2012 +0700 +++ b/Stocks/Stocks.Common/Stocks.Common.csproj Mon Feb 20 22:04:50 2012 +0700 @@ -53,6 +53,7 @@ + @@ -60,6 +61,7 @@ + diff -r bd448bb6e0ba -r 29ed98d659e9 Stocks/Stocks.Common/WebClientShim.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Stocks/Stocks.Common/WebClientShim.cs Mon Feb 20 22:04:50 2012 +0700 @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net; + +namespace Stocks.Common +{ + public class WebClientShim : IWebClientShim + { + private readonly WebClient _webClient; + + public WebClientShim(WebClient webClient) + { + _webClient = webClient; + } + + public string DownloadString(string address) + { + return _webClient.DownloadData(address).ToString(); + } + } +} diff -r bd448bb6e0ba -r 29ed98d659e9 Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs --- a/Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs Mon Feb 20 21:53:40 2012 +0700 +++ b/Stocks/Stocks.Service.Tests.Unit/StockServiceTests.cs Mon Feb 20 22:04:50 2012 +0700 @@ -6,6 +6,7 @@ using Stocks.Common; using System.Threading.Tasks; using System.Threading; +using System.Net; namespace Stocks.Service.Tests.Unit { @@ -15,13 +16,20 @@ public void Service_IsActive_property() { var file = "../../../Stocks.Service/companyData.json"; - var service = new StocksService(new ConfigurationService(file)); + var configurationService = new ConfigurationService(file); + var webClientShim = new WebClientShim(new WebClient()); + + var service = new StocksService( + configurationService, + webClientShim); + + Assert.Equal(false, service.IsActive); using (var task = Task.Factory.StartNew(() => { - Assert.Equal(false, service.IsActive); service.Start(); Assert.Equal(true, service.IsActive); + using (var task2 = Task.Factory.StartNew(() => Thread.Sleep(500))) { task2.Wait(); } service.Stop(); diff -r bd448bb6e0ba -r 29ed98d659e9 Stocks/Stocks.Service/StocksService.cs --- a/Stocks/Stocks.Service/StocksService.cs Mon Feb 20 21:53:40 2012 +0700 +++ b/Stocks/Stocks.Service/StocksService.cs Mon Feb 20 22:04:50 2012 +0700 @@ -1,14 +1,12 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Net; using System.Threading.Tasks; using NLog; using Stocks.Common; +using Stocks.Common.Events; using Stocks.Common.Models; -using Stocks.Common.Events; namespace Stocks.Service { @@ -20,24 +18,31 @@ private IConfigurationService _configurationService; private List _currentPrices; private string _serviceUrl; - private WebClient _webClient; + private IWebClientShim _webClient; public bool IsActive { get; private set; } public SummaryStats Stats { get; private set; } - public StocksService(IConfigurationService configurationService) + public StocksService( + IConfigurationService configurationService, + IWebClientShim webClientShim) { new AssemblyInit(); - _webClient = new WebClient(); + _webClient = webClientShim; _configurationService = configurationService; - _companies = configurationService.GetCompanies(); + Stats = new SummaryStats(); + + GetCompanyList(); + } + + private void GetCompanyList() + { + _companies = _configurationService.GetCompanies(); string symbolsCsv = _companies.Select( c => c.Symbol).Aggregate((c, d) => c + "," + d); _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv); - - Stats = new SummaryStats(); } public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e); @@ -110,17 +115,20 @@ continue; } - if (localPrice.Equals(webPrice)) { + if (localPrice.Equals(webPrice)) continue; - } - else { - localPrice.PreviousPrice = localPrice.CurrentPrice; - localPrice.CurrentPrice = webPrice.CurrentPrice; - RaisePriceChanged(localPrice); - } + else + UpdateLocalPrice(webPrice, localPrice); } } + private void UpdateLocalPrice(Price webPrice, Price localPrice) + { + localPrice.PreviousPrice = localPrice.CurrentPrice; + localPrice.CurrentPrice = webPrice.CurrentPrice; + RaisePriceChanged(localPrice); + } + private void UpdateStats(Stopwatch timeToDownload, string webResponse) { Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds;