public async Task CacheData_WhenCacheIsUnavailable_ReturnDefaultValueAndCheckConnectionTimeout()
        {
            // Arrange
            var tempCache = new CacheAccessor(new CacheConfig
            {
                ConnectionString = "invalid_connection_string"
            }); // configured with an incorrect server and will return null on error

            // Act
            var testValue = await tempCache.GetAsync <string>("lovely_non_existent_item");

            Thread.Sleep(50);
            var status1 = tempCache.Status;

            await tempCache.InitialiseIfNecessaryAsync();

            var status2 = tempCache.Status;

            // Assert
            Assert.IsNull(testValue);
            Assert.AreEqual(CacheAccessor.State.Connecting, status1);
            Assert.AreEqual(CacheAccessor.State.NotConnected, status2);
            Assert.IsTrue(tempCache.LastConnectionElapsedMilliseconds > 0 && tempCache.LastConnectionElapsedMilliseconds < 12000,
                          "The connection timeout should be more than 0 but less than 12 seconds. Actual timeouts are never inline with the stated max timeout, so for 10ms, the actual timeout is ~5s");
        }
        public async Task CacheData_WhenCacheIsUnavailable_ReturnNull()
        {
            // Arrange
            var tempCache = new CacheAccessor(new CacheConfig
            {
                ConnectionString = "invalid_connection_string"
            }); // configured with an incorrect server and will return null on error

            // Act
            var testValue = await tempCache.GetAsync <string>("lovely_non_existent_item");

            Assert.IsNull(testValue);
        }
        public async Task Cache_TestValueTypeObject_Roundtrips()
        {
            var cache = new CacheAccessor(new CacheConfig {
                IsAuditingEnabled = true, IsCentralCacheEnabled = false
            });
            var ops   = cache;
            var key   = Guid.NewGuid().ToString("N");
            var value = 4658792;
            await cache.InitialiseIfNecessaryAsync();

            await ops.SetAsync(key, value, null);

            var value2 = await cache.GetAsync <int>(key);

            Assert.AreEqual(value, value2);
        }
        public async Task Cache_TestWithoutRedis()
        {
            var cache = new CacheAccessor(new CacheConfig {
                IsAuditingEnabled = true, IsCentralCacheEnabled = false
            });
            var ops   = cache;
            var key   = Guid.NewGuid().ToString("N");
            var value = new Employee(3456, "test");
            await cache.InitialiseIfNecessaryAsync();

            await ops.SetAsync(key, value, null);

            var value2 = await cache.GetAsync <Employee>(key);

            Assert.AreEqual(cache.AuditLog[0].CachingEvent, eCacheEvent.KeySetInMemory);
            Assert.AreEqual(cache.AuditLog[1].CachingEvent, eCacheEvent.KeyValueGotFromMemoryAttempt);
            Assert.AreEqual(cache.AuditLog[2].CachingEvent, eCacheEvent.KeyValueGotFromMemory);
        }
        public async Task Cache_TestCustomObjects_AreNotClones()
        {
            var cache = new CacheAccessor(new CacheConfig {
                IsAuditingEnabled = true, IsCentralCacheEnabled = false
            });
            var ops   = cache;
            var key   = Guid.NewGuid().ToString("N");
            var value = new Employee(3456, "test");
            await cache.InitialiseIfNecessaryAsync();

            await ops.SetAsync(key, value, null);

            var value2 = await cache.GetAsync <Employee>(key);

            Assert.AreEqual(value.Id, value2.Id);
            Assert.AreEqual(value.Name, value2.Name);
            Assert.AreNotEqual(value, value2);
        }
        public async Task Cache_TestDictionaryObject_Roundtrips()
        {
            var cache = new CacheAccessor(new CacheConfig {
                IsAuditingEnabled = true, IsCentralCacheEnabled = false
            });
            var ops   = cache;
            var key   = Guid.NewGuid().ToString("N");
            var value = (IDictionary <string, string>) new Dictionary <string, string> {
                { "k", "v" }
            };
            await cache.InitialiseIfNecessaryAsync();

            await ops.SetAsync(key, value, null);

            var value2 = await cache.GetAsync <Dictionary <string, string> >(key);

            Assert.AreEqual(value, value2);
        }
        public async Task Cache_TestEnumerableObject_Roundtrips()
        {
            var cache = new CacheAccessor(new CacheConfig {
                IsAuditingEnabled = true, IsCentralCacheEnabled = false
            });
            var ops   = cache;
            var key   = Guid.NewGuid().ToString("N");
            var value = (IEnumerable) new List <string> {
                "this is a thing in the list"
            };
            await cache.InitialiseIfNecessaryAsync();

            await ops.SetAsync(key, value, null);

            var value2 = await cache.GetAsync <IEnumerable>(key);

            Assert.AreEqual(value, value2);
        }