Example #1
0
        public async Task BandwidthCheckTimesOutOnSlowCopyByUsingCopyToOptions()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec / 2); // Making sure the actual bandwidth checking options more permissive.
            var totalBytes    = actualBandwidthBytesPerSec * 2;
            var checkerConfig = new BandwidthChecker.Configuration(checkInterval, bandwidthLimit, maxBandwidthLimit: null, bandwidthLimitMultiplier: null, historicalBandwidthRecordsStored: null);
            var checker       = new BandwidthChecker(checkerConfig);

            using (var stream = new MemoryStream())
            {
                var bandwidthConfiguration = new BandwidthConfiguration()
                {
                    Interval      = checkInterval,
                    RequiredBytes = actualBandwidthBytesPerSec * 2,
                };

                var options = new CopyOptions(bandwidthConfiguration);
                var result  = await checker.CheckBandwidthAtIntervalAsync(
                    _context,
                    token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth, options),
                    options,
                    getErrorResult : diagnostics => new CopyFileResult(CopyResultCode.CopyBandwidthTimeoutError, diagnostics));

                Assert.Equal(CopyResultCode.CopyBandwidthTimeoutError, result.Code);
            }
        }
        public BandwidthCheckDialog(NetworkType networkType)
        {
            InitializeComponent();
            this.DataContext = this;
            this.NetworkType = networkType;
            Uri    target_uri;
            string uri_key;

            switch (networkType)
            {
            case NetworkType.IPv6:
                uri_key = "BandwidthCheckerV6";
                break;

            case NetworkType.IPv4:
            default:
                uri_key = "BandwidthChecker";
                break;
            }
            if (AppSettingsReader.TryGetUri(uri_key, out target_uri))
            {
                this.checker = new BandwidthChecker(target_uri);
                this.checker.BandwidthCheckCompleted += checker_BandwidthCheckCompleted;
                this.checker.RunAsync();
                this.Status = "帯域測定中";
            }
            else
            {
                this.IsChecking = false;
                this.Status     = "接続先設定が取得できません";
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GrpcCopyClient" /> class.
        /// </summary>
        internal GrpcCopyClient(GrpcCopyClientKey key, Configuration config)
        {
            GrpcEnvironment.InitializeIfNeeded();
            _channel    = new Channel(key.Host, key.GrpcPort, ChannelCredentials.Insecure, GrpcEnvironment.DefaultConfiguration);
            _client     = new ContentServer.ContentServerClient(_channel);
            _bufferSize = config.ClientBufferSize ?? ContentStore.Grpc.CopyConstants.DefaultBufferSize;
            var bandwidthSource = config.MinimumBandwidthMbPerSec == null
                ? (IBandwidthLimitSource) new HistoricalBandwidthLimitSource()
                : new ConstantBandwidthLimit(config.MinimumBandwidthMbPerSec.Value);

            _bandwidthChecker = new BandwidthChecker(bandwidthSource, config.BandwidthCheckInterval);
            Key = key;
        }
Example #4
0
        public async Task BandwidthCheckDoesNotAffectGoodCopies()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec / 2); // Lower limit is half actual bandwidth
            var totalBytes = actualBandwidthBytesPerSec * 2;
            var checker    = new BandwidthChecker(new ConstantBandwidthLimit(bandwidthLimit), checkInterval);

            using (var stream = new MemoryStream())
            {
                await checker.CheckBandwidthAtIntervalAsync(_context, token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth), stream);
            }
        }
