public void ExecuteEvictionStopsIfNumBytesInCacheFallsBelowLowerBoundBetweenTwoBeacons()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,                                       //should run method
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    configuration.CacheSizeLowerBound,                                           // stops already (second iteration)
                                                    0L);                                                                         // just for safety

            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });

            // when executing
            target.Execute();

            // then
            var tmp = mockBeaconCache.Received(8).NumBytesInCache;

            mockBeaconCache.Received(3).EvictRecordsByNumber(Arg.Any <int>(), 1);
        }
        public void ExecuteEvictionRunsUntilTheCacheSizeIsLessThanOrEqualToLowerBound()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,                                       //should run method
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    configuration.CacheSizeLowerBound,                                           // stops already
                                                    0L);                                                                         // just for safety

            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });


            // when executing the first time
            target.Execute();

            // then
            var tmp = mockBeaconCache.Received(8).NumBytesInCache;

            mockBeaconCache.Received(2).EvictRecordsByNumber(1, 1);
            mockBeaconCache.Received(2).EvictRecordsByNumber(42, 1);
        }
        public void ÉxecuteEvictionStopsIfThreadGetsInterruptedBetweenTwoBeacons()
        {
            // given
            var shutdown      = false;
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, () => shutdown);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,                                       //should run method
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    configuration.CacheSizeUpperBound,                                           // first iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    (configuration.CacheSizeUpperBound + configuration.CacheSizeLowerBound) / 2, // second iteration
                                                    configuration.CacheSizeLowerBound,                                           // stops already
                                                    0L);                                                                         // just for safety

            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });
            mockBeaconCache.EvictRecordsByNumber(Arg.Any <int>(), 1).Returns(x =>
            {
                shutdown = true;
                return(5);
            });

            // when executing
            target.Execute();

            // then
            var tmp = mockBeaconCache.Received(3).NumBytesInCache;

            mockBeaconCache.Received(1).EvictRecordsByNumber(Arg.Any <int>(), 1);
        }
        public void ExecuteEvictionDoesNotLogEvictionResultIfDebugIsDisabled()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    0L);
            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });
            mockBeaconCache.EvictRecordsByNumber(1, Arg.Any <int>()).Returns(5);
            mockBeaconCache.EvictRecordsByNumber(42, Arg.Any <int>()).Returns(1);

            mockLogger.IsDebugEnabled.Returns(false);

            // when executing the first time
            target.Execute();

            // then
            var tmp = mockLogger.Received(3).IsDebugEnabled;

            mockLogger.DidNotReceive().Debug(Arg.Any <string>());
        }
        public void ExecuteEvictionLogsEvictionResultIfDebugIsEnabled()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    0L);
            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });
            mockBeaconCache.EvictRecordsByNumber(1, Arg.Any <int>()).Returns(5);
            mockBeaconCache.EvictRecordsByNumber(42, Arg.Any <int>()).Returns(1);

            mockLogger.IsDebugEnabled.Returns(true);

            // when executing the first time
            target.Execute();

            // then
            var tmp = mockLogger.Received(3).IsDebugEnabled;

            mockLogger.Received(1).Debug("Removed 5 records from Beacon with ID 1");
            mockLogger.Received(1).Debug("Removed 1 records from Beacon with ID 42");
        }
        public void TheStrategyIsDisabledIfCacheSizeUpperBoundIsLessThanLowerBound()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 999L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            // then
            Assert.That(target.IsStrategyDisabled, Is.True);
        }
        public void ShouldRunGivesTrueIfNumBytesInCacheIsGreaterThanUpperBoundLimit()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1);

            // then
            Assert.That(target.ShouldRun, Is.True);
        }
        public void ExecuteEvictionDoesNotLogIfStrategyIsDisabledAndInfoIsDisabledInLogger()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, -1L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockLogger.IsInfoEnabled.Returns(false);

            // when executing the first time
            target.Execute();

            // then
            var tmp = mockLogger.Received(1).IsInfoEnabled;

            mockLogger.DidNotReceive().Info(Arg.Any <string>());

            // and when executing a second time
            target.Execute();

            // then
            tmp = mockLogger.Received(2).IsInfoEnabled;
            mockLogger.DidNotReceive().Info(Arg.Any <string>());
        }
        public void ExecuteEvictionCallsCacheMethodForEachBeacon()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    0L);
            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                42, 1
            });

            // when executing the first time
            target.Execute();

            // then
            var tmp = mockBeaconCache.Received(5).NumBytesInCache;

            mockBeaconCache.Received(1).EvictRecordsByNumber(1, 1);
            mockBeaconCache.Received(1).EvictRecordsByNumber(42, 1);
        }
        public void ExecuteEvictionLogsAMessageOnceAndReturnsIfStrategyIsDisabled()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, -1L);
            var target        = new SpaceEvictionStrategy(mockLogger, mockBeaconCache, configuration, isShutdownFunc);

            mockLogger.IsInfoEnabled.Returns(true);

            // when executing the first time
            target.Execute();

            // then
            var tmp = mockLogger.Received(1).IsInfoEnabled;

            mockLogger.Received(1).Info(Arg.Any <string>());

            // and when executing a second time
            mockLogger.ClearReceivedCalls();
            target.Execute();

            // then
            tmp = mockLogger.DidNotReceive().IsInfoEnabled;
            mockLogger.DidNotReceive().Info(Arg.Any <string>());
        }