public void ShouldScheduleCacheCleanOnTimer()
        {
            var interval = TimeSpan.FromMilliseconds(1);
            var bus      = new FakePublisher();
            var sut      = new EventStoreClusterClientCache(bus, EventStoreClusterClientFactory, interval, interval);

            sut.Handle(new SystemMessage.SystemInit());

            Assert.True(bus.Messages.OfType <TimerMessage.Schedule>().Count() == 1);
        }
        public async Task CleansCacheOnThreshold()
        {
            var interval         = TimeSpan.FromMinutes(30);
            var oldItemThreshold = TimeSpan.FromMilliseconds(500);
            var sut = new EventStoreClusterClientCache(new FakePublisher(), EventStoreClusterClientFactory, interval,
                                                       oldItemThreshold);
            var oldClient = sut.Get(new IPEndPoint(IPAddress.Loopback, 1113));

            sut.Handle(new ClusterClientMessage.CleanCache());
            await Task.Delay(oldItemThreshold.Add(TimeSpan.FromMilliseconds(500)));

            var newClient = sut.Get(new IPEndPoint(IPAddress.Loopback, 1113));

            newClient = sut.Get(new IPEndPoint(IPAddress.Loopback, 1113));
            Assert.AreNotEqual(oldClient, newClient);
        }
        public async Task ShouldDisposeClientOnceEvictedFromCache()
        {
            var interval         = TimeSpan.FromMinutes(30);
            var oldItemThreshold = TimeSpan.FromMilliseconds(500);
            var sut = new EventStoreClusterClientCache(new FakePublisher(), EventStoreClusterClientFactory, interval,
                                                       oldItemThreshold);
            var client = sut.Get(new IPEndPoint(IPAddress.Loopback, 1113));

            Assert.NotNull(client);

            await Task.Delay(oldItemThreshold.Add(TimeSpan.FromMilliseconds(500)));

            sut.Handle(new ClusterClientMessage.CleanCache());
            // Give the cache enough time to dispose the client
            await Task.Delay(oldItemThreshold);

            Assert.True(client.Disposed);
        }