public void Writing_Whole_File_In_Streamed_Blocks_Should_Create_Exact_Copy()
        {
            //use an output stream which works with buffers
            StreamedBlockOutputStream os = new StreamedBlockOutputStream(Token, 15000, b => UploadHandler.WriteBlockStreamed(b));
            MemoryStream ms = new MemoryStream(SourceFileContents);

            ms.WriteTo(os);

            UploadHandler.CompleteTransfer(Token.TransferId);

            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFilePath, TargetFilePath);


            TargetFile.Delete();
            InitToken();

            //use a source strem
            using (var fs = SourceFile.OpenRead())
            {
                fs.WriteTo(Token, SourceFile.Length, 10000, b => UploadHandler.WriteBlockStreamed(b));
                UploadHandler.CompleteTransfer(Token.TransferId);

                TargetFile.Refresh();
                FileAssert.AreEqual(SourceFilePath, TargetFilePath);
            }
        }
        public void Submitting_Blocks_Should_Update_Transferred_Block_Table()
        {
            var blocks = new Dictionary <long, BufferedDataBlock>();

            Random r    = new Random();
            var    list = CreateBufferedBlocks();

            var count = list.Count;

            for (int i = 0; i < count; i++)
            {
                int index = r.Next(list.Count);
                var block = list[index];
                blocks.Add(block.BlockNumber, block);

                UploadHandler.WriteBlock(list[index]);
                list.RemoveAt(index);

                //get transmission table and compare
                var transferredBlocks = UploadHandler.GetTransferredBlocks(Token.TransferId);
                Assert.AreEqual(blocks.Count, transferredBlocks.Count());
                transferredBlocks.Do(b =>
                {
                    Assert.AreEqual(blocks[b.BlockNumber].Offset, b.Offset);
                    Assert.AreEqual(blocks[b.BlockNumber].BlockLength, b.BlockLength);
                });
            }

            UploadHandler.CompleteTransfer(Token.TransferId);
        }
        public void Transfer_Should_Be_Denied_If_Download_Is_Already_Running()
        {
            //create file
            var blocks = CreateBufferedBlocks();

            blocks.Do(UploadHandler.WriteBlock);
            UploadHandler.CompleteTransfer(Token.TransferId);

            //request download
            var downloadToken = provider.DownloadTransfers.RequestDownloadToken(TargetFilePath, false);

            try
            {
                Token = UploadHandler.RequestUploadToken(TargetFilePath, true, 5000, "");
                Assert.Fail("Got upload token while download is running.");
            }
            catch (ResourceLockedException expected)
            {
            }
            finally
            {
                provider.DownloadTransfers.CancelTransfer(downloadToken.TransferId, AbortReason.ClientAbort);
            }

            //after downloading, it should work
            Token = UploadHandler.RequestUploadToken(TargetFilePath, true, 5000, "");
        }
        public void Writing_Streamed_Blocks_Should_Create_Clean_Copy()
        {
            using (var fs = SourceFile.OpenRead())
            {
                fs.WriteTo(Token, SourceFile.Length, Token.MaxBlockSize.Value, UploadHandler.WriteBlockStreamed);
            }

            UploadHandler.CompleteTransfer(Token.TransferId);

            FileAssert.AreEqual(SourceFile, TargetFile);
        }
        public void Writing_All_Blocks_Should_Create_Clean_Copy()
        {
            foreach (var block in CreateBufferedBlocks())
            {
                UploadHandler.WriteBlock(block);
            }

            TransferStatus status = UploadHandler.CompleteTransfer(Token.TransferId);

            FileAssert.AreEqual(SourceFile, TargetFile);
            Assert.AreEqual(TransferStatus.Completed, status);
        }
        public void Writing_Whole_File_In_Buffered_Blocks_Should_Create_Exact_Copy()
        {
            BufferedBlockOutputStream os = new BufferedBlockOutputStream(Token, 15000, b => UploadHandler.WriteBlock(b));
            MemoryStream ms = new MemoryStream(SourceFileContents);

            ms.WriteTo(os);

            UploadHandler.CompleteTransfer(Token.TransferId);

            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFilePath, TargetFilePath);
        }
