public void T_001() { Aver.AreEqual(0, m_Server.CurrentServerCallsNorm); Aver.IsTrue(0.99d < m_Server.CurrentTrustLevel); var tran1 = new LockTransaction("Dummy call to cause traffic", "NamespaceName", 0, 0.75d, LockOp.Assert(LockOp.Not(LockOp.Exists("AAAAA", "BBBBB"))) ); var user1 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 1", 1000); //make many calls at once LockTransactionResult result; for (var i = 0; i < 16; i++) { result = m_Server.ExecuteLockTransaction(user1, tran1); Aver.IsTrue(LockStatus.TransactionOK == result.Status); } //then stop //Trust level normalizes with time while (m_Server.CurrentTrustLevel > 0.75d) { Console.WriteLine(" Waiting for trust to fall ... Norm: {0}; Trust: {1}".Args(m_Server.CurrentServerCallsNorm, m_Server.CurrentTrustLevel)); Thread.Sleep(500); } //trust level is low, failure result = m_Server.ExecuteLockTransaction(user1, tran1); Aver.IsTrue(LockStatus.TransactionError == result.Status); Aver.IsTrue(LockErrorCause.MinimumRequirements == result.ErrorCause); //Trust level normalizes with time while (m_Server.CurrentTrustLevel < 0.75d) { Console.WriteLine(" ... Norm: {0}; Trust: {1}".Args(m_Server.CurrentServerCallsNorm, m_Server.CurrentTrustLevel)); Thread.Sleep(500); } //everything is OK again result = m_Server.ExecuteLockTransaction(user1, tran1); Aver.IsTrue(LockStatus.TransactionOK == result.Status); //for(var i=0; i<120; i++) //{ // if (i<30 || (i%3)==0) // { // var result = m_Server.ExecuteLockTransaction(user1, tran1); // Aver.AreEqual(LockStatus.TransactionOK, result.Status); // } // if (i==30) // Console.WriteLine("All CALLs stopped----------------"); // Thread.Sleep(500); // Console.WriteLine("Made {0} calls; Norm: {1}; Trust: {2}".Args(i, m_Server.CurrentServerCallsNorm, m_Server.CurrentTrustLevel)); //} }
public void SessionExpiration() { var insert = new LockTransaction("Expiration", "NamespaceName", 0, 0.0d, LockOp.Assert(LockOp.SetVar("T1", "a", 100)) ); var exists = new LockTransaction("Expiration", "NamespaceName", 0, 0.0d, LockOp.Assert(LockOp.Exists("T1", "a")) ); var dummy = new LockTransaction("Expiration", "NamespaceName", 0, 0.0d, LockOp.Assert(LockOp.True) ); var user1 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 1", 8); //8 seconds for session max life var user2 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 2", 10000); var result = m_Server.ExecuteLockTransaction(user1, insert); Aver.IsTrue(LockStatus.TransactionOK == result.Status); result = m_Server.ExecuteLockTransaction(user2, exists); Aver.IsTrue(LockStatus.TransactionOK == result.Status);//User 2 sees it for (var i = 0; i < 20; i++) { Thread.Sleep(1000); //Lets wait result = m_Server.ExecuteLockTransaction(user1, dummy); Aver.IsTrue(LockStatus.TransactionOK == result.Status); //User 1 keeps doing some stuff, so session does not epire Console.WriteLine("{0} sec. user 1 keeps sending to session 1.".Args(i)); } result = m_Server.ExecuteLockTransaction(user2, exists); Aver.IsTrue(LockStatus.TransactionOK == result.Status);//User 2 still sees it because user 1 kept sending requests Console.WriteLine(" User 2 still sees session 1 as user 1 kept sending requests"); for (var i = 0; i < 20; i++) { Thread.Sleep(1000);//Lets wait result = m_Server.ExecuteLockTransaction(user2, exists); if (LockStatus.TransactionError == result.Status && LockErrorCause.Statement == result.ErrorCause) { return; //User 2 does not see it as user 1 session expired } Console.WriteLine("{0} sec. ... has not expired yet...".Args(i)); } Aver.Fail("Did not expire even after significant delay"); }
public void MDSEnterReview_ARMonthEnd(int threadCount, int nsCount, int facCount, int durationSec) { var namespaces = new string[nsCount]; for (var i = 0; i < nsCount; i++) { namespaces[i] = "NS_{0}_{1}_{2}_{3}__{4}".Args(threadCount, nsCount, facCount, durationSec, i); } var facilities = new Facility[facCount]; for (var i = 0; i < facCount; i++) { facilities[i] = new Facility { ID = i * 456, Patients = Enumerable.Range(1, 100).Select(n => n).ToArray() } } ; var errors = new ConcurrentBag <Exception>(); var threads = new List <Thread>(); var totalLockAttempts = 0; var totalLockTaken = 0; var totalLocksReleaseAttempts = 0; var totalLocksReleased = 0; var totalMDSEnter = 0; var totalMDSReview = 0; var totalARMonthEnd = 0; var totalMDSEnterTaken = 0; var totalMDSReviewTaken = 0; var totalARMonthEndTaken = 0; for (var i = 0; i < threadCount; i++) { threads.Add(new Thread(() => { var user = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "user X", 10000); var sd = DateTime.Now; while ((DateTime.Now - sd).TotalSeconds < durationSec) { try { var ns = namespaces[ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, namespaces.Length - 1)]; var facility = facilities[ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, facilities.Length - 1)]; var patient = facility.Patients[ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, facility.Patients.Length - 1)]; var chance = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 10); LockTransaction businessOperationLockStart; LockTransaction businessOperationLockEnd; BusinessLock lockType; if (chance < 3) //MDS ENTER { lockType = BusinessLock.MDSEnter; businessOperationLockStart = new LockTransaction("MDS Entry for {0}@'{1}'".Args(patient, facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.Not(LockOp.Exists("Month-End", facility.ID.ToString()))), LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Review", facility.ID.ToString(), patient.ToString()))), LockOp.Assert(LockOp.SetVar("MDS-Entry", facility.ID.ToString(), patient.ToString(), allowDuplicates: true)) ); businessOperationLockEnd = new LockTransaction("MDS Entry for {0}@'{1}' is done".Args(patient, facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.DeleteVar("MDS-Entry", facility.ID.ToString(), patient.ToString())) ); } else if (chance < 6) //MDS REVIEW { lockType = BusinessLock.MDSReview; businessOperationLockStart = new LockTransaction("MDS Review for {0}@'{1}'".Args(patient, facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.Not(LockOp.Exists("Month-End", facility.ID.ToString()))), LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Entry", facility.ID.ToString(), patient.ToString()))), LockOp.Assert(LockOp.SetVar("MDS-Review", facility.ID.ToString(), patient.ToString(), allowDuplicates: false)) ); businessOperationLockEnd = new LockTransaction("MDS Review for {0}@'{1}' is done".Args(patient, facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.DeleteVar("MDS-Review", facility.ID.ToString(), patient.ToString())) ); } else // MONTH END { lockType = BusinessLock.ARMonthEnd; //note, this is FACILITY-level lock (unlike MDSs that are patient-level) businessOperationLockStart = new LockTransaction("Month End Close@'{0}'".Args(facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Entry", facility.ID.ToString()))), LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Review", facility.ID.ToString()))), LockOp.Assert(LockOp.SetVar("Month-End", facility.ID.ToString(), null)) ); businessOperationLockEnd = new LockTransaction("Month End Close@'{0}' is done".Args(facility.ID), ns, 0, 0.0d, LockOp.Assert(LockOp.DeleteVar("Month-End", facility.ID.ToString())) ); } Interlocked.Increment(ref totalLockAttempts); if (lockType == BusinessLock.MDSEnter) { Interlocked.Increment(ref totalMDSEnter); } else if (lockType == BusinessLock.MDSReview) { Interlocked.Increment(ref totalMDSReview); } else { Interlocked.Increment(ref totalARMonthEnd); } var tranResult = m_Server.ExecuteLockTransaction(user, businessOperationLockStart); if (tranResult.Status == LockStatus.TransactionOK) { Interlocked.Increment(ref totalLockTaken); if (lockType == BusinessLock.MDSEnter) { Interlocked.Increment(ref totalMDSEnterTaken); } else if (lockType == BusinessLock.MDSReview) { Interlocked.Increment(ref totalMDSReviewTaken); } else { Interlocked.Increment(ref totalARMonthEndTaken); } Thread.Sleep(10 + ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 150));//immitate some business work Interlocked.Increment(ref totalLocksReleaseAttempts); tranResult = m_Server.ExecuteLockTransaction(user, businessOperationLockEnd); if (tranResult.Status == LockStatus.TransactionOK) { Interlocked.Increment(ref totalLocksReleased); } } else { //locking conflict Aver.IsTrue(LockErrorCause.Statement == tranResult.ErrorCause); } } catch (Exception error) { errors.Add(error); Console.WriteLine("Total trans: {0}; ERROR: {1}".Args(totalLockAttempts, error.ToMessageWithType())); } } })); } foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } Console.WriteLine( @" Lock attempts: {0} taken: {1} ------------------------------ Release attempts: {2} released: {3}".Args(totalLockAttempts, totalLockTaken, totalLocksReleaseAttempts, totalLocksReleased)); Console.WriteLine( @" MDS Enter: {0} out of {1} MDS Reviews: {2} out of {3} AR Month End: {4} out of {5} ".Args(totalMDSEnterTaken, totalMDSEnter, totalMDSReviewTaken, totalMDSReview, totalARMonthEndTaken, totalARMonthEnd)); Aver.AreEqual(0, errors.Count); } }
public void MDS_Entry_Review() { var mdsEnter = new LockTransaction("MDS Entry for Zak Pak@'Cherry Grove'", "Clinical", 0, 0.5d, LockOp.Assert(LockOp.Not(LockOp.Exists("Month-End", "CGROVE-35"))), LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Review", "CGROVE-35", "R-45899"))), LockOp.Assert(LockOp.SetVar("MDS-Entry", "CGROVE-35", "R-45899", allowDuplicates: true)) ); var mdsReview = new LockTransaction("MDS Review for Zak Pak@'Cherry Grove'", "Clinical", 0, 0.5d, LockOp.Assert(LockOp.Not(LockOp.Exists("Month-End", "CGROVE-35"))), LockOp.Assert(LockOp.Not(LockOp.Exists("MDS-Entry", "CGROVE-35", "R-45899"))), LockOp.Assert(LockOp.SetVar("MDS-Review", "CGROVE-35", "R-45899", allowDuplicates: false)) ); var mdsReviewUnlock = new LockTransaction("MDS Review for Zak Pak@'Cherry Grove' is done", "Clinical", 0, 0.5d, LockOp.AnywayContinueAfter(LockOp.DeleteVar("MDS-Review", "CGROVE-35", "R-45899")) ); var user1 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 1", 1000); var user2 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 2", 1000); var user3 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 3", 1000); var user4 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 4", 1000); var result = m_Server.ExecuteLockTransaction(user1, mdsEnter); Aver.IsTrue(LockStatus.TransactionOK == result.Status); result = m_Server.ExecuteLockTransaction(user2, mdsEnter); Aver.IsTrue(LockStatus.TransactionOK == result.Status); result = m_Server.ExecuteLockTransaction(user3, mdsReview); Aver.IsTrue(LockStatus.TransactionError == result.Status);//Cant start review because someone else is entering Aver.AreEqual("1:/AssertOp/", result.FailedStatement); //User 1 ends session, this rolls back everything Aver.IsTrue(m_Server.EndLockSession(user1.ID)); //still user 2 is present result = m_Server.ExecuteLockTransaction(user3, mdsReview); Aver.IsTrue(LockStatus.TransactionError == result.Status);//Cant start review because someone else is entering Aver.AreEqual("1:/AssertOp/", result.FailedStatement); //User 2 ends session, this rolls back everything Aver.IsTrue(m_Server.EndLockSession(user2.ID)); //No more entering people REVIEW succeeds result = m_Server.ExecuteLockTransaction(user3, mdsReview); Aver.IsTrue(LockStatus.TransactionOK == result.Status);//Now the review has started!!!! //but enter fails because review conflicts with it result = m_Server.ExecuteLockTransaction(user4, mdsEnter); Aver.IsTrue(LockStatus.TransactionError == result.Status);//Cant start ENTER because someone else is reviewing Aver.AreEqual("1:/AssertOp/", result.FailedStatement); //Review is done result = m_Server.ExecuteLockTransaction(user3, mdsReviewUnlock); Aver.IsTrue(LockStatus.TransactionOK == result.Status);//Now the review has ended!!!! //so enter now succeeds just fine result = m_Server.ExecuteLockTransaction(user4, mdsEnter); Aver.IsTrue(LockStatus.TransactionOK == result.Status);//Can ENTER because review is over }
public void PutManyVarsSameNamespace(bool parallel, int varCount, int tblCount) { var ns = "NS_{0}_{1}_{2}".Args(parallel, varCount, tblCount); var user1 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 1", 10000); var user2 = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "User 2", 10000); var sw = Stopwatch.StartNew(); Action <int> put = (i) => { var statements = new LockOp.StatementOp[tblCount]; for (var t = 0; t < tblCount; t++) { statements[t] = LockOp.Assert(LockOp.SetVar("t" + t.ToString(), "v" + i.ToString(), i, "Variable number " + i.ToString())); } var insert = new LockTransaction("Set var", ns, 0, 0.0d, statements); var result = m_Server.ExecuteLockTransaction(user1, insert); Aver.IsTrue(LockStatus.TransactionOK == result.Status); }; if (parallel) { Parallel.For(0, varCount, put); } else { for (var i = 0; i < varCount; i++) { put(i); } } Console.WriteLine("Put {0} in {1} ms at {2} ops/sec".Args(varCount, sw.ElapsedMilliseconds, varCount / (sw.ElapsedTicks / (double)Stopwatch.Frequency))); sw.Restart(); Action <int> get = (i) => { var statements = new LockOp.StatementOp[tblCount]; for (var t = 0; t < tblCount; t++) { statements[t] = LockOp.Assert(LockOp.Exists("t" + t.ToString(), "v" + i.ToString(), i)); } var read = new LockTransaction("Get var", ns, 0, 0.0d, statements); var result = m_Server.ExecuteLockTransaction(user2, read); Aver.IsTrue(LockStatus.TransactionOK == result.Status); }; if (parallel) { Parallel.For(0, varCount, get); } else { for (var i = 0; i < varCount; i++) { get(i); } } Console.WriteLine("Read/verified {0} in {1} ms at {2} ops/sec".Args(varCount, sw.ElapsedMilliseconds, varCount / (sw.ElapsedTicks / (double)Stopwatch.Frequency))); }
public void T1() { var tran = new LockTransaction("Testing 101", "A", 0, 0.0d, LockOp.Assert(LockOp.True) ); var session = new Agni.Locking.Server.LockSessionData(new LockSessionID(null), "My session", 1000); var result = m_Server.ExecuteLockTransaction(session, tran); Aver.IsTrue(LockStatus.TransactionOK == result.Status); }