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 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 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 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 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 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 OpenKitInitializer(IOpenKitBuilder builder) { logger = builder.Logger; privacyConfiguration = PrivacyConfiguration.From(builder); openKitConfiguration = OpenKitConfiguration.From(builder); timingProvider = new DefaultTimingProvider(); threadIdProvider = new DefaultThreadIdProvider(); sessionIdProvider = new DefaultSessionIdProvider(); beaconCache = new BeaconCache(logger); beaconCacheEvictor = new BeaconCacheEvictor(logger, beaconCache, BeaconCacheConfiguration.From(builder), timingProvider); var httpClientConfig = HttpClientConfiguration.From(openKitConfiguration); // shared thread suspender between BeaconSender and HttpClient. HttpClient will be woken up when // BeaconSender shuts down var beaconSenderThreadSuspender = new InterruptibleThreadSuspender(); beaconSender = new BeaconSender( logger, httpClientConfig, new DefaultHttpClientProvider(logger, beaconSenderThreadSuspender), timingProvider, beaconSenderThreadSuspender); var watchdogThreadSuspender = new InterruptibleThreadSuspender(); sessionWatchdog = new SessionWatchdog( logger, new SessionWatchdogContext(timingProvider, watchdogThreadSuspender)); }
/// <summary> /// Constructor. /// </summary> /// <param name="logger">nstance implementing the <see cref="ILogger"/> interface for writing some useful debug messages.</param> /// <param name="beaconCache">The beacon cache to evict if necessary.</param> /// <param name="configuration"></param> internal SpaceEvictionStrategy(ILogger logger, IBeaconCache beaconCache, BeaconCacheConfiguration configuration, Func <bool> isShutdownFunc) { this.logger = logger; this.beaconCache = beaconCache; this.configuration = configuration; this.isShutdownFunc = isShutdownFunc; }
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 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 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); }
internal TimeEvictionStrategy(ILogger logger, IBeaconCache beaconCache, BeaconCacheConfiguration configuration, ITimingProvider timingProvider, Func <bool> isShutdownFunc) { this.logger = logger; this.beaconCache = beaconCache; this.configuration = configuration; this.timingProvider = timingProvider; this.isShutdownFunc = isShutdownFunc; LastRunTimestamp = -1; }
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); }
/// <summary> /// Public constructor, initializing the eviction thread with the default <see cref="TimeEvictionStrategy"/> /// and <see cref="SpaceEvictionStrategy"/> strategies. /// </summary> /// <param name="logger">Logger to write some debug output</param> /// <param name="beaconCache">The Beacon cache to check if entries need to be evicted</param> /// <param name="configuration">Beacon cache configuration</param> /// <param name="timingProvider">Timing provider required for time retrieval</param> public BeaconCacheEvictor(ILogger logger, IBeaconCache beaconCache, BeaconCacheConfiguration configuration, ITimingProvider timingProvider) : this(logger, beaconCache) { strategies = new IBeaconCacheEvictionStrategy[] { new TimeEvictionStrategy(logger, beaconCache, configuration, timingProvider, () => IsShutdownRequested), new SpaceEvictionStrategy(logger, beaconCache, configuration, () => IsShutdownRequested) }; }
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); }
private IBeaconCacheConfiguration MockBeaconCacheConfig(long maxRecordAge, long lowerSizeBound, long upperSizeBound) { var builder = Substitute.For <IOpenKitBuilder>(); builder.BeaconCacheMaxBeaconAge.Returns(maxRecordAge); builder.BeaconCacheLowerMemoryBoundary.Returns(lowerSizeBound); builder.BeaconCacheUpperMemoryBoundary.Returns(upperSizeBound); var config = BeaconCacheConfiguration.From(builder); return(config); }
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); }
internal override OpenKitConfiguration BuildConfiguration() { var device = new Device(OperatingSystem, Manufacturer, ModelID); var beaconCacheConfig = new BeaconCacheConfiguration( BeaconCacheMaxBeaconAge, BeaconCacheLowerMemoryBoundary, BeaconCacheUpperMemoryBoundary); return(new OpenKitConfiguration( OpenKitType.APPMON, applicationName, applicationName, DeviceID, EndpointURL, new DefaultSessionIDProvider(), TrustManager, device, ApplicationVersion, beaconCacheConfig)); }
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)); }
internal override OpenKitConfiguration BuildConfiguration() { var device = new Device(OperatingSystem, Manufacturer, ModelID); var beaconCacheConfig = new BeaconCacheConfiguration( BeaconCacheMaxBeaconAge, BeaconCacheLowerMemoryBoundary, BeaconCacheUpperMemoryBoundary); var beaconConfig = new BeaconConfiguration(BeaconConfiguration.DEFAULT_MULITPLICITY, DataCollectionLevel, CrashReportingLevel); return(new OpenKitConfiguration( OpenKitType.DYNATRACE, applicationName, applicationID, DeviceID, EndpointURL, new DefaultSessionIDProvider(), TrustManager, device, ApplicationVersion, beaconCacheConfig, beaconConfig)); }
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 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 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)); }
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>()); }