public void CacheData_TestThatMultipleInitialisationsResultsInOne()
        {
            var tempCache = CacheAccessor.Create();

            Parallel.For(0, 20, i =>
            {
                tempCache.InitialiseIfNecessaryAsync();
            });

            var connectingTask = tempCache.InitialiseIfNecessaryAsync();
            var status1        = tempCache.Status;

            connectingTask.Wait(); // wait for connection to complete
            var status2 = tempCache.Status;

            Assert.AreEqual(1, tempCache.ConnectionAttemptsCount, "Only 1 connection attempt should have been made in-spite of the bombardment of connection requests");
            Assert.IsTrue(status1 == CacheAccessor.State.NotConnected || status1 == CacheAccessor.State.Connecting);
            Assert.AreEqual(CacheAccessor.State.Connected, status2, "The cache should have connected by status2");
        }
        public void Cache_SetIsReallyAsync()
        {
            var cache = CacheAccessor.Create();
            var key   = Key();
            var value = CreateGuid();

            Task settingTask = cache.SetAsync(key, value);

            List <bool> flags = new List <bool>();

            while (true)
            {
                flags.Add(settingTask.IsCompleted);
                if (settingTask.IsCompleted)
                {
                    break;
                }
                Thread.Sleep(10);
            }

            Assert.IsTrue(flags.Where(x => x == false).Count() > 0, "There should be at least a few points where the setting task was running");
            Assert.IsTrue(flags.Where(x => x).Count() > 0, "Some flags should indicate the task completed");
        }
        public async Task Cache_TestThatConnectIsAsync()
        {
            const long MAX_CONNECTION_TIME_MS = 60000;

            // Arrange
            var key   = Key();
            var value = CreateGuid();

            // Put something in the cache, the close the connection
            using (var cache = CacheAccessor.Create())
                await cache.SetAsync(key, value);

            // Re-create the cache
            using (var cache = CacheAccessor.Create())
            {
                // Act
                var cacheValue = await cache.GetAsync <string>(key);

                Assert.IsNull(cacheValue);

                var sw = Stopwatch.StartNew();
                while (cache.Status == CacheAccessor.State.NotConnected || cache.Status == CacheAccessor.State.Connecting)
                {
                    await Task.Delay(500);

                    Assert.IsFalse(sw.ElapsedMilliseconds > MAX_CONNECTION_TIME_MS, "Connecting took too long ( > {0})", sw.ElapsedMilliseconds);
                }
                sw.Stop();
                Assert.IsFalse(sw.ElapsedMilliseconds > MAX_CONNECTION_TIME_MS, "Connecting took too long ( > {0})", sw.ElapsedMilliseconds);

                cacheValue = await cache.GetAsync <string>(key);

                Assert.AreEqual(value, cacheValue);

                await cache.DeleteAsync(key);
            }
        }
 public void Setup()
 {
     // Create cache that is actually connected
     _cacheThatsConnected = CacheAccessor.Create();
     _cacheThatsConnected.InitialiseIfNecessaryAsync().Wait();
 }