public void Pausing_With_Parallel_Block_Read_In_Progress_Should_Work() { ThreadPool.QueueUserWorkItem(cb => { Thread.Sleep(200); try { PauseAndVerifyStatusIsPaused(); } catch (Exception e) { Assert.Fail(e.Message); } }); ThreadPool.QueueUserWorkItem(cb => { try { Downloads.ReadBlock(Token.TransferId, 1); } catch (Exception e) { Console.WriteLine(e); Assert.Fail("Got an exception while reading block."); } }); Thread.Sleep(3000); }
public void If_File_Size_Is_Not_Multitude_Of_Block_Size_Then_Last_Block_Should_Be_Smaller_Then_Others() { var maxSize = Downloads.MaxBlockSize ?? 2048; //upload a file that consists of a total of 5.5 (6) blocks File.WriteAllBytes(Context.DownloadFile0Template.FullName, new byte[maxSize * 5 + maxSize / 2]); Context.DownloadFile0Template.Refresh(); SourceFile = Context.DownloadFolder.AddFile(Context.DownloadFile0Template.FullName, "multiple.bin", false); Downloads.CancelTransfer(Token.TransferId, AbortReason.ClientAbort); Token = Downloads.RequestDownloadToken(SourceFileInfo.FullName, false, maxSize); Assert.AreEqual(maxSize, Token.DownloadBlockSize); Assert.AreEqual(6, Token.TotalBlockCount); List <BufferedDataBlock> blockList = new List <BufferedDataBlock>(); int blockNumber = 0; while (true) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); blockList.Add(block); if (block.IsLastBlock) { break; } } Assert.AreEqual(6, blockList.Count); Assert.AreEqual(maxSize / 2, blockList[5].BlockLength); }
public void Request_Should_Throw_Expiration_Exception() { //expire and wait for the scheduler to readjust SystemTime.Now = () => Token.ExpirationTime.Value.AddSeconds(1); Thread.CurrentThread.Join(500); Downloads.ReadBlock(Token.TransferId, 0); }
public void Requesting_First_Block_Multiple_Times_Should_Work() { var block1 = Downloads.ReadBlock(Token.TransferId, 0); var block2 = Downloads.ReadBlock(Token.TransferId, 0); Assert.AreEqual(block1.BlockNumber, block2.BlockNumber); Assert.AreEqual(block1.BlockLength, block2.BlockLength); Assert.AreNotEqual(0, block1.Data.Length); CollectionAssert.AreEqual(block1.Data, block2.Data); }
public void Pausing_Should_Be_Possible_After_Having_Read_Last_Block() { CompleteReading(); PauseAndVerifyStatusIsPaused(); //read last block again var copy = Downloads.ReadBlock(Token.TransferId, recentBlock.BlockNumber); CollectionAssert.AreEqual(recentBlock.Data, copy.Data); }
public void Resuming_Reading_Should_Switch_Status_Back_To_Running() { Downloads.ReadBlock(Token.TransferId, 0); PauseAndVerifyStatusIsPaused(); //read next block Downloads.ReadBlock(Token.TransferId, 1); var status = Downloads.GetTransferStatus(Token.TransferId); Assert.AreEqual(TransferStatus.Running, status); }
private void CompleteReading() { while (recentBlock == null || recentBlock.IsLastBlock == false) { long nextBlock = recentBlock == null ? 0 : recentBlock.BlockNumber + 1; var block = Downloads.ReadBlock(Token.TransferId, nextBlock); ReceiveBlock(block); } //make sure we have an exact copy FileAssert.AreEqual(Context.DownloadFile0Template.FullName, TargetFilePath); }
public void Requesting_Random_Block_Numbers_Should_Work() { for (int i = 0; i < 10; i++) { Downloads.ReadBlock(Token.TransferId, i); } var block20 = Downloads.ReadBlock(Token.TransferId, 20); var block4 = Downloads.ReadBlock(Token.TransferId, 4); var block20Copy = Downloads.ReadBlock(Token.TransferId, 20); Assert.AreEqual(block20.Data, block20Copy.Data); }
public void Number_Of_Available_Blocks_Should_Match_Token() { long blockNumber = 0; //request data using block numbers according to token info for (int i = 0; i < Token.TotalBlockCount; i++) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); ReceiveBlock(block); } //make sure we got all the data FileAssert.AreEqual(Context.DownloadFile0Template.FullName, TargetFilePath); }
public void Requesting_Block_Size_With_Specified_Value_Should_Be_Allowed_If_Service_Does_Not_Have_Limit() { if (Downloads.MaxBlockSize.HasValue) { Assert.Inconclusive("Download service has a maximum block size - did not run test."); } var blockSize = 2048; Token = Downloads.RequestDownloadToken(SourceFileInfo.FullName, false, blockSize); Assert.AreEqual(blockSize, Token.DownloadBlockSize); BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, 0); Assert.AreEqual(blockSize, block.BlockLength); Assert.AreEqual(blockSize, block.Data.Length); }
public void Requesting_Token_With_Block_Size_Below_Maximum_Should_Return_Small_Blocks() { if (!Downloads.MaxBlockSize.HasValue) { Assert.Inconclusive("Download service does not provide maximum block size"); } var blockSize = Downloads.MaxBlockSize.Value / 2; Token = Downloads.RequestDownloadToken(SourceFileInfo.FullName, false, blockSize); Assert.AreEqual(blockSize, Token.DownloadBlockSize); BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, 0); Assert.AreEqual(blockSize, block.BlockLength); Assert.AreEqual(blockSize, block.Data.Length); }
public void Requesting_New_Block_Should_Fail_Once_The_Token_Expired() { try { Downloads.ReadBlock(Token.TransferId, 0); } catch { Assert.Fail("Reading first block should have worked."); } //expire and wait for the scheduler to readjust SystemTime.Now = () => Token.ExpirationTime.Value.AddSeconds(1); Thread.CurrentThread.Join(500); Downloads.ReadBlock(Token.TransferId, 1); }
public void Requesting_Token_With_Block_Size_Above_Max_Size_Should_Fall_Back_To_Maxium() { if (!Downloads.MaxBlockSize.HasValue) { Assert.Inconclusive("Download service does not provide maximum block size"); } var expectedSize = Downloads.MaxBlockSize.Value; var blockSize = expectedSize + 10000; Token = Downloads.RequestDownloadToken(SourceFileInfo.FullName, false, blockSize); Assert.AreEqual(expectedSize, Token.DownloadBlockSize); BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, 0); Assert.AreEqual(expectedSize, block.BlockLength); Assert.AreEqual(expectedSize, block.Data.Length); }
public void Reading_Blocks_Should_Deliver_All_Data() { long blockNumber = 0; //read blocks until last block is indicated while (true) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); ReceiveBlock(block); if (block.IsLastBlock) { break; } } //make sure we got all the data FileAssert.AreEqual(Context.DownloadFile0Template.FullName, TargetFilePath); }
public void All_But_Last_Block_Should_Indicate_There_Is_More_Data() { List <BufferedDataBlock> blockList = new List <BufferedDataBlock>(); int blockNumber = 0; while (true) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); blockList.Add(block); if (block.IsLastBlock) { break; } } Assert.Greater(blockList.Count, 1); Assert.IsTrue(blockList.Last().IsLastBlock); blockList.RemoveAt(blockList.Count - 1); blockList.ForEach(b => Assert.IsFalse(b.IsLastBlock)); }
public void Requesting_Last_Block_Multiple_Times_Should_Work() { long blockNumber = 0; BufferedDataBlock block; while (true) { block = Downloads.ReadBlock(Token.TransferId, blockNumber++); if (block.IsLastBlock) { break; } } Assert.IsTrue(block.IsLastBlock); BufferedDataBlock copy = Downloads.ReadBlock(Token.TransferId, block.BlockNumber); Assert.AreEqual(block.BlockNumber, copy.BlockNumber); CollectionAssert.AreEqual(block.Data, copy.Data); }
public void Service_Should_Always_Return_Same_Data() { List <BufferedDataBlock> blockList = new List <BufferedDataBlock>(); long blockNumber = 0; while (true) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); blockList.Add(block); if (block.IsLastBlock) { break; } } for (int i = 0; i < blockList.Count; i++) { BufferedDataBlock block = blockList[i]; BufferedDataBlock copy = Downloads.ReadBlock(Token.TransferId, i); Assert.AreEqual(block.BlockLength, copy.BlockLength); Assert.AreEqual(block.BlockNumber, copy.BlockNumber); CollectionAssert.AreEqual(block.Data, copy.Data); Assert.AreEqual(block.Offset, copy.Offset); } //the same in the other direction for (int i = blockList.Count - 1; i >= 0; i--) { BufferedDataBlock block = blockList[i]; BufferedDataBlock copy = Downloads.ReadBlock(Token.TransferId, i); Assert.AreEqual(block.BlockLength, copy.BlockLength); Assert.AreEqual(block.BlockNumber, copy.BlockNumber); CollectionAssert.AreEqual(block.Data, copy.Data); Assert.AreEqual(block.Offset, copy.Offset); } }
public void Received_Blocks_Should_Indicate_Offset_Within_Resource() { long blockNumber = 0; var timestamp = DateTimeOffset.Now.AddMinutes(5); SystemTime.Now = () => timestamp; while (true) { BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++); if (block.BlockNumber != 0) { Assert.AreEqual(new FileInfo(TargetFilePath).Length, block.Offset); } ReceiveBlock(block); if (block.IsLastBlock) { break; } } }
public void Random_Block_Access_Should_Be_Possible() { Downloads.ReadBlock(Token.TransferId, 5); Downloads.ReadBlock(Token.TransferId, 7); Downloads.ReadBlock(Token.TransferId, 4); }
public void Pausing_Should_Be_Possible_After_Having_Started_Downloading() { Downloads.ReadBlock(Token.TransferId, 0); PauseAndVerifyStatusIsPaused(); CompleteReading(); }
public void Status_Should_Have_Been_Adjusted_After_Reading_First_Block() { Assert.AreEqual(TransferStatus.Starting, Downloads.GetTransferStatus(Token.TransferId)); Downloads.ReadBlock(Token.TransferId, 0); Assert.AreEqual(TransferStatus.Running, Downloads.GetTransferStatus(Token.TransferId)); }
public void Submitting_Block_Number_Above_Total_Blocks_Should_Fail() { Downloads.ReadBlock(Token.TransferId, Token.TotalBlockCount); }
public void Submitting_Negative_Block_Number_Should_Fail() { Downloads.ReadBlock(Token.TransferId, -1); }