public void AddingObjects_GetObjectsMustReturnCorrectNumberOfObjects() { var manager = new InMemoryObjectManager(); // We should have zero object in the manager Assert.Empty(manager.GetObjects()); // FIRST TRANSACTION var firstTransaction = manager.CreateTransaction(); firstTransaction.Add(new TestObjectA(Guid.NewGuid(), "first object")); Assert.Empty(manager.GetObjects()); // We should still have zero object in the manager, because transaction is not commited yet firstTransaction.Commit(); Assert.Single(manager.GetObjects()); // We should have one object in the manager after transaction commit // SECOND TRANSACTION var secondTransaction = manager.CreateTransaction(); secondTransaction.Add(new TestObjectA(Guid.NewGuid(), "second object")); Assert.Single(manager.GetObjects()); // We should have one object in the manager before transaction commit secondTransaction.Commit(); Assert.Equal(2, manager.GetObjects().Count()); // We should have two object in the manager before transaction commit // THIRD TRANSACTION var thirdTransaction = manager.CreateTransaction(); thirdTransaction.Add(new TestObjectA(Guid.NewGuid(), "third object")); thirdTransaction.Add(new TestObjectA(Guid.NewGuid(), "forth object")); Assert.Equal(2, manager.GetObjects().Count()); // We should have two object in the manager before transaction commit thirdTransaction.Commit(); Assert.Equal(4, manager.GetObjects().Count()); // We should have four object in the manager before transaction commit // Assert that GetObjects by internal version id works Assert.Single(manager.GetObjects(1)); // In version 1 we should have 1 object Assert.Equal(2, manager.GetObjects(2).Count()); // In version 2 we should have 2 object Assert.Equal(4, manager.GetObjects(3).Count()); // In version 3 we should have 4 object }
public void AddDeleteTest_GetObjectsMustReturnCorrectResult() { var manager = new InMemoryObjectManager(); // FIRST TRANSACTION: Add two object var firstTransaction = manager.CreateTransaction(); var firstObj = new TestObjectA(Guid.NewGuid(), "first object"); firstTransaction.Add(firstObj); var secondObj = new TestObjectA(Guid.NewGuid(), "second object"); firstTransaction.Add(secondObj); firstTransaction.Commit(); // SECOND TRANSACTION: Delete first object var secondTransaction = manager.CreateTransaction(); secondTransaction.Delete(firstObj.Id); secondTransaction.Commit(); // THIRD TRANSACTION: Delete second object var thirdTransaction = manager.CreateTransaction(); thirdTransaction.Delete(secondObj.Id); thirdTransaction.Commit(); // FORTH TRANSACTION: Add second object again var forthTransaction = manager.CreateTransaction(); forthTransaction.Add(new TestObjectA(secondObj.Id, "second object version 2")); forthTransaction.Commit(); // In version 1 we should have 2 object Assert.Equal(2, manager.GetObjects(1).Count()); // In version 1 name of first object should be "first object" Assert.Contains(manager.GetObjects(1), o => o.Id == firstObj.Id && ((TestObjectA)o).Name == "first object"); // In version 1 name of second object should be "second object" Assert.Contains(manager.GetObjects(1), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); // In version 2 we should have only 1 object Assert.Single(manager.GetObjects(2)); // In version 2 we should have the second object named "second object" Assert.Contains(manager.GetObjects(2), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); // In version 3 we should have no objects Assert.Empty(manager.GetObjects(3)); // In version 4 we should have 1 object Assert.Single(manager.GetObjects(4)); // In version 4 we should have the second object named "second object version 2" Assert.Contains(manager.GetObjects(4), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object version 2"); }
public void WorkAfterRollback_ShouldNotBeCommited() { var manager = new InMemoryObjectManager(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // FIRST TRANSACTION var firstTransaction = manager.CreateTransaction(); var firstObj = firstTransaction.Add(new TestObjectA(Guid.NewGuid(), "first object")); var secondObj = firstTransaction.Add(new TestObjectA(Guid.NewGuid(), "second object")); var thirdObj = firstTransaction.Add(new TestObjectA(Guid.NewGuid(), "third object")); firstTransaction.Commit(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // SECOND TRANSACTION var secondTransaction = manager.CreateTransaction(); // Insert forth object var forthObj = secondTransaction.Add(new TestObjectA(Guid.NewGuid(), "forth object")); // Delete second object secondTransaction.Delete(secondObj.Id); // Update third object var thirdObjUpdated = new TestObjectA(thirdObj.Id, "third object updated"); secondTransaction.Update(thirdObjUpdated); secondTransaction.Rollback(); // In version 1 we should have 3 object Assert.Equal(3, manager.GetObjects(1).Count()); // In version 1 name of first object should be "first object" Assert.Contains(manager.GetObjects(1), o => o.Id == firstObj.Id && ((TestObjectA)o).Name == "first object"); // In version 1 name of second object should be "second object" Assert.Contains(manager.GetObjects(1), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); // Version 2 should not exist, because we did a rollback of the second transaction Assert.Throws <ArgumentException>(() => manager.GetObjects(2)); }
public void DeleteObject_GetObjectsMustReturnCorrectResult() { var manager = new InMemoryObjectManager(); // FIRST TRANSACTION: Add two object var firstTransaction = manager.CreateTransaction(); var firstObj = new TestObjectA(Guid.NewGuid(), "first object"); firstTransaction.Add(firstObj); var secondObj = new TestObjectA(Guid.NewGuid(), "second object"); firstTransaction.Add(secondObj); firstTransaction.Commit(); // SECOND TRANSACTION: Delete first object var secondTransaction = manager.CreateTransaction(); secondTransaction.Delete(firstObj.Id); secondTransaction.Commit(); // Deletion version should not be null Assert.NotNull(firstObj.DeletionVersion); // In version 1 we should have 2 object Assert.Equal(2, manager.GetObjects(1).Count()); // In version 1 name of first object should be "first object" Assert.Contains(manager.GetObjects(1), o => o.Id == firstObj.Id && ((TestObjectA)o).Name == "first object"); // In version 1 name of second object should be "second object" Assert.Contains(manager.GetObjects(1), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); // In version 2 we should have only 1 object Assert.Single(manager.GetObjects(2)); // In version 2 we should have the second object named "second object" Assert.Contains(manager.GetObjects(2), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); }
public void CreateTransactionWithCustomId_TestGetObject() { var manager = new InMemoryObjectManager(); var myCustomId = "my custom version id 1"; var firstTransaction = manager.CreateTransaction(myCustomId); var objToAdd = new TestObjectA(Guid.NewGuid(), "hej"); firstTransaction.Add(objToAdd); firstTransaction.Commit(); // Asser that GetObjects work with custom ids var objects = manager.GetObjects(myCustomId); Assert.Single(objects); var obj = manager.GetObject(objToAdd.Id, myCustomId); Assert.Equal(objToAdd, obj); }
public void UpdateObject_GetObjectsMustReturnCorrectResult() { var manager = new InMemoryObjectManager(); // FIRST TRANSACTION: Add two object var firstTransaction = manager.CreateTransaction(); var firstObj = new TestObjectA(Guid.NewGuid(), "first object"); firstTransaction.Add(firstObj); var secondObj = new TestObjectA(Guid.NewGuid(), "second object"); firstTransaction.Add(secondObj); firstTransaction.Commit(); // SECOND TRANSACTION: Update name of second object var secondTransaction = manager.CreateTransaction(); var updatedObj = new TestObjectA(secondObj.Id, "second object updated"); secondTransaction.Update(updatedObj); secondTransaction.Commit(); // In version 1 we should have 2 object Assert.Equal(2, manager.GetObjects(1).Count()); // In version 1 name of first object should be "first object" Assert.Contains(manager.GetObjects(1), o => o.Id == firstObj.Id && ((TestObjectA)o).Name == "first object"); // In version 1 name of second object should be "second object" Assert.Contains(manager.GetObjects(1), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object"); // In version 2 we should still have 2 object Assert.Equal(2, manager.GetObjects(2).Count()); // In version 2 name of first object should still be "first object" Assert.Contains(manager.GetObjects(2), o => o.Id == firstObj.Id && ((TestObjectA)o).Name == "first object"); // In version 2 name of second object should be changed to "second object updated" Assert.Contains(manager.GetObjects(2), o => o.Id == secondObj.Id && ((TestObjectA)o).Name == "second object updated"); }
public void TestConcurrentReaderOneWriter() { var manager = new InMemoryObjectManager(); // Add initial object var transaction = manager.CreateTransaction(); var objToAdd = new TestObjectA(Guid.NewGuid(), "initial"); transaction.Add(objToAdd); transaction.Commit(); // Update name of object to inital 2 transaction = manager.CreateTransaction(); var objToUpdate = new TestObjectA(objToAdd.Id, "initial 2"); transaction.Update(objToUpdate); transaction.Commit(); // Thread that create a new transaction every 10 milisecond new Thread(() => { for (int i = 0; i < 100; i++) { var transaction = manager.CreateTransaction(); var objToUpdate = new TestObjectA(objToAdd.Id, "trans version " + transaction.Version.InternalVersionId); transaction.Update(objToUpdate); //Give some time to other threads to kick in Thread.Sleep(10); transaction.Commit(); Assert.Single(manager.GetObjects(transaction.Version.InternalVersionId)); } }).Start(); // Reading thread #1 new Thread(() => { for (int i = 0; i < 100; i++) { Assert.Single(manager.GetObjects(1)); Assert.Equal("initial", ((TestObjectA)manager.GetObjects(1).First()).Name); Thread.Sleep(10); } }).Start(); // Reading thread #2 new Thread(() => { for (int i = 0; i < 100; i++) { Assert.Single(manager.GetObjects(1)); Assert.Equal("initial", ((TestObjectA)manager.GetObjects(1).First()).Name); Thread.Sleep(10); } }).Start(); // Reading thread #3 new Thread(() => { for (int i = 0; i < 100; i++) { Assert.Single(manager.GetObjects(2)); Assert.Equal("initial 2", ((TestObjectA)manager.GetObjects(2).First()).Name); Thread.Sleep(10); } }).Start(); Thread.Sleep(1500); }