Example #5
0
        protected override async void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            Uri    target_uri;
            string uri_key;

            switch (NetworkType)
            {
            case NetworkType.IPv6:
                uri_key = "BandwidthCheckerV6";
                break;

            case NetworkType.IPv4:
            default:
                uri_key = "BandwidthChecker";
                break;
            }
            if (AppSettingsReader.TryGetUri(uri_key, out target_uri))
            {
                Status     = "帯域測定中";
                IsChecking = true;
                cancellationTokenSource = new CancellationTokenSource();
                var checker = new BandwidthChecker(target_uri, NetworkType);
                try {
                    var result = await checker.RunAsync(cancellationTokenSource.Token);

                    if (result.Succeeded)
                    {
                        Result = (int)((result.Bitrate / 1000) * 0.8 / 100) * 100;
                        Status = String.Format("帯域測定完了: {0}kbps, 設定推奨値: {1}kbps",
                                               result.Bitrate / 1000,
                                               (int)((result.Bitrate / 1000) * 0.8 / 100) * 100);
                    }
                    else
                    {
                        Status = "帯域測定失敗。接続できませんでした";
                    }
                }
                catch (OperationCanceledException) {
                    Status = "キャンセルされました";
                }
                IsChecking = false;
            }
            else
            {
                this.IsChecking = false;
                this.Status     = "接続先設定が取得できません";
            }
        }
            public CheckBandwidthCommand(SettingViewModel owner)
            {
                this.owner = owner;
                Uri target_uri;

                if (AppSettingsReader.TryGetUri("BandwidthChecker", out target_uri))
                {
                    this.checker = new BandwidthChecker(target_uri);
                    this.checker.BandwidthCheckCompleted += checker_BandwidthCheckCompleted;
                }
                else
                {
                    canExecute = false;
                }
            }
Example #7
0
        internal GrpcCopyClient(GrpcCopyClientKey key, GrpcCopyClientConfiguration configuration, IClock?clock = null, ByteArrayPool?sharedBufferPool = null)
        {
            Key            = key;
            _configuration = configuration;
            _clock         = clock ?? SystemClock.Instance;

            GrpcEnvironment.WaitUntilInitialized();
            _channel = new Channel(key.Host, key.GrpcPort,
                                   ChannelCredentials.Insecure,
                                   options: GrpcEnvironment.GetClientOptions(_configuration.GrpcCoreClientOptions));

            _client = new ContentServer.ContentServerClient(_channel);

            _bandwidthChecker = new BandwidthChecker(configuration.BandwidthCheckerConfiguration);
            _pool             = sharedBufferPool ?? new ByteArrayPool(_configuration.ClientBufferSizeBytes);
        }
Example #8
0
        public async Task BandwidthCheckTimesOutOnSlowCopy()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec * 2); // Lower limit is twice actual bandwidth
            var totalBytes = actualBandwidthBytesPerSec * 2;
            var checker    = new BandwidthChecker(new ConstantBandwidthLimit(bandwidthLimit), checkInterval);

            using (var stream = new MemoryStream())
            {
                await Assert.ThrowsAsync(
                    typeof(TimeoutException),
                    async() => await checker.CheckBandwidthAtIntervalAsync(_context, token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth), stream));
            }
        }
Example #9
0
        public void TestFlush()
        {
            var checker = new BandwidthChecker();
            var now     = 10000;

            checker.AddSent(1, now - 2);
            checker.AddSent(2, now);
            checker.AddSent(4, now + 2);

            Assert.Equal(2, checker.GetSentPacketsPerSecond(now));

            checker.Flush(now - 1);
            Assert.Equal(1, checker.GetSentPacketsPerSecond(now));

            checker.Flush(now + 999);
            Assert.Equal(0, checker.GetSentPacketsPerSecond(now));
        }
Example #10
0
        public async Task BandwidthCheckTimesOutOnSlowCopy()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec * 2); // Lower limit is twice actual bandwidth
            var totalBytes    = actualBandwidthBytesPerSec * 2;
            var checkerConfig = new BandwidthChecker.Configuration(checkInterval, bandwidthLimit, maxBandwidthLimit: null, bandwidthLimitMultiplier: null, historicalBandwidthRecordsStored: null);
            var checker       = new BandwidthChecker(checkerConfig);

            using (var stream = new MemoryStream())
            {
                var result = await checker.CheckBandwidthAtIntervalAsync(_context, token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth), stream);

                Assert.Equal(CopyFileResult.ResultCode.CopyBandwidthTimeoutError, result.Code);
            }
        }
