Progress reporting stream.
Наследование: NotifyPropertyChangedStream
Пример #1
0
 public void SetLength() {
     var mockedStream = new Mock<Stream>();
     mockedStream.Setup(s => s.SetLength(It.IsAny<long>())).Callback<long>((l) => mockedStream.Setup(mock => mock.Length).Returns(l));
     using (ProgressStream progress = new ProgressStream(mockedStream.Object)) {
         progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs args) {
             if (args.PropertyName == Utils.NameOf((ProgressStream s) => s.Length)) {
                 this.lengthCalls++;
             }
         };
         progress.SetLength(100);
         progress.SetLength(100);
         Assert.AreEqual(1, this.lengthCalls);
         Assert.That(progress.Length, Is.EqualTo(100));
     }
 }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission) {
            if (transmission == null) {
                throw new ArgumentNullException("transmission");
            }

            this.abort = new AbortableStream(wrappedStream);
            this.pause = new PausableStream(this.abort);
            this.bandwidthNotify = new BandwidthNotifyingStream(this.pause);
            this.progress = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception)) {
                    transmission.Status = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond)) {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                    transmission.Position = p.Position;
                } else if (e.PropertyName == Utils.NameOf(() => p.Length)) {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status)) {
                    if (t.Status == TransmissionStatus.ABORTING) {
                        this.abort.Abort();
                        this.pause.Resume();
                    } else if (t.Status == TransmissionStatus.PAUSED) {
                        this.pause.Pause();
                    } else if (t.Status == TransmissionStatus.TRANSMITTING) {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED) {
                this.abort.Abort();
            }
        }
Пример #3
0
        public void Position() {
            var mockedStream = new Mock<Stream>();
            mockedStream.Setup(s => s.SetLength(It.IsAny<long>()));
            mockedStream.SetupProperty(s => s.Position);
            using (var underTest = new ProgressStream(mockedStream.Object)) {
                underTest.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                    var p = sender as ProgressStream;
                    if (e.PropertyName == Utils.NameOf(() => p.Length)) {
                        this.lengthCalls++;
                    }

                    if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                        this.positionCalls++;
                    }
                };
                underTest.SetLength(100);
                Assert.AreEqual(1, this.lengthCalls);
                Assert.AreEqual(0, this.positionCalls);
                underTest.Position = 50;
                underTest.Position = 50;
                Assert.AreEqual(1, this.positionCalls);
                underTest.Position = 55;
                Assert.AreEqual(2, this.positionCalls);
                Assert.AreEqual(1, this.lengthCalls);
            }
        }
Пример #4
0
 public void ConstructorExtractsLengthOfGivenStream() {
     long length = 10;
     using (var underTest = new ProgressStream(Mock.Of<Stream>(s => s.Length == length))) {
         Assert.That(underTest.Length, Is.EqualTo(length));
     }
 }
Пример #5
0
 public void UpdateLengthIfInputStreamGrowsAfterStartReading() {
     using (var stream = new MemoryStream()) {
         long initialLength = 100;
         long length = initialLength;
         byte[] buffer = new byte[initialLength];
         stream.Write(buffer, 0, buffer.Length);
         using (var progress = new ProgressStream(stream)) {
             progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                 var t = sender as ProgressStream;
                 if (e.PropertyName == Utils.NameOf(() => t.Position)) {
                     Assert.That(t.Position, Is.LessThanOrEqualTo(length));
                     Assert.That(t.Length, Is.LessThanOrEqualTo(length));
                 }
             };
             progress.Read(buffer, 0, buffer.Length / 2);
             stream.Write(buffer, 0, buffer.Length);
             length = length + buffer.Length;
             progress.Read(buffer, 0, buffer.Length / 2);
             progress.Read(buffer, 0, buffer.Length / 2);
             progress.Read(buffer, 0, buffer.Length / 2);
             stream.Write(buffer, 0, buffer.Length);
             length = length + buffer.Length;
             progress.Read(buffer, 0, buffer.Length);
         }
     }
 }
