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");
        }
Ejemplo n.º 2
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();
        }