public void NormalDownload() { double lastPercent = 0; this.transmission.AddPositionConstraint(Is.LessThanOrEqualTo(this.remoteLength)); this.transmission.AddLengthConstraint(Is.EqualTo(this.remoteLength).Or.EqualTo(0)); this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var t = sender as Transmission; if (e.PropertyName == Utils.NameOf(() => t.Percent)) { Assert.That(t.Percent, Is.Null.Or.GreaterThanOrEqualTo(lastPercent)); lastPercent = t.Percent.GetValueOrDefault(); } }; using (IFileDownloader downloader = new SimpleFileDownloader()) { downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg); Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length); Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash); Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash); } }
public void AbortWhileDownload() { this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Callback(() => Thread.Sleep(1)).Returns(1); this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { Assert.That((sender as Transmission).Status, Is.Not.EqualTo(TransmissionStatus.FINISHED)); }; try { Task t; IFileDownloader downloader = new SimpleFileDownloader(); t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg)); t.Wait(100); this.transmission.Abort(); t.Wait(); Assert.Fail(); } catch (AggregateException e) { Assert.IsInstanceOf(typeof(AbortException), e.InnerException); Assert.That(this.transmission.Status, Is.EqualTo(TransmissionStatus.ABORTED)); return; } Assert.Fail(); }
public void IOExceptionThrownIfIOExceptionOccursOnRead() { this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Throws<IOException>(); using (IFileDownloader downloader = new SimpleFileDownloader()) { Assert.Throws<IOException>(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg)); } }
public void DisposeWhileDownload() { this.mockedMemStream.Setup(memstream => memstream.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Callback(() => Thread.Sleep(1)).Returns(1); try { Task t; using (IFileDownloader downloader = new SimpleFileDownloader()) { t = Task.Factory.StartNew(() => downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg)); } t.Wait(); Assert.Fail(); } catch (AggregateException e) { Assert.IsInstanceOf(typeof(ObjectDisposedException), e.InnerException); } }
public void DownloadWithThreeUpdate() { SetUp(2 * 1024 * 1024 + 1); this.hashAlg = new SHA1Reuse(); using (IFileDownloader downloader = new SimpleFileDownloader()) { int count = 0; downloader.DownloadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg, (byte[] checksum, long length) => { ++count; }); Assert.AreEqual(3, count); Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length); Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash); Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash); } }