Example #1
0
        public void ExecuteEvictionIsStoppedIfThreadGetsInterrupted()
        {
            // given
            var shutdown      = false;
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, () => shutdown);

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(1000L, 2099L);

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

            // when
            target.Execute();

            // then verify interactions
            var tmp = mockBeaconCache.Received(1).BeaconIDs;

            mockBeaconCache.Received(1).EvictRecordsByAge(Arg.Any <int>(), 2099L - configuration.MaxRecordAge);
        }
Example #2
0
        public void ExecuteEvictionLogsTheNumberOfRecordsRemoved()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(1000L, 2099L);

            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                1, 42
            });
            mockBeaconCache.EvictRecordsByAge(1, Arg.Any <long>()).Returns(2);
            mockBeaconCache.EvictRecordsByAge(42, Arg.Any <long>()).Returns(5);

            mockLogger.IsDebugEnabled.Returns(true);

            // when
            target.Execute();

            // then verify that the logger was invoked
            var tmp = mockLogger.Received(2).IsDebugEnabled;

            mockLogger.Received(1).Debug(target.GetType().Name + " - Removed 2 records from Beacon with ID 1");
            mockLogger.Received(1).Debug(target.GetType().Name + " - Removed 5 records from Beacon with ID 42");
        }
Example #3
0
        public void ExecuteEvictionDoesNotLogIfStrategyIsDisabledAndInfoIsDisabledInLogger()
        {
            // given
            var configuration = new BeaconCacheConfiguration(0L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            mockLogger.IsInfoEnabled.Returns(false);

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

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

            mockLogger.DidNotReceiveWithAnyArgs().Info(string.Empty);
            mockBeaconCache.DidNotReceiveWithAnyArgs().EvictRecordsByAge(0, 0L);

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

            // then
            tmp = mockLogger.Received(2).IsInfoEnabled;
            mockLogger.DidNotReceiveWithAnyArgs().Info(string.Empty);
            mockBeaconCache.DidNotReceiveWithAnyArgs().EvictRecordsByAge(0, 0L);
        }
Example #4
0
        public void TheStrategyIsNotDisabledIFMaxRecordAgeIsGreaterThanZero()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            // then
            Assert.That(target.IsStrategyDisabled, Is.False);
        }
Example #5
0
        public void TheStrategyIsDisabledIfBeaconMaxAgeIsSetToZero()
        {
            // given
            var configuration = new BeaconCacheConfiguration(0L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            // then
            Assert.That(target.IsStrategyDisabled, Is.True);
        }
Example #6
0
        public void TheInitialLastRunTimestampIsMinusOne()
        {
            // given
            var configuration = new BeaconCacheConfiguration(-1L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            // then
            Assert.That(target.LastRunTimestamp, Is.EqualTo(-1L));
        }
Example #7
0
        public void ShouldRunGivesFalseIfLastRunIsLessThanMaxAgeMillisecondsAgo()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc)
            {
                LastRunTimestamp = 1000
            };

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(target.LastRunTimestamp + configuration.MaxRecordAge - 1);

            // then
            Assert.That(target.ShouldRun, Is.False);
        }
Example #8
0
        public void ExecuteEvictionStopsIfNoBeaconIdsAreAvailableInCache()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(1000L, 2000L);
            mockBeaconCache.BeaconIDs.Returns(new HashSet <int>());

            // when
            target.Execute();

            // then verify interactions
            var tmp = mockBeaconCache.Received(1).BeaconIDs;

            mockTimingProvider.Received(3).ProvideTimestampInMilliseconds();
            mockBeaconCache.DidNotReceiveWithAnyArgs().EvictRecordsByAge(0, 0L);

            // also ensure that the last run timestamp was updated
            Assert.That(target.LastRunTimestamp, Is.EqualTo(2000L));
        }
Example #9
0
        public void LastRuntimeStampIsAdjustedDuringFirstExecution()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(1000L, 1001L);

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

            // then
            Assert.That(target.LastRunTimestamp, Is.EqualTo(1000L));
            mockTimingProvider.Received(2).ProvideTimestampInMilliseconds();

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

            // then
            Assert.That(target.LastRunTimestamp, Is.EqualTo(1000L));
            mockTimingProvider.Received(3).ProvideTimestampInMilliseconds();
        }
Example #10
0
        public void ExecuteEvictionCallsEvictionForEachBeaconSeparately()
        {
            // given
            var configuration = new BeaconCacheConfiguration(1000L, 1000L, 2000L);
            var target        = new TimeEvictionStrategy(mockLogger, mockBeaconCache, configuration, mockTimingProvider, isShutdownFunc);

            mockTimingProvider.ProvideTimestampInMilliseconds().Returns(1000L, 2099L);
            mockBeaconCache.BeaconIDs.Returns(new HashSet <int> {
                1, 42
            });

            // when
            target.Execute();

            // then verify interactions
            var tmp = mockBeaconCache.Received(1).BeaconIDs;

            mockBeaconCache.Received(1).EvictRecordsByAge(1, 2099L - configuration.MaxRecordAge);
            mockBeaconCache.Received(1).EvictRecordsByAge(42, 2099L - configuration.MaxRecordAge);
            mockTimingProvider.Received(3).ProvideTimestampInMilliseconds();

            // also ensure that the last run timestamp was updated
            Assert.That(target.LastRunTimestamp, Is.EqualTo(2099L));
        }