Example #11
0
        public async Task BandwidthCheckDoesNotAffectGoodCopies()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec / 2); // Lower limit is half actual bandwidth
            var totalBytes    = actualBandwidthBytesPerSec * 2;
            var checkerConfig = new BandwidthChecker.Configuration(checkInterval, bandwidthLimit, maxBandwidthLimit: null, bandwidthLimitMultiplier: null, historicalBandwidthRecordsStored: null);
            var checker       = new BandwidthChecker(checkerConfig);

            using (var stream = new MemoryStream())
            {
                var result = await checker.CheckBandwidthAtIntervalAsync(_context, token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth), stream);

                Assert.True(result.Succeeded);
            }
        }
Example #12
0
        public void TestGetBytesPerSeconds()
        {
            var checker = new BandwidthChecker();
            var now     = 10000;

            // 시간순서로 정렬
            // 오래된 패킷이 가장 먼저 들어간다
            checker.AddSent(1, now - 1001);
            checker.AddSent(2, now - 1000);
            checker.AddSent(4, now - 999);
            checker.AddSent(8, now - 1);
            checker.AddSent(16, now);
            checker.AddSent(32, now + 1);

            Assert.Equal(4 + 8 + 16, checker.GetSentBytesPerSecond(now));
            Assert.Equal(2 + 4 + 8, checker.GetSentBytesPerSecond(now - 1));
            Assert.Equal(1 + 2 + 4, checker.GetSentBytesPerSecond(now - 20));
        }
Example #13
0
        public BandwidthCheckDialog()
        {
            InitializeComponent();
            this.DataContext = this;
            Uri target_uri;

            if (AppSettingsReader.TryGetUri("BandwidthChecker", out target_uri))
            {
                this.checker = new BandwidthChecker(target_uri);
                this.checker.BandwidthCheckCompleted += checker_BandwidthCheckCompleted;
                this.checker.RunAsync();
                this.Status = "帯域測定中";
            }
            else
            {
                this.IsChecking = false;
                this.Status     = "接続先設定が取得できません";
            }
        }
Example #14
0
        public async Task BandwidthCheckDoesNotAffectGoodCopies()
        {
            var checkInterval = TimeSpan.FromSeconds(1);
            var actualBandwidthBytesPerSec = 1024;
            var actualBandwidth            = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec);
            var bandwidthLimit             = MbPerSec(bytesPerSec: actualBandwidthBytesPerSec / 10); // Lower limit significantly to actual bandwidth
            var totalBytes    = actualBandwidthBytesPerSec * 2;
            var checkerConfig = new BandwidthChecker.Configuration(checkInterval, bandwidthLimit, maxBandwidthLimit: null, bandwidthLimitMultiplier: null, historicalBandwidthRecordsStored: null);
            var checker       = new BandwidthChecker(checkerConfig);

            using (var stream = new MemoryStream())
            {
                var options = new CopyOptions(bandwidthConfiguration: null);
                var result  = await checker.CheckBandwidthAtIntervalAsync(
                    _context,
                    token => CopyRandomToStreamAtSpeed(token, stream, totalBytes, actualBandwidth, options),
                    options,
                    getErrorResult : diagnostics => new CopyFileResult(CopyResultCode.CopyBandwidthTimeoutError, diagnostics));

                result.ShouldBeSuccess();
            }
        }
