public void TestAcceptance_LockNumberGenerator() { //---------------Set up test pack------------------- //Create an instance of the number for a specific type of number (e.g. Invoice number) BORegistry.BusinessObjectManager = new BusinessObjectManagerSpy(); string numberType = RandomValueGen.GetRandomString(); BOSequenceNumberLocking.LoadNumberGenClassDef(); //DatabaseConnection.CurrentConnection.ExecuteRawSql("Delete From numbergenerator"); INumberGenerator numGen = new NumberGeneratorPessimisticLocking(numberType); numGen.SetSequenceNumber(0); Thread.Sleep(1); // ensure that the new time is higher. just here to check if this resolves a sporadically failing test. //get the next number for invoice number numGen.NextNumber(); //Clear all loaded objects from object manager BORegistry.BusinessObjectManager = new BusinessObjectManagerSpy(); Thread.Sleep(1); // ensure that the new time is higher. just here to check if this resolves a sporadically failing test. //---------------Execute Test ---------------------- //Create a seperate instance of the number generator (simulating a simultaneous user). INumberGenerator numGen2 = new NumberGeneratorPessimisticLocking(numberType); Assert.AreNotSame(numGen, numGen2); //try Get second number try { numGen2.NextNumber(); Assert.Fail("Should not be able to get second number since locked"); } //---------------Test Result ----------------------- //should get locking error catch (BusObjPessimisticConcurrencyControlException ex) { Assert.IsTrue(ex.Message.Contains("You cannot begin edits on the 'BOSequenceNumberLocking', as another user has started edits and therefore locked to this record")); } }
public void TestAcceptance_LockNumberGenerator_ClearsAfter15Minutes() { //---------------Set up test pack------------------- //Create an entry in the number generator table for entry type to seed with seed = 0 and lockduration = 15 minutes. BORegistry.BusinessObjectManager = new BusinessObjectManagerSpy(); TestUtil.WaitForGC(); string numberType = RandomValueGen.GetRandomString(); BOSequenceNumberLocking.LoadNumberGenClassDef(); //DatabaseConnection.CurrentConnection.ExecuteRawSql("Delete From numbergenerator"); var numGen = new NumberGeneratorPessimisticLocking(numberType); numGen.SetSequenceNumber(0); //get the next number for invoice number long num = numGen.NextNumber(); BOSequenceNumberLocking boSequenceNumber1 = numGen.BoSequenceNumber; Assert.AreEqual(1, num, "The first generated number should be 1"); // set the datetime locked to > 15 minutes ago. UpdateDatabaseLockAsExpired(numberType, 20); BORegistry.BusinessObjectManager = new BusinessObjectManagerSpy(); //---------------Execute Test ---------------------- //Create a seperate instance of the number generator. //try Get number var numGen2 = new NumberGeneratorPessimisticLocking(numberType); Assert.AreNotSame(numGen, numGen2); //try Get second number num = numGen2.NextNumber(); BOSequenceNumberLocking boSequenceNumber2 = numGen2.BoSequenceNumber; //---------------Test Result ----------------------- Assert.AreNotSame(numGen, numGen2); Assert.AreNotSame(boSequenceNumber2, boSequenceNumber1); //should not get locking error //assert nextnumber = 1 Assert.AreEqual(1, num, "The second generated number should be 1. Time: " + DateTime.Now.ToLongTimeString()); }