Ejemplo n.º 1
0
        public void TestMultipleLocks()
        {
            NameLockFactoryForTests nlf = new NameLockFactoryForTests();

            Random    rnd = new Random(1402);
            const int NUMBER_OF_ACCOUNTS = 20;
            const int NUMBER_OF_TASKS    = 50;

            Account[] allAccounts = new Account[NUMBER_OF_ACCOUNTS];

            for (int i = 0; i < NUMBER_OF_ACCOUNTS; ++i)
            {
                allAccounts[i] = new Account(rnd.Next(2000), nlf, "Key " + i);
            }

            Task[] tasks = new Task[NUMBER_OF_TASKS];
            for (int i = 0; i < NUMBER_OF_TASKS; ++i)
            {
                Random rnd2 = new Random(i);
                tasks[i] = new Task(delegate()
                {
                    for (int j = 0; j < 10; ++j)
                    {
                        allAccounts[rnd2.Next(NUMBER_OF_ACCOUNTS)].DoTransactions();
                    }
                }
                                    );
                tasks[i].Start();
            }

            Task.WaitAll(tasks);

            Assert.IsTrue(nlf.dictLocksShadow.IsEmpty);
        }
Ejemplo n.º 2
0
        public void TestCaseSensitiveLocks()
        {
            NameLockFactoryForTests nlf = new NameLockFactoryForTests(true);

            Random    rnd             = new Random(1402);
            const int NUMBER_OF_TASKS = 50;

            Account[] allAccounts = new Account[] {
                new Account(rnd.Next(2000), nlf, "account"),
                new Account(rnd.Next(2000), nlf, "ACCOUNT"),
                new Account(rnd.Next(2000), nlf, "Account")
            };

            Task[] tasks = new Task[NUMBER_OF_TASKS];
            for (int i = 0; i < NUMBER_OF_TASKS; ++i)
            {
                Random rnd2 = new Random(i);
                tasks[i] = new Task(delegate()
                {
                    for (int j = 0; j < 10; ++j)
                    {
                        allAccounts[rnd2.Next(allAccounts.Length)].DoTransactions();
                    }
                }
                                    );
                tasks[i].Start();
            }

            Task.WaitAll(tasks);

            Assert.IsTrue(nlf.dictLocksShadow.IsEmpty);
        }
Ejemplo n.º 3
0
        public void TestWithSingleLock()
        {
            NameLockFactoryForTests nlf = new NameLockFactoryForTests();

            Thread[] threads = new Thread[10];
            Account  acc     = new Account(1000, nlf);

            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(acc.DoTransactions));
                threads[i] = t;
            }
            for (int i = 0; i < 10; i++)
            {
                threads[i].Start();
            }

            // wait for all threads to terminate
            for (int i = 0; i < 10; i++)
            {
                threads[i].Join();
            }

            // if something goes wrong with locking, Account throws a "Negative Balance" exception
            Assert.IsTrue(nlf.dictLocksShadow.IsEmpty);
        }