Simple file uploader. Takes a given stream and uploads it to the server. Resuming an Upload is not supported.
Inheritance: IFileUploader
Beispiel #1
0
        public void UploadWhileAnotherProcessIsWritingToFile() {
            var fileName = "slowFile.txt";
            var chunkSize = 1024;
            var chunks = 100;
            byte[] chunk = new byte[chunkSize];
            var finalLength = chunks * chunkSize;
            var file = new FileInfo(Path.Combine(this.LocalTestDir.FullName, fileName));
            var mockedDocument = new Mock<IDocument>();
            var transmissionStatus = new Transmission(TransmissionType.UPLOAD_NEW_FILE, fileName);
            mockedDocument.Setup(doc => doc.Name).Returns(fileName);
            using (var remoteStream = new MemoryStream()) {
                mockedDocument.Setup(doc => doc.SetContentStream(It.IsAny<IContentStream>(), It.Is<bool>(b => b == true), It.Is<bool>(b => b == true)))
                    .Callback<IContentStream, bool, bool>((s, b, r) => s.Stream.CopyTo(remoteStream))
                        .Returns(new Mock<IObjectId>().Object);
                using (var fileStream = file.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Read)) {
                    using (var task = Task.Factory.StartNew(() => {
                        var newFileHandle = new FileInfo(file.FullName);
                        using (var hashAlg = new SHA1Managed())
                        using (var readingFileStream = newFileHandle.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (var uploader = new SimpleFileUploader()) {
                            uploader.UploadFile(mockedDocument.Object, readingFileStream, transmissionStatus, hashAlg);
                        }

                        Assert.That(remoteStream.Length, Is.EqualTo(finalLength));
                    })) {
                        for (int i = 0; i < chunks; i++) {
                            Thread.Sleep(10);
                            fileStream.Write(chunk, 0, chunkSize);
                        }

                        task.Wait();
                    }
                }
            }
        }
        public void AbortTest() {
            this.mockedDocument.Setup(doc => doc.SetContentStream(It.IsAny<IContentStream>(), It.Is<bool>(b => b == true), It.Is<bool>(b => b == true)))
                .Callback<IContentStream, bool, bool>((s, b, r) => s.Stream.CopyTo(this.mockedMemStream.Object))
                .Returns(new Mock<IObjectId>().Object);
            this.mockedMemStream.Setup(memstream => memstream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Callback(() => Thread.Sleep(100));
            this.transmission.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
                Assert.That((sender as Transmission).Status, Is.Not.EqualTo(TransmissionStatus.FINISHED));
            };
            try {
                Task t;
                IFileUploader uploader = new SimpleFileUploader();
                t = Task.Factory.StartNew(() => uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg));
                t.Wait(10);
                this.transmission.Abort();
                t.Wait();
                Assert.Fail();
            } catch (AggregateException e) {
                Assert.IsInstanceOf(typeof(UploadFailedException), e.InnerException);
                Assert.IsInstanceOf(typeof(AbortException), e.InnerException.InnerException);
                Assert.That(this.transmission.Status, Is.EqualTo(TransmissionStatus.ABORTED));
                return;
            }

            Assert.Fail();
        }
        public void NormalUploadTest() {
            this.mockedDocument.Setup(doc => doc.SetContentStream(It.IsAny<IContentStream>(), It.Is<bool>(b => b == true), It.Is<bool>(b => b == true)))
                .Callback<IContentStream, bool, bool>((s, b, r) => s.Stream.CopyTo(this.mockedMemStream.Object))
                .Returns(new Mock<IObjectId>().Object);
            using (IFileUploader uploader = new SimpleFileUploader()) {
                transmission.AddLengthConstraint(Is.EqualTo(0).Or.EqualTo(this.localContent.Length));
                transmission.AddPositionConstraint(Is.LessThanOrEqualTo(this.localContent.Length));

                IDocument result = uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);

                Assert.AreEqual(result, this.mockedDocument.Object);
                Assert.AreEqual(this.localContent.Length, this.mockedMemStream.Object.Length);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localContent), this.hashAlg.Hash);
                this.mockedMemStream.Object.Seek(0, SeekOrigin.Begin);
                Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.mockedMemStream.Object), this.hashAlg.Hash);
            }
        }
 public void IOExceptionTest() {
     this.mockedDocument.Setup(doc => doc.SetContentStream(It.IsAny<IContentStream>(), It.IsAny<bool>(), It.Is<bool>(b => b == true)))
         .Throws<IOException>();
     using (IFileUploader uploader = new SimpleFileUploader()) {
         try {
             uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);
             Assert.Fail();
         } catch (UploadFailedException e) {
             Assert.IsInstanceOf(typeof(IOException), e.InnerException);
             Assert.AreEqual(this.mockedDocument.Object, e.LastSuccessfulDocument);
         }
     }
 }