Example #1
0
        public void IndependentTransactions()
        {
            const int threadCount = 2;

            int initialCount;

            using (var scope = TestScope.Create())
            {
                ConcurrencyUtility.CheckForParallelism(scope.Resolve <ISqlExecuter>(), threadCount);

                var context = scope.Resolve <Common.ExecutionContext>();
                initialCount = context.Repository.TestEntity.BaseEntity.Query().Count();
            }

            var id1 = Guid.NewGuid();

            Parallel.For(0, threadCount, thread =>
            {
                using (var scope = TestScope.Create())
                {
                    var context = scope.Resolve <Common.ExecutionContext>();

                    Assert.AreEqual(initialCount, context.Repository.TestEntity.BaseEntity.Query().Count());
                    Thread.Sleep(100);
                    context.Repository.TestEntity.BaseEntity.Insert(new TestEntity.BaseEntity {
                        ID = id1, Name = TestNamePrefix + Guid.NewGuid()
                    });                                                                                                                              // Each thread uses the same ID to make sure only one thread can run this code at same time.
                    Assert.AreEqual(initialCount + 1, context.Repository.TestEntity.BaseEntity.Query().Count());
                }
            });
        }
        private void Execute2ParallelInserts(int testCount, Action <int, TestAutoCode._Helper.Simple_Repository> action)
        {
            const int threadCount = 2;

            using (var scope = TestScope.Create())
            {
                ConcurrencyUtility.CheckForParallelism(scope.Resolve <ISqlExecuter>(), threadCount);
                DeleteOldData(scope);
                scope.CommitAndClose();
            }

            for (int test = 1; test <= testCount; test++)
            {
                Console.WriteLine("Test: " + test);
                var containers = Enumerable.Range(0, threadCount).Select(t => TestScope.Create()).ToArray();

                try
                {
                    var repositories = containers.Select(c => c.Resolve <Common.DomRepository>().TestAutoCode.Simple).ToArray();
                    foreach (var r in repositories)
                    {
                        Assert.IsTrue(r.Query().Count() >= 0); // Cold start.
                    }
                    Parallel.For(0, threadCount, process =>
                    {
                        action(process, repositories[process]);
                        containers[process].CommitAndClose();
                        containers[process].Dispose();
                        containers[process] = null;
                    });
                }
                finally
                {
                    foreach (var c in containers)
                    {
                        if (c != null)
                        {
                            c.Dispose();
                        }
                    }
                }
            }
        }
        private Exception[] ExecuteParallel(Action <Common.ExecutionContext>[] actions, Action <Common.ExecutionContext> coldStartInsert, Action <Common.ExecutionContext> coldStartQuery)
        {
            int threadCount = actions.Length;

            using (var scope = TestScope.Create())
            {
                var sqlExecuter = scope.Resolve <ISqlExecuter>();
                ConcurrencyUtility.CheckForParallelism(sqlExecuter, threadCount);
                DeleteOldData(scope);

                coldStartInsert(scope.Resolve <Common.ExecutionContext>());

                scope.CommitAndClose();
            }

            var containers = Enumerable.Range(0, threadCount).Select(t => TestScope.Create()).ToArray();

            var exceptions = new Exception[threadCount];

            try
            {
                var contexts = containers.Select(c => c.Resolve <Common.ExecutionContext>()).ToArray();
                foreach (var context in contexts)
                {
                    coldStartQuery(context);
                }

                Parallel.For(0, threadCount, thread =>
                {
                    try
                    {
                        actions[thread].Invoke(contexts[thread]);
                    }
                    catch (Exception ex)
                    {
                        exceptions[thread] = ex;
                        contexts[thread].PersistenceTransaction.DiscardOnDispose();
                    }
                    finally
                    {
                        containers[thread].CommitAndClose();
                        containers[thread].Dispose();
                        containers[thread] = null;
                    }
                });
            }
            finally
            {
                foreach (var c in containers)
                {
                    if (c != null)
                    {
                        c.Dispose();
                    }
                }
            }

            for (int x = 0; x < threadCount; x++)
            {
                Console.WriteLine("Exception " + x + ": " + exceptions[x] + ".");
            }
            return(exceptions);
        }