public void Create_ThrowsIfCancelled()
 {
     using (var test = new AutomaticProgressReporterTest())
     {
         Assert.Throws <OperationCanceledException>(
             () => AutomaticProgressReporter.Create(
                 test.Connection.Object,
                 test.Request,
                 test.Interval,
                 new CancellationToken(canceled: true)));
     }
 }
        public void Create_ThrowsForNullRequest()
        {
            using (var test = new AutomaticProgressReporterTest())
            {
                var exception = Assert.Throws <ArgumentNullException>(
                    () => AutomaticProgressReporter.Create(
                        test.Connection.Object,
                        request: null,
                        interval: test.Interval,
                        cancellationToken: CancellationToken.None));

                Assert.Equal("request", exception.ParamName);
            }
        }
        private static void VerifyInvalidInterval(TimeSpan interval)
        {
            using (var test = new AutomaticProgressReporterTest())
            {
                var exception = Assert.Throws <ArgumentOutOfRangeException>(
                    () => AutomaticProgressReporter.Create(
                        test.Connection.Object,
                        test.Request,
                        interval,
                        CancellationToken.None));

                Assert.Equal("interval", exception.ParamName);
            }
        }
        public void Dispose_DoesNotDisposeConnection()
        {
            var connection = new Mock <IConnection>(MockBehavior.Strict);
            var request    = MessageUtilities.Create(
                requestId: "a",
                type: MessageType.Request,
                method: MessageMethod.GetServiceIndex,
                payload: new GetServiceIndexRequest(packageSourceRepository: "https://unit.test"));

            using (var reporter = AutomaticProgressReporter.Create(
                       connection.Object,
                       request,
                       TimeSpan.FromHours(1),
                       CancellationToken.None))
            {
            }

            connection.Verify();
        }
            internal AutomaticProgressReporterTest(TimeSpan?interval = null, int expectedSentCount = 0)
            {
                _expectedSentCount = expectedSentCount;

                var payload = new HandshakeRequest(
                    ProtocolConstants.CurrentVersion,
                    ProtocolConstants.CurrentVersion);

                CancellationTokenSource = new CancellationTokenSource();
                Interval  = interval.HasValue ? interval.Value : ProtocolConstants.MaxTimeout;
                SentEvent = new ManualResetEventSlim(initialState: false);

                Connection = new Mock <IConnection>(MockBehavior.Strict);

                Connection.Setup(x => x.SendAsync(
                                     It.IsNotNull <Message>(),
                                     It.IsAny <CancellationToken>()))
                .Callback <Message, CancellationToken>(
                    (message, cancellationToken) =>
                {
                    ++_actualSentCount;

                    if (_actualSentCount >= expectedSentCount)
                    {
                        SentEvent.Set();
                    }
                })
                .Returns(Task.FromResult(0));

                Request = MessageUtilities.Create(
                    requestId: "a",
                    type: MessageType.Request,
                    method: MessageMethod.Handshake,
                    payload: payload);
                Reporter = AutomaticProgressReporter.Create(
                    Connection.Object,
                    Request,
                    Interval,
                    CancellationTokenSource.Token);
            }
 protected override AutomaticProgressReporter GetProgressReporter(IConnection connection, Message message, CancellationToken cancellationToken)
 {
     Logger.Verbose(string.Format(Resources.CreatingProgressReporter, progressReporterTimeSpan.ToString()));
     return(AutomaticProgressReporter.Create(connection, message, progressReporterTimeSpan, cancellationToken));
 }