This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
forked from rust-lang/rustup
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support resumption of partial downloads
* Adds support only for the Curl backend, which is the default anyway. * In order to distinguish between a file that has been fully downloaded (but not used yet) and should therefore be hash-checked vs. those that require more data, store partials with a .partial extension. * Adds a simple http-server to rustup-mock, to allow the download module to be properly tested. It's not clear how to easily emulate a server that stops half-way without that. The tests for the overall download-resumption functionality should be fairly re-usable if we migrate to another download solution in the future (e.g. in rust-lang#993) * Don't bother with resumption for meta-data files, since they're likely to go out of date anyway.
- Loading branch information
Showing
11 changed files
with
567 additions
and
220 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#![cfg(feature = "curl-backend")] | ||
|
||
extern crate download; | ||
extern crate rustup_mock; | ||
extern crate tempdir; | ||
|
||
use std::fs::{self, File}; | ||
use std::io::{Read}; | ||
use std::path::Path; | ||
|
||
use tempdir::TempDir; | ||
|
||
use rustup_mock::http_server; | ||
|
||
use download::*; | ||
|
||
fn setup(test: &Fn(TempDir, http_server::Server) -> ()) { | ||
let tmp = TempDir::new("rustup-download-test-").expect("creating tempdir for test"); | ||
let served_dir = &tmp.path().join("test-files"); | ||
fs::DirBuilder::new().create(served_dir).expect("setting up a folder to server files from"); | ||
let server = http_server::Server::serve_from(served_dir).expect("setting up http server for test"); | ||
test(tmp, server); | ||
} | ||
|
||
fn file_contents(path: &Path) -> String { | ||
let mut result = String::new(); | ||
File::open(&path).unwrap().read_to_string(&mut result).expect("reading test result file"); | ||
result | ||
} | ||
|
||
#[test] | ||
fn when_download_is_interrupted_partial_file_is_left_on_disk() { | ||
setup(&|tmpdir, mut server| { | ||
let target_path = tmpdir.path().join("downloaded"); | ||
|
||
server.put_file_from_bytes("test-file", b"12345"); | ||
|
||
server.stop_after_bytes(3); | ||
download_to_path_with_backend( | ||
Backend::Curl, &server.address().join("test-file").unwrap(), &target_path, true, None) | ||
.expect("Test download failed"); | ||
|
||
assert_eq!(file_contents(&target_path), "123"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn download_interrupted_and_resumed() { | ||
setup(&|tmpdir, mut server| { | ||
let target_path = tmpdir.path().join("downloaded"); | ||
|
||
server.put_file_from_bytes("test-file", b"12345"); | ||
|
||
server.stop_after_bytes(3); | ||
download_to_path_with_backend( | ||
Backend::Curl, &server.address().join("test-file").unwrap(), &target_path, true, None) | ||
.expect("Test download failed"); | ||
|
||
server.stop_after_bytes(2); | ||
download_to_path_with_backend( | ||
Backend::Curl, &server.address().join("test-file").unwrap(), &target_path, true, None) | ||
.expect("Test download failed"); | ||
|
||
assert_eq!(file_contents(&target_path), "12345"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn resuming_download_with_callback_that_needs_to_read_contents() { | ||
setup(&|tmpdir, mut server| { | ||
let target_path = tmpdir.path().join("downloaded"); | ||
|
||
server.put_file_from_bytes("test-file", b"12345"); | ||
|
||
server.stop_after_bytes(3); | ||
download_to_path_with_backend( | ||
Backend::Curl, &server.address().join("test-file").unwrap(), &target_path, true, Some(&|_| {Ok(())})) | ||
.expect("Test download failed"); | ||
|
||
server.stop_after_bytes(2); | ||
download_to_path_with_backend( | ||
Backend::Curl, &server.address().join("test-file").unwrap(), &target_path, true, Some(&|_| {Ok(())})) | ||
.expect("Test download failed"); | ||
|
||
assert_eq!(file_contents(&target_path), "12345"); | ||
}); | ||
} |
Oops, something went wrong.