Пример #6
0
 public void ResumeTest() {
     byte[] inputContent = new byte[100];
     long offset = 100;
     using (var stream = new MemoryStream(inputContent)) 
     using (var offsetstream = new OffsetStream(stream, offset)) {
         using (var progress = new ProgressStream(offsetstream)) {
             progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                 var p = sender as ProgressStream;
                 if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                     this.position = (long)p.Position;
                     this.percent = p.Percent.GetValueOrDefault();
                 }
             };
             progress.Seek(0, SeekOrigin.Begin);
             Assert.AreEqual(offset, this.position);
             Assert.AreEqual(50, this.percent);
             progress.Seek(10, SeekOrigin.Current);
             Assert.AreEqual(offset + 10, this.position);
             progress.Seek(0, SeekOrigin.End);
             Assert.AreEqual(100, this.percent);
             Assert.AreEqual(offset + inputContent.Length, this.position);
             progress.Seek(0, SeekOrigin.Begin);
             progress.WriteByte(0);
             Assert.AreEqual(offset + 1, this.position);
             Assert.AreEqual(50.5, this.percent);
             progress.WriteByte(0);
             Assert.AreEqual(offset + 2, this.position);
             Assert.AreEqual(51, this.percent);
             progress.Write(new byte[10], 0, 10);
             Assert.AreEqual(56, this.percent);
         }
     }
 }
Пример #7
0
        public void SeekTest() {
            using (var stream = new MemoryStream()) {
                using (var progress = new ProgressStream(stream)) {
                    progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                        var p = sender as ProgressStream;
                        if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                            this.positionCalls++;
                            this.position = (long)p.Position;
                            this.percent = (double)p.Percent;
                        }
                    };
                    progress.SetLength(100);
                    progress.Seek(10, SeekOrigin.Begin);
                    Assert.AreEqual(10, this.position);
                    Assert.AreEqual(10, this.percent);
                    progress.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(20, this.position);
                    Assert.AreEqual(20, this.percent);
                    progress.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(30, this.position);
                    Assert.AreEqual(30, this.percent);
                    progress.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(40, this.position);
                    Assert.AreEqual(40, this.percent);
                    progress.Seek(5, SeekOrigin.Current);
                    Assert.AreEqual(45, this.position);
                    Assert.AreEqual(45, this.percent);
                    progress.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(55, this.position);
                    Assert.AreEqual(55, this.percent);
                    progress.SetLength(1000);
                    progress.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(65, this.position);
                    Assert.AreEqual(6.5, this.percent);

                    progress.Seek(0, SeekOrigin.End);
                    Assert.AreEqual(100, this.percent);
                    Assert.AreEqual(1000, this.position);
                }
            }
        }
Пример #8
0
 public void Write() {
     using (var stream = new MemoryStream()) {
         byte[] buffer = new byte[10];
         using (var progress = new ProgressStream(stream)) {
             progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                 var t = sender as ProgressStream;
                 if (e.PropertyName == Utils.NameOf(() => t.Position)) {
                     this.positionCalls++;
                     this.position = (long)t.Position;
                     this.percent = (double)t.Percent.GetValueOrDefault();
                 }
             };
             progress.SetLength(buffer.Length * 10);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual(buffer.Length, this.position);
             Assert.AreEqual(10, this.percent);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual(buffer.Length * 2, this.position);
             Assert.AreEqual(20, this.percent);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual(buffer.Length * 3, this.position);
             Assert.AreEqual(30, this.percent);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual(buffer.Length * 4, this.position);
             Assert.AreEqual(40, this.percent);
             progress.Write(buffer, 0, buffer.Length / 2);
             Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position);
             Assert.AreEqual(45, this.percent);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual((buffer.Length * 5) + (buffer.Length / 2), this.position);
             Assert.AreEqual(55, this.percent);
             progress.SetLength(buffer.Length * 100);
             progress.Write(buffer, 0, buffer.Length);
             Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position);
             Assert.AreEqual(6.5, this.percent);
         }
     }
 }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission)
        {
            if (transmission == null)
            {
                throw new ArgumentNullException("transmission");
            }

            this.abort                  = new AbortableStream(wrappedStream);
            this.pause                  = new PausableStream(this.abort);
            this.bandwidthNotify        = new BandwidthNotifyingStream(this.pause);
            this.progress               = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception))
                {
                    transmission.Status          = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond))
                {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position))
                {
                    transmission.Position = p.Position;
                }
                else if (e.PropertyName == Utils.NameOf(() => p.Length))
                {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status))
                {
                    if (t.Status == TransmissionStatus.ABORTING)
                    {
                        this.abort.Abort();
                        this.pause.Resume();
                    }
                    else if (t.Status == TransmissionStatus.PAUSED)
                    {
                        this.pause.Pause();
                    }
                    else if (t.Status == TransmissionStatus.TRANSMITTING)
                    {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED)
            {
                this.abort.Abort();
            }
        }