Bandwidth limited stream.
Inheritance: NotifyPropertyChangedStream
 public void ConstructorWithoutLimits() {
     using (var memory = new MemoryStream(this.buffer))
     using (var underTest = new BandwidthLimitedStream(memory)) {
         Assert.That(underTest.ReadLimit, Is.Null);
         Assert.That(underTest.WriteLimit, Is.Null);
     }
 }
        public void ReadOrWriteOneByteMoreThanLimitTakesAtMinimumOneSecond([Values(true, false)]bool read) {
            byte[] buf = new byte[this.length];
            using (var memstream = new MemoryStream(this.buffer))
            using (var underTest = new BandwidthLimitedStream(memstream, limit: this.limit)) {
                var start = DateTime.Now;
                if (read) {
                    underTest.Read(buf, 0, (int)this.limit + 1);
                } else {
                    underTest.Write(buf, 0, (int)this.limit + 1);
                }

                TimeSpan duration = DateTime.Now - start;
                Assert.That(duration.TotalMilliseconds, Is.AtLeast(1000));
            }
        }
 public void SetAndGetReadAndWriteLimits([Values(true, false)]bool limitRead, [Values(true, false)]bool limitWrite) {
     using (var memory = new MemoryStream(this.buffer))
     using (var underTest = new BandwidthLimitedStream(memory)) {
         Assert.That(underTest.ReadLimit, Is.Null);
         Assert.That(underTest.WriteLimit, Is.Null);
         underTest.ReadLimit = limitRead ? this.limit : (long?)null;
         Assert.That(underTest.ReadLimit, Is.EqualTo(limitRead ? this.limit : (long?)null));
         Assert.That(underTest.WriteLimit, Is.Null);
         underTest.WriteLimit = limitWrite ? this.limit : (long?)null;
         Assert.That(underTest.ReadLimit, Is.EqualTo(limitRead ? this.limit : (long?)null));
         Assert.That(underTest.WriteLimit, Is.EqualTo(limitWrite ? this.limit : (long?)null));
         underTest.DisableReadLimit();
         Assert.That(underTest.ReadLimit, Is.Null);
         Assert.That(underTest.WriteLimit, Is.EqualTo(limitWrite ? this.limit : (long?)null));
         underTest.DisableWriteLimit();
         Assert.That(underTest.ReadLimit, Is.Null);
         Assert.That(underTest.WriteLimit, Is.Null);
     }
 }
        public void ChangingLimitsNotifiesListener() {
            using (var memory = new MemoryStream(this.buffer))
            using (var underTest = new BandwidthLimitedStream(memory)) {
                int readLimitNotified = 0;
                int writeLimitNotified = 0;
                long? expectedReadLimit = this.limit;
                long? expectedWriteLimit = this.limit;
                underTest.PropertyChanged += (sender, e) => {
                    Assert.That(sender, Is.EqualTo(underTest));
                    if (e.PropertyName == Utils.NameOf((BandwidthLimitedStream s) => s.ReadLimit)) {
                        readLimitNotified++;
                        Assert.That((sender as BandwidthLimitedStream).ReadLimit, Is.EqualTo(expectedReadLimit));
                    } else if (e.PropertyName == Utils.NameOf((BandwidthLimitedStream s) => s.WriteLimit)) {
                        writeLimitNotified++;
                        Assert.That((sender as BandwidthLimitedStream).WriteLimit, Is.EqualTo(expectedWriteLimit));
                    }
                };
                underTest.WriteLimit = expectedWriteLimit;
                underTest.ReadLimit = expectedReadLimit;

                Assert.That(readLimitNotified, Is.EqualTo(1));
                Assert.That(writeLimitNotified, Is.EqualTo(1));

                expectedReadLimit = null;
                expectedWriteLimit = null;

                underTest.DisableLimits();

                Assert.That(readLimitNotified, Is.EqualTo(2));
                Assert.That(writeLimitNotified, Is.EqualTo(2));
            }
        }