public void LoadWithCallback_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            mockCacheData.SetupProperty(m => m.IsCached, false);
            mockCacheData.Setup(m => m.Load(false)).Callback(() => mockCacheData.Object.IsCached = true);
            var callbackExecuted = false;

            cacheDataController.Initialize(mockCacheData.Object);
            cacheDataController.LoadWithCallback(() => { callbackExecuted = true; });

            // Wait for a maximum of 1 second for the callback to be called
            for (int i = 0; i < 10; i++)
            {
                if (callbackExecuted)
                {
                    break;
                }
                Thread.Sleep(100);
            }

            if (!callbackExecuted)
            {
                Assert.Fail("Boolean value was not set to true");
            }

            mockCacheData.Verify(m => m.Load(false), Times.Once());
            Assert.IsTrue(cacheDataController.IsCached, "IsCached should be true after load");
        }
        public void Initialize_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            cacheDataController.Initialize(mockCacheData.Object);

            Assert.AreSame(mockCacheData.Object, cacheDataController.Data, "CacheData should be set");
        }
Ejemplo n.º 3
0
        public void Remove_TestMethod()
        {
            ICacheController cacheController = new CacheController();

            var cacheDataController = new CacheDataController();

            cacheController.Add(cacheDataController);
            Assert.IsTrue(cacheController.Cache.Count == 1);

            cacheController.Remove(cacheDataController);
            Assert.IsTrue(cacheController.Cache.Count == 0);
        }
        public void LoadTwice_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            mockCacheData.SetupProperty(m => m.IsCached, false);
            mockCacheData.Setup(m => m.Load(false)).Callback(() => mockCacheData.Object.IsCached = true);

            cacheDataController.Initialize(mockCacheData.Object);
            cacheDataController.Load();
            cacheDataController.Load();

            mockCacheData.Verify(m => m.Load(false), Times.Once());
        }
Ejemplo n.º 5
0
        private static void TestLoadWithCallback(ICacheController cache, CacheDataController data)
        {
            var loadNotificationCount = 0;
            var startStatusReceived = false;
            var completeStatusRecieved = false;

            var mre = new ManualResetEvent(false);

            Action<LoadingStatus, int, string> callback = (status, loadingKey, loadingMessage) =>
            {
                switch (status)
                {
                    case LoadingStatus.Starting:
                        startStatusReceived = true;
                        break;
                    case LoadingStatus.Loading:
                        break;
                    case LoadingStatus.Loaded:
                        completeStatusRecieved = true;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("status");
                }
                // ReSharper disable AccessToModifiedClosure
                loadNotificationCount++;
                // ReSharper restore AccessToModifiedClosure
            };
            cache.SetupLoadNotification(typeof(TestCacheData), CacheNotificationAction.Add,
                callback);
            var callbackRaised = false;
            data.LoadWithCallback(() =>
            {
                callbackRaised = true;
                mre.Set();
            });

            mre.WaitOne(1000);

            Assert.IsTrue(callbackRaised, "Callback was not raised");
            Assert.IsTrue(startStatusReceived, "Start status missing");
            Assert.IsTrue(loadNotificationCount == 2, "There should be 2 LoadNotification messages");
            Assert.IsTrue(completeStatusRecieved, "Complete status missing");

            cache.SetupLoadNotification(typeof(TestCacheData), CacheNotificationAction.Remove, callback);

            // Return to a IsCached = false state
            cache.Invalidate();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Any time a Category, EventPattern or Value changes, we should invalidate the FileData
        /// </summary>
        /// <param name="cacheDataController"></param>
        private void PrepareFileEvents(CacheDataController cacheDataController)
        {
            var file = (cacheDataController.InternalData as FileData);

            if (file == null)
            {
                throw new Exception("Error");
            }

            // If the delegate was already assigned then this will remove it, otherwise the remove will be ignored. - Sara

            // ReSharper disable DelegateSubtraction
            XmlDal.DataModel.EventCacheDataController.InvalidateNotificationEvent -= file.InvalidateEvent;
            XmlDal.DataModel.ValueCacheDataController.InvalidateNotificationEvent -= file.InvalidateValue;

            // ReSharper restore DelegateSubtraction
            XmlDal.DataModel.EventCacheDataController.InvalidateNotificationEvent += file.InvalidateEvent;
            XmlDal.DataModel.ValueCacheDataController.InvalidateNotificationEvent += file.InvalidateValue;
        }
Ejemplo n.º 7
0
        private static CacheController PrepareCache(out MemoryCacheDataStore dataStore, out CacheDataController data)
        {
            var cache = new CacheController();
            dataStore = new MemoryCacheDataStore();
            cache.SetupDataStore(dataStore);

            data = CacheFactory.CreateCacheData(cache, typeof(TestCacheData));
            // The following line of code will force a reload
            var testData = data.Data as TestCacheData;
            Assert.IsNotNull(testData, "Test Data should not be null");
            testData.Model = MODEL_DATA;
            // This will return us to an IsCached = false state - Sara
            cache.Invalidate();

            return cache;
        }