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