Exemple #1
0
        public void ExecuteEvictionLogsTheNumberOfRecordsRemoved()
        {
            // given
            var configuration = MockBeaconCacheConfig(1000L, 1000L, 2000L);
            var target        = CreateTimeEvictionStrategyWith(configuration);
            var keyOne        = new BeaconKey(1, 0);
            var keyTwo        = new BeaconKey(42, 0);

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

            mockBeaconCache.BeaconKeys.Returns(new HashSet <BeaconKey> {
                keyOne, keyTwo
            });
            mockBeaconCache.EvictRecordsByAge(keyOne, Arg.Any <long>()).Returns(2);
            mockBeaconCache.EvictRecordsByAge(keyTwo, Arg.Any <long>()).Returns(5);

            mockLogger.IsDebugEnabled.Returns(true);

            // when
            target.Execute();

            // then verify that the logger was invoked
            _ = mockLogger.Received(2).IsDebugEnabled;
            mockLogger.Received(1).Debug($"{target.GetType().Name} - Removed 2 records from Beacon with key {keyOne}");
            mockLogger.Received(1).Debug($"{target.GetType().Name} - Removed 5 records from Beacon with key {keyTwo}");
        }
Exemple #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");
        }
Exemple #3
0
        private void DoExecute()
        {
            // first get a snapshot of all inserted beacons
            var beaconKeys = beaconCache.BeaconKeys;

            if (beaconKeys.Count == 0)
            {
                // no beacons - set last run timestamp and return immediately
                LastRunTimestamp = timingProvider.ProvideTimestampInMilliseconds();
                return;
            }

            // retrieve the timestamp when we start with execution
            var currentTimestamp = timingProvider.ProvideTimestampInMilliseconds();
            var smallestAllowedBeaconTimestamp = currentTimestamp - configuration.MaxRecordAge;

            // iterate over the previously obtained set and evict for each beacon
            using (IEnumerator <BeaconKey> beaconKeyIterator = beaconKeys.GetEnumerator())
            {
                while (!isShutdownFunc() && beaconKeyIterator.MoveNext())
                {
                    var beaconKey         = beaconKeyIterator.Current;
                    var numRecordsRemoved = beaconCache.EvictRecordsByAge(beaconKey, smallestAllowedBeaconTimestamp);
                    if (numRecordsRemoved > 0 && logger.IsDebugEnabled)
                    {
                        logger.Debug($"{GetType().Name} - Removed {numRecordsRemoved.ToInvariantString()} records from Beacon with key {beaconKey}");
                    }
                }
            }

            // last but not least update the last runtime
            LastRunTimestamp = currentTimestamp;
        }