Units tests, multithreading, is this test make a sense?
I want to test if a method is called from several threads, there is no concurrency problem :
I simulate the method is called 3 times simultaneously and insert 2000 customers in my cache.
I want to be sure there is no conccurency problem.

        [Test, Category("Multithreading")]
        public void Should_Insert_2000_Customers_Using_Concurrency()
        {
            var fakeCustomers = InitCustomersFromDb();
            Action insertCustomersFrom_0_to_1000 = new Action(() => 
            {
                Parallel.For(0, 1000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
                 
            });


            Action insertCustomersFrom_1000_to_2000 = new Action(() => 
            {
                Parallel.For(1000, 2000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
            });

            Action insertCustomersFrom_0_to_2000 = new Action(() => 
            {
                Parallel.For(0, 2000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
            });

            List<Task> tasks = new List<Task>();

            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_0_to_1000));
            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_1000_to_2000));
            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_0_to_2000));

            Task.WaitAll(tasks.ToArray());
            var customers = CustomerCache.Instance.GetCustomers();

            Assert.That(customers.Count == 2000);
        }

Regards, 

Florian
Units tests, multithreading, is this test make a sense?
===

I want to test if a method is called from several threads, there is no concurrency problem :
I simulate the method is called 3 times simultaneously and insert 2000 customers in my cache.
I want to be sure there is no conccurency problem.

        [Test, Category("Multithreading")]
        public void Should_Insert_2000_Customers_Using_Concurrency()
        {
            var fakeCustomers = InitCustomersFromDb();
            Action insertCustomersFrom_0_to_1000 = new Action(() => 
            {
                Parallel.For(0, 1000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
                 
            });


            Action insertCustomersFrom_1000_to_2000 = new Action(() => 
            {
                Parallel.For(1000, 2000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
            });

            Action insertCustomersFrom_0_to_2000 = new Action(() => 
            {
                Parallel.For(0, 2000, x =>
                {
                    CustomerCache.Instance.SetCustomer(x.ToString(), fakeCustomers[0]);
                });
            });

            List<Task> tasks = new List<Task>();

            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_0_to_1000));
            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_1000_to_2000));
            tasks.Add(Task.Factory.StartNew(insertCustomersFrom_0_to_2000));

            Task.WaitAll(tasks.ToArray());
            var customers = CustomerCache.Instance.GetCustomers();

            Assert.That(customers.Count == 2000);
        }

Regards, 

Florian