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); }
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"); }
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); }
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); }
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); }
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)); }
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); }
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)); }
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(); }
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)); }