Example #15
0
        internal GrpcCopyClient(Context context, GrpcCopyClientKey key, GrpcCopyClientConfiguration configuration, IClock?clock = null, ByteArrayPool?sharedBufferPool = null)
        {
            Key            = key;
            _configuration = configuration;
            _clock         = clock ?? SystemClock.Instance;

            GrpcEnvironment.WaitUntilInitialized();
            var  channelCreds      = ChannelCredentials.Insecure;
            bool?encryptionEnabled = _configuration.GrpcCoreClientOptions?.EncryptionEnabled;

            Tracer.Info(context, $"Grpc Encryption Enabled = {encryptionEnabled == true}, GRPC Port: {key.GrpcPort}");

            List <ChannelOption> options = new List <ChannelOption>(GrpcEnvironment.GetClientOptions(_configuration.GrpcCoreClientOptions));

            if (encryptionEnabled == true)
            {
                try
                {
                    channelCreds = TryGetSecureChannelCredentials(context, _configuration, out var hostName) ?? ChannelCredentials.Insecure;
                    if (channelCreds != ChannelCredentials.Insecure)
                    {
                        options.Add(new ChannelOption(ChannelOptions.SslTargetNameOverride, hostName));
                    }
                }
                catch (Exception ex)
                {
                    Tracer.Error(context, ex, $"Creating Encrypted Grpc Channel Failed.");
                }
            }

            Tracer.Debug(context, $"Client connecting to {key.Host}:{key.GrpcPort}. Channel Encrypted = {channelCreds != ChannelCredentials.Insecure}");

            _channel = new Channel(key.Host, key.GrpcPort, channelCreds, options: options);
            _client  = new ContentServer.ContentServerClient(_channel);

            _bandwidthChecker = new BandwidthChecker(_configuration.BandwidthCheckerConfiguration);
            _pool             = sharedBufferPool ?? new ByteArrayPool(_configuration.ClientBufferSizeBytes);
        }
Example #16
0
        public async Task CancellationShouldNotCauseTaskUnobservedException()
        {
            // This test checks that the bandwidth checker won't cause task unobserved exception
            // for the task provided via 'copyTaskFactory' if the CheckBandwidthAtIntervalAsync
            // is called and the operation is immediately cancelled.

            var checker = new BandwidthChecker(GetConfiguration());

            using var cts = new CancellationTokenSource();

            OperationContext context         = new OperationContext(new Context(TestGlobal.Logger), cts.Token);
            int numberOfUnobservedExceptions = 0;

            EventHandler <UnobservedTaskExceptionEventArgs> taskSchedulerOnUnobservedTaskException = (o, args) => { numberOfUnobservedExceptions++; };

            try
            {
                TaskScheduler.UnobservedTaskException += taskSchedulerOnUnobservedTaskException;

                // Cancelling the operation even before starting it.
                cts.Cancel();

                // Using task completion source as an event to force the task completion in a specific time.
                var tcs = new TaskCompletionSource <object>();

                using var stream = new MemoryStream();
                var resultTask = checker.CheckBandwidthAtIntervalAsync(
                    context,
                    copyTaskFactory: token => Task.Run <CopyFileResult>(
                        async() =>
                {
                    await tcs.Task;

                    throw new Exception("1");
                }),
                    destinationStream: stream);

                await Task.Delay(10);

                try
                {
                    (await resultTask).IgnoreFailure();
                }
                catch (OperationCanceledException)
                {
                }

                // Triggering a failure
                tcs.SetResult(null);

                // Forcing a full GC cycle that will call all the finalizers.
                // This is important because the finalizer thread will detect tasks with unobserved exceptions.
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
            finally
            {
                // It is important to unsubscribe from the global event to prevent a memory leak when a test instance will stay
                // in memory indefinitely.
                TaskScheduler.UnobservedTaskException -= taskSchedulerOnUnobservedTaskException;
            }

            // This test is not 100% bullet proof, and it is possible that the test will pass even when the issue is still in the code.
            // But the original issue was very consistent and the test was failing even from the IDE in Debug mode all the time.
            numberOfUnobservedExceptions.Should().Be(0);
        }
 /// <nodoc />
 public BandwidthCheckedCopier(IAbsolutePathRemoteFileCopier inner, BandwidthChecker.Configuration config)
 {
     _inner   = inner;
     _checker = new BandwidthChecker(config);
 }
 /// <nodoc />
 public BandwidthCheckedCopier(IFileCopier <T> inner, BandwidthChecker.Configuration config, ILogger logger)
 {
     _inner   = inner;
     _checker = new BandwidthChecker(config);
     _logger  = logger;
 }