public async Task FillCacheWithoutRemovingClients()
        {
            int maxClientCount    = 10;
            var clientWrapperList = new List <(ResourceWrapper <GrpcCopyClient> Wrapper, GrpcCopyClient Client)>();
            var clientConfig      = new GrpcCopyClient.Configuration(bandwidthCheckInterval: TimeSpan.FromSeconds(30), minimumBandwidthMbPerSec: null, clientBufferSize: 65536);

            _clientCache = new GrpcCopyClientCache(_context, clientConfig, maxClientCount: maxClientCount, maxClientAgeMinutes: 63, waitBetweenCleanupMinutes: 30);

            for (int i = 0; i < maxClientCount; i++)
            {
                var clientWrapper = await _clientCache.CreateAsync(LocalHost, i, true);

                clientWrapperList.Add((clientWrapper, clientWrapper.Value));
            }

            // Create new clients for every port
            Assert.Equal(maxClientCount, _clientCache.Counter.GetCounterValue(ResourcePoolCounters.Created));

            // Zero clients were cleaned
            Assert.Equal(0, _clientCache.Counter.GetCounterValue(ResourcePoolCounters.Cleaned));

            // Zero clients were reused
            Assert.Equal(0, _clientCache.Counter.GetCounterValue(ResourcePoolCounters.Reused));

            foreach (var c in clientWrapperList)
            {
                c.Wrapper._lastUseTime -= TimeSpan.FromDays(1);
                c.Wrapper.Dispose();
            }

            await _clientCache.CleanupAsync();

            // All clients were cleaned
            Assert.Equal(maxClientCount, _clientCache.Counter.GetCounterValue(ResourcePoolCounters.Cleaned));
        }
        /// <summary>
        /// Constructor for <see cref="GrpcFileCopier"/>.
        /// </summary>
        public GrpcFileCopier(Context context, GrpcCopyClient.Configuration clientConfig, int grpcPort, int maxGrpcClientCount, int maxGrpcClientAgeMinutes, int grpcClientCleanupDelayMinutes, bool useCompression = false)
        {
            _context        = context;
            _grpcPort       = grpcPort;
            _useCompression = useCompression;

            _clientCache = new GrpcCopyClientCache(context, clientConfig, maxGrpcClientCount, maxGrpcClientAgeMinutes, grpcClientCleanupDelayMinutes);
        }
        internal void DistributedService
        (
            [Description("Path to DistributedContentSettings file")] string settingsPath,
            [Description("Cache name")] string cacheName,
            [Description("Cache root path")] string cachePath,
            [DefaultValue((int)ServiceConfiguration.GrpcDisabledPort), Description(GrpcPortDescription)] int grpcPort,
            [Description("Name of the memory mapped file used to share GRPC port. 'CASaaS GRPC port' if not specified.")] string grpcPortFileName,
            [DefaultValue(null), Description("Writable directory for service operations (use CWD if null)")] string dataRootPath,
            [DefaultValue(null), Description("Identifier for the stamp this service will run as")] string stampId,
            [DefaultValue(null), Description("Identifier for the ring this service will run as")] string ringId,
            [DefaultValue(Constants.OneMB), Description("Max size quota in MB")] int maxSizeQuotaMB,
            [DefaultValue(false)] bool debug,
            [DefaultValue(false), Description("Whether or not GRPC is used for file copies")] bool useDistributedGrpc,
            [DefaultValue(false), Description("Whether or not GZip is used for GRPC file copies")] bool useCompressionForCopies,
            [DefaultValue(null), Description("Buffer size for streaming GRPC copies")] int?bufferSizeForGrpcCopies,
            [DefaultValue(null), Description("Files greater than this size are compressed if compression is used")] int?gzipBarrierSizeForGrpcCopies
        )
        {
            Initialize();

            if (debug)
            {
                System.Diagnostics.Debugger.Launch();
            }

            try
            {
                Validate();

                var dcs = JsonConvert.DeserializeObject <DistributedContentSettings>(File.ReadAllText(settingsPath));

                var host = new HostInfo(stampId, ringId, new List <string>());

                if (grpcPort == 0)
                {
                    grpcPort = Helpers.GetGrpcPortFromFile(_logger, grpcPortFileName);
                }

                var(minimumSpeedInMbPerSec, bandwidthCheckIntervalSeconds) = dcs.GetBandwidthCheckSettings();
                var copyClientConfig = new GrpcCopyClient.Configuration(
                    bandwidthCheckInterval: TimeSpan.FromSeconds(bandwidthCheckIntervalSeconds),
                    minimumBandwidthMbPerSec: minimumSpeedInMbPerSec,
                    clientBufferSize: bufferSizeForGrpcCopies);

                var grpcCopier = new GrpcFileCopier(
                    context: new Interfaces.Tracing.Context(_logger),
                    clientConfig: copyClientConfig,
                    grpcPort: grpcPort,
                    maxGrpcClientCount: dcs.MaxGrpcClientCount,
                    maxGrpcClientAgeMinutes: dcs.MaxGrpcClientAgeMinutes,
                    grpcClientCleanupDelayMinutes: dcs.GrpcClientCleanupDelayMinutes,
                    useCompression: useCompressionForCopies);

                var copier = useDistributedGrpc
                        ? grpcCopier
                        : (IAbsolutePathFileCopier) new DistributedCopier();

                var arguments = CreateDistributedCacheServiceArguments(
                    copier: copier,
                    pathTransformer: useDistributedGrpc ? new GrpcDistributedPathTransformer(_logger) : (IAbsolutePathTransformer) new DistributedPathTransformer(),
                    copyRequester: grpcCopier,
                    dcs: dcs,
                    host: host,
                    cacheName: cacheName,
                    cacheRootPath: cachePath,
                    grpcPort: (uint)grpcPort,
                    maxSizeQuotaMB: maxSizeQuotaMB,
                    dataRootPath: dataRootPath,
                    ct: _cancellationToken,
                    bufferSizeForGrpcCopies: bufferSizeForGrpcCopies,
                    gzipBarrierSizeForGrpcCopies: gzipBarrierSizeForGrpcCopies);

                DistributedCacheServiceFacade.RunAsync(arguments).GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }