コード例 #1
0
 public void ListCountries()
 {
     var svc = new CountriesService
     {
         FilePath = @"TestData\ListCountries.xml"
     };
     var countries = svc.ListCountires();
     Assert.IsNotNull(countries);
     Assert.IsTrue(countries.Count == 3);
 }
コード例 #2
0
 public void AddOneCountry()
 {
     var svc = new CountriesService
     {
         FilePath = @"TestData\AddOneCountry.xml"
     };
     svc.AddCountry(new Country
     {
         Capitol = "Warsaw",
         Name = "Poland"
     });
 }
コード例 #3
0
        public void TestSynchronizedAccess()
        {
            const int count = 100;
            var rnd = new Random();
            var countries = new List<Country>(count);
            for (int i = 0; i < count; i++)
            {
                countries.Add(new Country
                {
                    Capitol = rnd.Next().ToString(),
                    Name = rnd.NextDouble().ToString()
                });
            }

            var svc = new CountriesService
            {
                FilePath = @"TestData\TestSynchronizedAccess.xml"
            };
            svc.DeleteFile();

            var tasks = new List<Task>();
            var j = 0;
            foreach(var country in countries)
            {
                tasks.Add(new Task(() =>
                {
                    svc.AddCountry(country);
                }));
                if (++j % 5 == 0)
                    tasks.Add(new Task(() =>
                    {
                        var list = svc.ListCountires();
                        System.Diagnostics.Debug.WriteLine("Countries count: {0}", list.Count);
                    }));
            }

            tasks.ForEach(t => t.Start());
            Task.WaitAll(tasks.ToArray());
        }