public void GivenWrongLengthWillFix() { string newFile = DownloadTests.SafePath("testing.test"); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1); Assert.AreEqual(Constants.ONE_GIG_FILE_LENGTH, parameters.FileSize); }
public void IdIsAssignedWhenNotSupplied() { var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1); Assert.NotNull(parameters.Id); Assert.True(parameters.Id.Length > 10); }
public void DownloadSmallFilesWithNonOptimizedStream(int threadCount) { var uri = new Uri(Constants.TWENTY_MEG_FILE); var path = DownloadTests.SafePath("sites_vcf.gz"); Action <string> logger = (message) => { }; var timer = new Stopwatch(); timer.Start(); var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) }); LargeFileDownloadParameters.EnsureCleanFile(path, true); using (var stream = new FileStream(path, FileMode.OpenOrCreate)) { ILargeFileDownloadParameters parameters = new LargeFileDownloadWithStreamParameters(uri, stream, Constants.TWENTY_MEG_FILE_LENGTH, maxThreads: threadCount); Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds); //try to open the file DownloadTests.ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM); } }
public void GivenWrongLengthWillNotFixIfTurnedOff() { string newFile = DownloadTests.SafePath("testing.test"); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1, verifyLength: false); Assert.AreEqual(1, parameters.FileSize); }
public void SparseFileIsCreatedOnWindows() { string newFile = DownloadTests.SafePath("testing.test"); try { var timer = new Stopwatch(); timer.Start(); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, Constants.ONE_GIG_FILE_LENGTH); Stream s = parameters.GetOutputStream(); //write to the end of the file s.Seek((Constants.ONE_GIG_FILE_LENGTH) - 100, SeekOrigin.Begin); var encoding = new System.Text.ASCIIEncoding(); Byte[] bytes = encoding.GetBytes("hello"); s.Write(bytes, 0, bytes.Length); s.Close(); var info = new FileInfo(newFile); timer.Stop(); //we should've pre-allocated file length Assert.AreEqual(info.Length, Constants.ONE_GIG_FILE_LENGTH ); Assert.True(timer.ElapsedMilliseconds < 1500); //should happen in less than a second since it's sparse } finally { File.Delete(newFile); } }
public void MultipleParallelChunkedDownload(int threadCount, int parallelFactor) { Action <string> download = (prefix) => { var uri = new Uri(Constants.TWENTY_MEG_FILE); var path = SafePath(prefix + "sites_vcf.gz"); Action <string> logger = (message) => Trace.WriteLine(message); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters( uri, path, 29996532, null, maxThreads: threadCount); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM); }; var tasks = new List <Task>(); for (int i = 0; i < parallelFactor; i++) { int i1 = i; Task t = Task.Factory.StartNew(() => download(i1.ToString())); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); }
public void SparseFileIsCreatedOnWindows() { string newFile = DownloadTests.SafePath("testing.test"); try { var timer = new Stopwatch(); timer.Start(); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, Constants.ONE_GIG_FILE_LENGTH); Stream s = parameters.GetOutputStream(); //write to the end of the file s.Seek((Constants.ONE_GIG_FILE_LENGTH) - 100, SeekOrigin.Begin); var encoding = new System.Text.ASCIIEncoding(); Byte[] bytes = encoding.GetBytes("hello"); s.Write(bytes, 0, bytes.Length); s.Close(); var info = new FileInfo(newFile); timer.Stop(); //we should've pre-allocated file length Assert.AreEqual(info.Length, Constants.ONE_GIG_FILE_LENGTH); Assert.True(timer.ElapsedMilliseconds < 1500); //should happen in less than a second since it's sparse } finally { File.Delete(newFile); } }
public DownloadFileCommand(Uri uri, string targetFileName, string fileId = null, CancellationToken token = new CancellationToken(), bool enableLogging = true) { ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, targetFileName, 0, fileId ?? targetFileName); _parameters = parameters; _token = token; _enableLogging = enableLogging; }
public DownloadFileCommand(BaseSpaceClient client, FileCompact file, string targetFileName, IClientSettings settings, CancellationToken token = new CancellationToken(), bool enableLogging = true, int threadCount = DEFAULT_THREADS) { DateTime expiration; string url = GetFileContentUrl(client, file.Id, out expiration); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(new Uri(url), targetFileName, maxThreads: threadCount, maxChunkSize: (int?)settings.FileDownloadMultipartSizeThreshold, id: file.Id); _parameters = parameters; _token = token; _enableLogging = enableLogging; }
public DownloadFileCommand(BaseSpaceClient client, FileCompact file, string targetFileName, IClientSettings settings, int threadCount, int maxChunkSize, CancellationToken token = new CancellationToken(), bool enableLogging = true) { DateTime expiration; string url = GetFileContentUrl(client, file.Id, out expiration); _fileName = string.Format("[{0}],{1}", file.Id, file.Name); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(new Uri(url), targetFileName, maxThreads: threadCount, maxChunkSize: maxChunkSize, id: file.Id); _parameters = parameters; _token = token; _enableLogging = enableLogging; }
public void ParallelChunkedDownload(int threadCount) { var uri = new Uri(Constants.TWENTY_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action <string> logger = (message) => Trace.WriteLine(message); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 29996532, null, maxThreads: threadCount); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM); }
public void DownloadZeroByteFile() { var uri = new Uri(Constants.ZERO_BYTE_FILE); string path = SafePath("sites_vcf.gz"); Action <string> logger = (message) => { }; var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, null, null); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(15)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", parameters.MaxThreads, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.ZERO_BYTE_CHECKSUM); }
public void DownloadSmallerInSingleChunk() { var uri = new Uri(Constants.FIVE_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action <string> logger = (message) => { }; var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, maxChunkSize: Constants.FIVE_MEG_LENGTH, maxThreads: 1); Task task = parameters.DownloadAsync(logger: logger, progress: new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(s => logger("progress"))); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", 1, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM); }
public void ParallelChunkedOneGig(int threadCount) { string path = ""; try { var uri = new Uri(Constants.ONE_GIG_FILE_S_SL); path = SafePath("sites_vcf.gz"); Action <string> logger = (message) => { }; var timer = new Stopwatch(); //a list to hold measured transfer rates var transferRateList = new List <double>(); var asyncProgress = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>( obj => { transferRateList.Add(obj.DownloadBitRate); logger("progress"); }); var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) }); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1297662912, null, maxThreads: threadCount); timer.Start(); Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager, progress: asyncProgress); task.Wait(TimeSpan.FromMinutes(25)); timer.Stop(); var averageTransferRate = transferRateList.Average(); Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", threadCount, timer.ElapsedMilliseconds, averageTransferRate); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.ONE_GIG_CHECKSUM); //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 30% (accuracy of atleast 70%) Assert.AreEqual(parameters.FileSize, averageTransferRate * timer.Elapsed.TotalSeconds, 0.30 * parameters.FileSize); } finally { File.Delete(path); } }
public void CancellationTokenWillCancel() { var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false); var writeQueue = new ConcurrentQueue <ChunkedFilePart>(); byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world"); var mockClient = new Mock <ISimpleHttpGetByRangeClient>(); mockClient.Setup(x => x.Get(It.IsAny <Uri>(), It.IsAny <long>(), It.IsAny <long>())) .Returns(() => { Thread.Sleep(200); return(new SimpleHttpResponse(206, sampleResponse, null)); }); int timesAskedForSlow = -1; var readStack = new ConcurrentStack <int>(); //add all of the chunks to the stack readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray()); Func <int, bool> shouldSlw = i => { timesAskedForSlow++; return(false); }; var tokenSource = new CancellationTokenSource(); var failureToken = new FailureToken(); var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) }); var task = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token); task.Start(); Thread.Sleep(500); tokenSource.Cancel(); task.Wait(TimeSpan.FromMinutes(2)); int current; readStack.TryPop(out current); Assert.True(current != 10); //we shouldn't get to 10 before the cancel works }
public void DownloadSmallerFile() { //8 threads //MaxChunkSize = 1048576 //FileSize = 5242880 var uri = new Uri(Constants.FIVE_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action <string> logger = (message) => { }; var transferRateList = new List <double>(); var asyncProgress = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>( obj => { transferRateList.Add(obj.DownloadBitRate); Debug.WriteLine(obj.DownloadBitRate); logger("progress"); }); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, null, maxThreads: 8); Task task = parameters.DownloadAsync(logger: logger, progress: asyncProgress); task.Wait(TimeSpan.FromMinutes(1)); timer.Stop(); var averageTransferRate = transferRateList.Average(); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM); if (averageTransferRate > 0)// sometimes tx rate is 0, possibly due to caching { Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", 8, timer.ElapsedMilliseconds, averageTransferRate); //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 40% (accuracy of atleast 60%) because it is a small file and txrate is calculated once every two seconds. Assert.GreaterOrEqual(averageTransferRate * timer.Elapsed.TotalSeconds, 0.40 * parameters.FileSize); } }
public void DownloadSmallerInSingleChunk() { var uri = new Uri(Constants.FIVE_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action<string> logger = (message) => { }; var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, maxChunkSize: Constants.FIVE_MEG_LENGTH, maxThreads: 1); Task task = parameters.DownloadAsync(logger: logger, progress: new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>( s=> logger("progress")) ); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", 1, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM); }
public void DownloadZeroByteFile() { var uri = new Uri(Constants.ZERO_BYTE_FILE); string path = SafePath("sites_vcf.gz"); Action<string> logger = (message) => { }; var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path,null, null); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(15)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", parameters.MaxThreads, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.ZERO_BYTE_CHECKSUM); }
private static bool ProcessArgs(string[] args) { var options = new DownloadOptions(); try { var optionSet = CreateOptions(options); optionSet.Parse(args); if (options.ShowHelp || (options.Uri == null)) { optionSet.WriteOptionDescriptions(Console.Out); } else if (options.HasException) { Console.WriteLine(options.Exception.Message); } else { //go get file size GetSizeAndSource(options); LargeFileDownloadParameters parameters = null; if (options.Uri != null && options.FileSize.HasValue && options.FileSize.Value > 0) { parameters = new LargeFileDownloadParameters(options.Uri, options.OutputFile, options.FileSize.Value, options.MaxThreads, options.MaxChunkSize); } if (parameters != null) { Action <string> logger = (message) => Debug.WriteLine(message); var token = new CancellationToken(); var watch = new Stopwatch(); watch.Start(); IAsyncProgress <LargeFileDownloadProgressChangedEventArgs> progress = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(Handler); var task = parameters.DownloadAsync(token, progress, logger); task.Wait(); watch.Stop(); ClearCurrentConsoleLine(); Console.WriteLine("done in {0}ms ({1}m {2}s {3}ms", watch.ElapsedMilliseconds, watch.Elapsed.Minutes, watch.Elapsed.Seconds, watch.Elapsed.Milliseconds); if (options.IsInteractive) { return(true); } Environment.Exit(0); } } } catch (Exception e) { Console.WriteLine(e.Message); if (options.IsInteractive) { return(true); } Environment.Exit(-1); } return(!options.ShouldExit); }
public void MultipleParallelChunkedDownload(int threadCount, int parallelFactor) { Action<string> download = (prefix) => { var uri = new Uri(Constants.TWENTY_MEG_FILE); var path = SafePath(prefix +"sites_vcf.gz"); Action<string> logger = (message) => Trace.WriteLine(message); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters( uri, path, 29996532,null, maxThreads: threadCount); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM); }; var tasks = new List<Task>(); for (int i = 0; i < parallelFactor; i++) { int i1 = i; Task t = Task.Factory.StartNew(() => download(i1.ToString())); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); }
public void MaxChunkSizeCalculation(int fileSize, int chunkSize, int expected) { var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", fileSize, verifyLength: false, maxChunkSize: chunkSize); Assert.AreEqual(expected, parameters.MaxChunkSize); }
public void ParallelChunkedOneGig(int threadCount) { string path = ""; try { var uri = new Uri(Constants.ONE_GIG_FILE_S_SL); path = SafePath("sites_vcf.gz"); Action<string> logger = (message) => { }; var timer = new Stopwatch(); //a list to hold measured transfer rates var transferRateList = new List<double>(); var asyncProgress = new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>( obj => { transferRateList.Add(obj.DownloadBitRate); logger("progress"); }); var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) }); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1297662912, null, maxThreads: threadCount); timer.Start(); Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager, progress: asyncProgress); task.Wait(TimeSpan.FromMinutes(25)); timer.Stop(); var averageTransferRate = transferRateList.Average(); Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", threadCount, timer.ElapsedMilliseconds, averageTransferRate); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.ONE_GIG_CHECKSUM); //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 30% (accuracy of atleast 70%) Assert.AreEqual(parameters.FileSize, averageTransferRate * timer.Elapsed.TotalSeconds, 0.30 * parameters.FileSize); } finally { File.Delete(path); } }
public void IdIsAssignedWhenNotSupplied() { var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1); Assert.NotNull( parameters.Id); Assert.True(parameters.Id.Length > 10); }
public void GivenWrongLengthWillNotFixIfTurnedOff() { string newFile = DownloadTests.SafePath("testing.test"); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1,verifyLength: false); Assert.AreEqual(1, parameters.FileSize); }
public void GivenWrongLengthWillFix() { string newFile = DownloadTests.SafePath("testing.test"); var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile,1); Assert.AreEqual(Constants.ONE_GIG_FILE_LENGTH, parameters.FileSize); }
public void ParallelChunkedDownload(int threadCount) { var uri = new Uri(Constants.TWENTY_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action<string> logger = ( message) => Trace.WriteLine(message); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 29996532, null, maxThreads: threadCount); Task task = parameters.DownloadAsync(logger: logger); task.Wait(TimeSpan.FromMinutes(5)); timer.Stop(); Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM); }
public void DownloadSmallerFile() { //8 threads //MaxChunkSize = 1048576 //FileSize = 5242880 var uri = new Uri(Constants.FIVE_MEG_FILE); var path = SafePath("sites_vcf.gz"); Action<string> logger = (message) => { }; var transferRateList = new List<double>(); var asyncProgress = new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>( obj => { transferRateList.Add(obj.DownloadBitRate); Debug.WriteLine(obj.DownloadBitRate); logger("progress"); }); var timer = new Stopwatch(); timer.Start(); ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, null, maxThreads: 8); Task task = parameters.DownloadAsync(logger: logger, progress:asyncProgress); task.Wait(TimeSpan.FromMinutes(1)); timer.Stop(); var averageTransferRate = transferRateList.Average(); //try to open the file ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM); if (averageTransferRate > 0)// sometimes tx rate is 0, possibly due to caching { Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", 8, timer.ElapsedMilliseconds, averageTransferRate); //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 40% (accuracy of atleast 60%) because it is a small file and txrate is calculated once every two seconds. Assert.GreaterOrEqual(averageTransferRate * timer.Elapsed.TotalSeconds, 0.40 * parameters.FileSize); } }
public void IdIsAssignedWhenSupplied() { var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1, id:"test"); Assert.True(parameters.Id == "test"); }
private static bool ProcessArgs(string[] args) { var options = new DownloadOptions(); try { var optionSet = CreateOptions(options); optionSet.Parse(args); if (options.ShowHelp || (options.Uri == null)) { optionSet.WriteOptionDescriptions(Console.Out); } else if (options.HasException) { Console.WriteLine(options.Exception.Message); } else { //go get file size GetSizeAndSource(options); LargeFileDownloadParameters parameters = null; if (options.Uri != null && options.FileSize.HasValue && options.FileSize.Value > 0) { parameters = new LargeFileDownloadParameters(options.Uri, options.OutputFile, options.FileSize.Value, options.MaxThreads, options.MaxChunkSize); } if (parameters != null) { Action<string> logger = (message) => Debug.WriteLine(message); var token = new CancellationToken(); var watch = new Stopwatch(); watch.Start(); IAsyncProgress<LargeFileDownloadProgressChangedEventArgs> progress =new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>(Handler); var task = parameters.DownloadAsync(token, progress, logger); task.Wait(); watch.Stop(); ClearCurrentConsoleLine(); Console.WriteLine("done in {0}ms ({1}m {2}s {3}ms", watch.ElapsedMilliseconds, watch.Elapsed.Minutes, watch.Elapsed.Seconds, watch.Elapsed.Milliseconds); if (options.IsInteractive) { return true; } Environment.Exit(0); } } } catch (Exception e) { Console.WriteLine(e.Message); if (options.IsInteractive) { return true; } Environment.Exit(-1); } return !options.ShouldExit; }
public void IdIsAssignedWhenSupplied() { var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1, id: "test"); Assert.True(parameters.Id == "test"); }
public void CancellationTokenWillCancel() { var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false); var writeQueue = new ConcurrentQueue<ChunkedFilePart>(); byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world"); var mockClient = new Mock<ISimpleHttpGetByRangeClient>(); mockClient.Setup(x => x.Get(It.IsAny<Uri>(), It.IsAny<long>(), It.IsAny<long>())) .Returns(() => { Thread.Sleep(200); return new SimpleHttpResponse(206, sampleResponse, null); }); int timesAskedForSlow = -1; var readStack = new ConcurrentStack<int>(); //add all of the chunks to the stack readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray()); Func<int, bool> shouldSlw = i => { timesAskedForSlow++; return false; }; var tokenSource = new CancellationTokenSource(); var failureToken = new FailureToken(); var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) }); var task = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token); task.Start(); Thread.Sleep(500); tokenSource.Cancel(); task.Wait(TimeSpan.FromMinutes(2)); int current; readStack.TryPop(out current); Assert.True(current != 10); //we shouldn't get to 10 before the cancel works }