public void IndependentTransactions() { const int threadCount = 2; int initialCount; using (var scope = RhetosProcessHelper.CreateScope()) { RhetosProcessHelper.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 = RhetosProcessHelper.CreateScope()) { 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 Exception[] ExecuteParallel(Action <Common.ExecutionContext>[] actions, Action <Common.ExecutionContext> coldStartInsert, Action <Common.ExecutionContext> coldStartQuery) { int threadCount = actions.Count(); using (var container = new RhetosTestContainer(true)) { var sqlExecuter = container.Resolve <ISqlExecuter>(); RhetosProcessHelper.CheckForParallelism(sqlExecuter, threadCount); DeleteOldData(container); coldStartInsert(container.Resolve <Common.ExecutionContext>()); } var containers = Enumerable.Range(0, threadCount).Select(t => new RhetosTestContainer(true)).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.DiscardChanges(); } finally { 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); }
private void Execute2ParallelInserts(int testCount, Action <int, TestAutoCodeCached._Helper.Simple_Repository> action) { const int threadCount = 2; using (var container = new RhetosTestContainer(true)) { DeleteOldData(container); RhetosProcessHelper.CheckForParallelism(container.Resolve <ISqlExecuter>(), threadCount); } for (int test = 1; test <= testCount; test++) { Console.WriteLine("Test: " + test); var containers = new[] { new RhetosTestContainer(true), new RhetosTestContainer(true) }; var repositories = containers.Select(c => c.Resolve <Common.DomRepository>().TestAutoCodeCached.Simple).ToList(); foreach (var r in repositories) { Assert.IsTrue(r.Query().Count() >= 0); // Cold start. } try { Parallel.For(0, threadCount, process => { action(process, repositories[process]); containers[process].Dispose(); containers[process] = null; }); } finally { foreach (var c in containers) { if (c != null) { c.Dispose(); } } } } }