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 ExecuteEvictionLogsEvictionResultIfDebugIsEnabled()
        {
            // given
            var configuration = MockBeaconCacheConfig(1000L, 1000L, 2000L);
            var target        = CreateSpaceEvictionStrategyWith(configuration);

            mockBeaconCache.NumBytesInCache.Returns(configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    configuration.CacheSizeUpperBound + 1,
                                                    0L);
            var keyOne = new BeaconKey(42, 0);
            var keyTwo = new BeaconKey(1, 0);

            mockBeaconCache.BeaconKeys.Returns(new HashSet <BeaconKey> {
                keyOne, keyTwo
            });
            mockBeaconCache.EvictRecordsByNumber(keyTwo, Arg.Any <int>()).Returns(5);
            mockBeaconCache.EvictRecordsByNumber(keyOne, Arg.Any <int>()).Returns(1);

            mockLogger.IsDebugEnabled.Returns(true);

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

            // then
            _ = mockLogger.Received(3).IsDebugEnabled;
            mockLogger.Received(1).Debug($"{target.GetType().Name} - Removed 1 records from Beacon with key {keyOne}");
            mockLogger.Received(1).Debug($"{target.GetType().Name} - Removed 5 records from Beacon with key {keyTwo}");
        }
        /// <summary>
        /// Performs execution of strategy.
        /// </summary>
        private void DoExecute()
        {
            var removedRecordsPerBeacon = new Dictionary <BeaconKey, int>();

            while (!isShutdownFunc() && beaconCache.NumBytesInCache > configuration.CacheSizeLowerBound)
            {
                var beaconKeys = beaconCache.BeaconKeys;
                using (IEnumerator <BeaconKey> iterator = beaconKeys.GetEnumerator())
                {
                    while (!isShutdownFunc() && iterator.MoveNext() &&
                           beaconCache.NumBytesInCache > configuration.CacheSizeLowerBound)
                    {
                        var beaconKey = iterator.Current;

                        // remove 1 record from Beacon cache for given beaconKey
                        // the result is the number of records removed, which might be in range [0, numRecords=1]
                        var numRecordsRemoved = beaconCache.EvictRecordsByNumber(beaconKey, 1);

                        if (!logger.IsDebugEnabled)
                        {
                            continue;
                        }

                        if (!removedRecordsPerBeacon.ContainsKey(beaconKey))
                        {
                            removedRecordsPerBeacon.Add(beaconKey, numRecordsRemoved);
                        }
                        else
                        {
                            removedRecordsPerBeacon[beaconKey] += numRecordsRemoved;
                        }
                    }
                }
            }

            if (logger.IsDebugEnabled)
            {
                foreach (var entry in removedRecordsPerBeacon)
                {
                    logger.Debug($"{GetType().Name} - Removed {entry.Value.ToInvariantString()} records from Beacon with key {entry.Key}");
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Performs execution of strategy.
        /// </summary>
        private void DoExecute()
        {
            var removedRecordsPerBeacon = new Dictionary <int, int>();

            while (!isShutdownFunc() && beaconCache.NumBytesInCache > configuration.CacheSizeLowerBound)
            {
                var beaconIDs = beaconCache.BeaconIDs;
                IEnumerator <int> iterator = beaconIDs.GetEnumerator();

                while (!isShutdownFunc() && iterator.MoveNext() && beaconCache.NumBytesInCache > configuration.CacheSizeLowerBound)
                {
                    var beaconID = iterator.Current;

                    // remove 1 record from Beacon cache for given beaconID
                    // the result is the number of records removed, which might be in range [0, numRecords=1]
                    int numRecordsRemoved = beaconCache.EvictRecordsByNumber(beaconID, 1);

                    if (logger.IsDebugEnabled)
                    {
                        if (!removedRecordsPerBeacon.ContainsKey(beaconID))
                        {
                            removedRecordsPerBeacon.Add(beaconID, numRecordsRemoved);
                        }
                        else
                        {
                            removedRecordsPerBeacon[beaconID] += numRecordsRemoved;
                        }
                    }
                }
            }

            if (logger.IsDebugEnabled)
            {
                foreach (var entry in removedRecordsPerBeacon)
                {
                    logger.Debug("Removed " + entry.Value + " records from Beacon with ID " + entry.Key);
                }
            }
        }