private void EnusreCacheExists()
        {
            if (_cache != null) return;

            lock (_locker)
            {
                if (_cache == null)
                    _cache = new ObjectCache();
            }
        }
        public void Should_not_add_to_cache_when_instance_is_null()
        {
            // Arrange
            var key = Guid.NewGuid();
            var objectCache = new ObjectCache();

            // Act
            objectCache.Set(key, null);

            // Assert
            Assert.That(objectCache.Count, Is.EqualTo(0));
        }
        public void Should_throw_when_key_is_not_unique()
        {
            // Arrange
            var key = Guid.NewGuid();
            var objectCache = new ObjectCache();
            var obj1 = new object();
            var obj2 = new object();

            objectCache.Set(key, obj1);

            // Act & assert
            Assert.Throws<ArgumentException>(() => objectCache.Set(key, obj2));
        }
        public IObjectCache FindCache()
        {
            var items = FindHttpDictionary();
            if (!items.Contains(CacheKey))
            {
                lock (items.SyncRoot)
                {
                    if (!items.Contains(CacheKey))
                    {
                        var cache = new ObjectCache();
                        items.Add(CacheKey, cache);
                        return cache;
                    }
                }
            }

            return (ObjectCache)items[CacheKey];
        }
        public void Should_dispose_IDisposable_instances_when_cleared()
        {
            // Arrange
            var key = Guid.NewGuid();

            var disposable = new Mock<IDisposable>();
            disposable.Setup(x => x.Dispose());

            var objectCache = new ObjectCache();
            objectCache.Set(key, disposable.Object);

            // Act
            objectCache.Clear();

            // Assert
            Assert.That(objectCache.Count, Is.EqualTo(0));
            disposable.Verify(x => x.Dispose(), Times.Exactly(1));
        }
        public void Should_return_existing_cache_when_session_is_available()
        {
            // Arrange
            var cache = new ObjectCache();
            cache.Set(Guid.NewGuid(), new object());

            var dictionary = new Dictionary<string, object>();
            dictionary.Add(HttpSessionLifecycle.CacheKey, cache);

            var lifecycle = new HttpSessionLifecycle();
            HttpSessionLifecycle.HasSessionResolver = () => true;
            HttpSessionLifecycle.DictionaryResolver = () => dictionary;

            // Act
            var result = lifecycle.FindCache() as ObjectCache;

            // Assert
            Assert.That(result, Is.EqualTo(cache));
            Assert.That(result.Count, Is.EqualTo(1));
        }