public void TestObjectUniqing() { GentleSettings.SkipQueryExecution = false; ObjectMap map = ObjectFactory.GetMap(Broker.SessionBroker, typeof(MailingList)); if (map.CacheStrategy != CacheStrategy.Never) { // test without cache CacheManager.Clear(); GentleSettings.CacheObjects = false; int cacheCount = CacheManager.Count; l1 = MailingList.Retrieve(1); Assert.IsTrue(CacheManager.Count == cacheCount, "Item was erroneously added to cache."); l2 = MailingList.Retrieve(1); Assert.AreNotSame(l1, l2, "Object references are supposed to be different."); Check.LogInfo(LogCategories.General, "TestObjectUniqing --- after execution (without cache):"); GentleStatistics.LogStatistics(LogCategories.Cache); // test with cache GentleSettings.CacheObjects = true; l1 = MailingList.Retrieve(1); Assert.IsTrue(CacheManager.Count == ++cacheCount, "Item was not added to cache."); l2 = MailingList.Retrieve(1); Assert.IsTrue(CacheManager.Count == cacheCount, "Item was added to cache again."); Assert.AreSame(l1, l2, "Object references are supposed to be identical."); Check.LogInfo(LogCategories.General, "TestObjectUniqing --- after execution (with cache)"); GentleStatistics.LogStatistics(LogCategories.Cache); } }
public void TestUniqingScope() { GentleSettings.CacheObjects = true; GentleSettings.SkipQueryExecution = false; GentleSettings.UniqingScope = UniqingScope.Thread; CacheManager.Clear(); ObjectMap map = ObjectFactory.GetMap(Broker.SessionBroker, typeof(MailingList)); if (map.CacheStrategy != CacheStrategy.Never) { // access in this thread (populates cache) int cacheCount1stThread = CacheManager.Count; l1 = MailingList.Retrieve(1); Assert.IsTrue(CacheManager.Count == ++cacheCount1stThread, "Item was not added to cache."); l2 = MailingList.Retrieve(1); Assert.IsTrue(CacheManager.Count == cacheCount1stThread, "Item was added to cache again."); Assert.AreSame(l1, l2, "Object references are supposed to be identical."); Check.LogInfo(LogCategories.General, "TestUniqingScope --- after execution (thread {0}):", SystemSettings.ThreadIdentity); GentleStatistics.LogStatistics(LogCategories.Cache); // access same type in separate thread Thread thread = new Thread(RetrieveSpecificList); // remember the threads id (check for name to match SystemSettings.ThreadIdentity behavior) string threadId = thread.Name != null ? thread.Name : thread.GetHashCode().ToString(); int cacheCount2ndThread = CacheManager.GetCount(threadId); thread.Start(); thread.Join(); // wait for completion // we should see only a mailinglist being added to the cache Assert.AreEqual(cacheCount1stThread, CacheManager.Count, "Item was added to wrong cache store."); Assert.AreEqual(++cacheCount2ndThread, CacheManager.GetCount(threadId), "Item was not added to cache for 2nd thread."); Check.LogInfo(LogCategories.General, "TestUniqingScope --- after execution (thread {0}):", thread.GetHashCode()); GentleStatistics.LogStatistics(LogCategories.Cache); // under normal circumstances we should make sure to clean items belonging to the // terminated thread; lets test that this works too :) CacheManager.Clear(threadId); Assert.AreEqual(--cacheCount2ndThread, CacheManager.GetCount(threadId), "Items were not properly flushed from the cache."); } }
static GentleSettings() { Configurator.Configure(typeof(GentleSettings)); // log initial statistics GentleStatistics.LogStatistics(enabledLogCategories); }