public void EF_LoadActivities_AtStart_DataHandlerLevel() { context = Start(); var sctx = context.Security; var user1Id = TestUser.User1.Id; var rootEntityId = Id("E01"); // create 30 activities sctx.CreateSecurityEntity(rootEntityId, default(int), user1Id); for (int entityId = rootEntityId + 1; entityId < rootEntityId + 31; entityId++) { sctx.CreateSecurityEntity(entityId, rootEntityId, user1Id); } var lastId = Db().ExecuteTestScript <int>("select top 1 Id from [EFMessages] order by Id desc").First(); // test0: initial int dbId0; var expectedCs0 = new CompletionState { LastActivityId = lastId }; var cs0 = DataHandler.LoadCompletionState(sctx.DataProvider, out dbId0); Assert.AreEqual(lastId, dbId0); Assert.AreEqual(expectedCs0.ToString(), cs0.ToString()); // test1: create unprocessed activities: set "wait" on 4 continuous activity (except last) + 2 gaps before Db().ExecuteTestScript(@"declare @last int select top 1 @last = Id from [EFMessages] order by Id desc UPDATE EFMessages set ExecutionState = 'Executing' where Id in (@last-1, @last-2, @last-3, @last-4, @last-6, @last-9) "); int dbId1; var expectedCs1 = new CompletionState { LastActivityId = lastId, Gaps = new[] { lastId - 9, lastId - 6, lastId - 4, lastId - 3, lastId - 2, lastId - 1 } }; var cs1 = DataHandler.LoadCompletionState(sctx.DataProvider, out dbId1); Assert.AreEqual(dbId1, lastId); Assert.AreEqual(expectedCs1.ToString(), cs1.ToString()); // test2: create unprocessed activities: set "wait" on last 5 continuous activity (except last) + 2 gaps before Db().ExecuteTestScript(@"declare @last int select top 1 @last = Id from [EFMessages] order by Id desc UPDATE EFMessages set ExecutionState = 'Executing' where Id in (@last) "); int dbId2; var expectedCs2 = new CompletionState { LastActivityId = lastId - 5, Gaps = new[] { lastId - 9, lastId - 6 } }; var cs2 = DataHandler.LoadCompletionState(sctx.DataProvider, out dbId2); Assert.AreEqual(dbId2, lastId); Assert.AreEqual(expectedCs2.ToString(), cs2.ToString()); }
public void EF_LoadActivities_AtStart_ActivityQueueLevel() { int lastActivityIdFromDb; CompletionState uncompleted; context = Start(); var sctx = context.Security; var user1Id = TestUser.User1.Id; var rootEntityId = Id("E01"); // create 30 activities sctx.CreateSecurityEntity(rootEntityId, default(int), user1Id); for (int entityId = rootEntityId + 1; entityId < rootEntityId + 31; entityId++) { sctx.CreateSecurityEntity(entityId, rootEntityId, user1Id); } var lastId = Db().ExecuteTestScript <int>("select top 1 Id from [EFMessages] order by Id desc").First(); // test0: initial state var expectedCs = new CompletionState { LastActivityId = lastId }; uncompleted = DataHandler.LoadCompletionState(SecurityContext.General.DataProvider, out lastActivityIdFromDb); SecurityActivityQueue.Startup(uncompleted, lastActivityIdFromDb); var cs0 = SecurityActivityQueue.GetCurrentState().Termination; Assert.AreEqual(expectedCs.ToString(), cs0.ToString()); // test1: create some unprocessed activities: 4 continuous activity (except last) + 2 gaps before // last-2 and last-6 "Wait", the others "Executing" by another appdomain. Db().ExecuteTestScript(@"declare @last int select top 1 @last = Id from [EFMessages] order by Id desc UPDATE EFMessages set ExecutionState = 'Executing', LockedBy = 'AnotherComputer' where Id in (@last-1, @last-3, @last-4, @last-9) UPDATE EFMessages set ExecutionState = 'Wait', LockedBy = null, LockedAt = null where Id in (@last-2, @last-6) "); var expectedIsFromDb1 = String.Join(", ", new[] { lastId - 9, lastId - 4, lastId - 3, lastId - 1, lastId }); uncompleted = DataHandler.LoadCompletionState(SecurityContext.General.DataProvider, out lastActivityIdFromDb); SecurityActivityQueue.Startup(uncompleted, lastActivityIdFromDb); var cs1 = SecurityActivityQueue.GetCurrentState().Termination; var idsFromDb1 = String.Join(", ", Db().GetUnprocessedActivityIds()); Assert.AreEqual(expectedCs.ToString(), cs1.ToString()); Assert.AreEqual(expectedIsFromDb1, idsFromDb1); // test2: create unprocessed activities: last 5 continuous activity + 2 gaps before // last-2 and last-6 "Wait", the others "Executing" by another appdomain. Db().ExecuteTestScript(@"declare @last int select top 1 @last = Id from [EFMessages] order by Id desc UPDATE EFMessages set ExecutionState = 'Executing', LockedBy = 'AnotherComputer' where Id in (@last, @last-1, @last-3, @last-4, @last-9) UPDATE EFMessages set ExecutionState = 'Wait', LockedBy = null, LockedAt = null where Id in (@last-2, @last-6) "); var expectedIsFromDb2 = String.Join(", ", new[] { lastId - 9, lastId - 4, lastId - 3, lastId - 1, lastId, lastId }); uncompleted = DataHandler.LoadCompletionState(SecurityContext.General.DataProvider, out lastActivityIdFromDb); SecurityActivityQueue.Startup(uncompleted, lastActivityIdFromDb); var cs2 = SecurityActivityQueue.GetCurrentState().Termination; var idsFromDb2 = String.Join(", ", Db().GetUnprocessedActivityIds()); Assert.AreEqual(expectedCs.ToString(), cs2.ToString()); Assert.AreEqual(expectedIsFromDb2, idsFromDb2); }