Пример #7
0
        public void Pausing_With_Parallel_Block_Read_In_Progress_Should_Work()
        {
            var       blocks    = CreateBufferedBlocks();
            bool      isRunning = true;
            Exception exception = null;

            ThreadPool.QueueUserWorkItem(cb =>
            {
                while (isRunning)
                {
                    Thread.Sleep(50);
                    try
                    {
                        if (UploadHandler.GetTransferStatus(Token.TransferId) == TransferStatus.Running)
                        {
                            PauseAndVerifyStatusIsPaused();
                        }
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
            });


            ThreadPool.QueueUserWorkItem(cb =>
            {
                try
                {
                    blocks.Do(b => UploadHandler.WriteBlock(b));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Assert.Fail("Got an exception while reading block.");
                }
            });

            //both actions run on background threads - make a break
            Thread.CurrentThread.Join(3000);

            UploadHandler.CompleteTransfer(Token.TransferId);
            Assert.IsNull(exception, exception == null ? "" : exception.Message);

            FileAssert.AreEqual(SourceFile, TargetFile);
        }
Пример #8
0
        public void Pausing_And_Resuming_Transfer_Should_Update_Reported_Transfer_Status()
        {
            List <BufferedDataBlock> blocks = CreateBufferedBlocks();

            foreach (var block in blocks)
            {
                UploadHandler.PauseTransfer(Token.TransferId);
                Assert.AreEqual(TransferStatus.Paused, UploadHandler.GetTransferStatus(Token.TransferId));

                UploadHandler.WriteBlock(block);

                Assert.AreEqual(TransferStatus.Running, UploadHandler.GetTransferStatus(Token.TransferId));
            }

            //complete and recompare
            UploadHandler.CompleteTransfer(Token.TransferId);
            FileAssert.AreEqual(SourceFile, TargetFile);
        }
        public void Retransmitted_Blocks_Should_Replace_Entries_In_Transferred_Block_Table()
        {
            var block = CreateBufferedBlocks()[10];

            //modify data in one of the blocks
            int blockLength = block.BlockLength.Value / 2;

            block.BlockLength = blockLength;
            block.Data        = block.Data.CreateCopy(blockLength);

            //write block
            UploadHandler.WriteBlock(block);

            //check block
            var blocks = UploadHandler.GetTransferredBlocks(Token.TransferId);

            Assert.AreEqual(1, blocks.Count());
            Assert.AreEqual(blockLength, blocks.Single().BlockLength);

            UploadHandler.PauseTransfer(Token.TransferId);
            TargetFile.Refresh();
            Assert.AreEqual(block.Offset + blockLength, TargetFile.Length);

            //get unmodified block and write that one
            block = CreateBufferedBlocks()[10];
            UploadHandler.WriteBlock(block);

            blocks = UploadHandler.GetTransferredBlocks(Token.TransferId);
            Assert.AreEqual(1, blocks.Count());
            Assert.AreNotEqual(blockLength, blocks.Single().BlockLength);
            Assert.AreEqual(block.BlockLength, blocks.Single().BlockLength);

            UploadHandler.PauseTransfer(Token.TransferId);
            TargetFile.Refresh();
            Assert.AreEqual(block.Offset + block.BlockLength, TargetFile.Length);

            UploadHandler.CompleteTransfer(Token.TransferId);
        }
        public void Unordered_Writing_Should_Be_Supported()
        {
            var blocks = new Dictionary <long, BufferedDataBlock>();

            Random r    = new Random();
            var    list = CreateBufferedBlocks();

            var count = list.Count;

            for (int i = 0; i < count; i++)
            {
                int index = r.Next(list.Count);
                var block = list[index];
                blocks.Add(block.BlockNumber, block);

                UploadHandler.WriteBlock(list[index]);
                list.RemoveAt(index);
            }

            UploadHandler.CompleteTransfer(Token.TransferId);
            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFile, TargetFile);
        }
        public void Retransmitted_Blocks_Should_Overwrite_Previously_Transferred_Data()
        {
            List <BufferedDataBlock> blocks = CreateBufferedBlocks();

            //modify data in one of the blocks
            Array.Reverse(blocks[10].Data);

            foreach (var block in blocks)
            {
                UploadHandler.WriteBlock(block);
            }

            //pause in order to release stream
            UploadHandler.PauseTransfer(Token.TransferId);
            FileAssert.AreNotEqual(SourceFile, TargetFile);

            //write correct block data
            blocks = CreateBufferedBlocks();
            UploadHandler.WriteBlock(blocks[10]);

            //complete and recompare
            UploadHandler.CompleteTransfer(Token.TransferId);
            FileAssert.AreEqual(SourceFile, TargetFile);
        }