Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit c2f2859

Browse files
Setting up a download sequence to show it works
1 parent 28a741b commit c2f2859

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected override void Run(bool success)
130130

131131
if (!result)
132132
{
133-
Logger.Warning($"Downloaded MD5 {md5} does not match expected. Deleting {Destination}.");
133+
Logger.Warning($"Downloaded MD5 {md5} does not match {ValidationHash}. Deleting {Destination}.");
134134
fileSystem.FileDelete(Destination);
135135
}
136136
else
@@ -237,7 +237,7 @@ public bool Download()
237237

238238
protected string Destination { get; }
239239

240-
protected string ValidationHash { get; }
240+
public string ValidationHash { get; set; }
241241

242242
protected int RetryCount { get; }
243243
}
@@ -264,23 +264,41 @@ public DownloadTextTask(CancellationToken token, string url)
264264

265265
protected override string RunWithReturn(bool success)
266266
{
267-
base.RunWithReturn(success);
267+
var result = base.RunWithReturn(success);
268268

269269
RaiseOnStart();
270270

271-
var webRequest = WebRequest.Create(Url);
272-
webRequest.Method = "GET";
273-
webRequest.Timeout = 3000;
274-
275-
using (var webResponse = (HttpWebResponse) webRequest.GetResponseWithoutException())
271+
try
276272
{
277-
var webResponseCharacterSet = webResponse.CharacterSet ?? Encoding.UTF8.BodyName;
278-
var encoding = Encoding.GetEncoding(webResponseCharacterSet);
273+
Logger.Trace($"Downloading {Url}");
274+
var webRequest = WebRequest.Create(Url);
275+
webRequest.Method = "GET";
276+
webRequest.Timeout = 3000;
279277

280-
using (var responseStream = webResponse.GetResponseStream())
281-
using (var reader = new StreamReader(responseStream, encoding))
282-
return reader.ReadToEnd();
278+
using (var webResponse = (HttpWebResponse)webRequest.GetResponseWithoutException())
279+
{
280+
var webResponseCharacterSet = webResponse.CharacterSet ?? Encoding.UTF8.BodyName;
281+
var encoding = Encoding.GetEncoding(webResponseCharacterSet);
282+
283+
using (var responseStream = webResponse.GetResponseStream())
284+
using (var reader = new StreamReader(responseStream, encoding))
285+
{
286+
result = reader.ReadToEnd();
287+
}
288+
}
289+
}
290+
catch (Exception ex)
291+
{
292+
Errors = ex.Message;
293+
if (!RaiseFaultHandlers(new DownloadException("Error downloading text", ex)))
294+
throw;
283295
}
296+
finally
297+
{
298+
RaiseOnEnd(result);
299+
}
300+
301+
return result;
284302
}
285303

286304
protected virtual void UpdateProgress(float progress)

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,57 @@ public void TestDownloadTextTask()
9292
var resultLines = result.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
9393
resultLines[0].Should().Be("# If you would like to crawl GitHub contact us at [email protected].");
9494
}
95+
96+
[Test]
97+
public void TestDownloadFileAndHash()
98+
{
99+
InitializeTaskManager();
100+
101+
var gitArchivePath = TestBasePath.Combine("git.zip");
102+
var gitLfsArchivePath = TestBasePath.Combine("git-lfs.zip");
103+
104+
var fileSystem = new FileSystem();
105+
106+
var downloadGitMd5Task = new DownloadTextTask(CancellationToken.None,
107+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip.MD5.txt?cb=1");
108+
109+
var downloadGitTask = new DownloadTask(CancellationToken.None, fileSystem,
110+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip", gitArchivePath, retryCount: 1);
111+
112+
var downloadGitLfsMd5Task = new DownloadTextTask(CancellationToken.None,
113+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip.MD5.txt?cb=1");
114+
115+
var downloadGitLfsTask = new DownloadTask(CancellationToken.None, fileSystem,
116+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip", gitLfsArchivePath, retryCount: 1);
117+
118+
var result = true;
119+
Exception exception = null;
120+
121+
var autoResetEvent = new AutoResetEvent(false);
122+
123+
downloadGitMd5Task
124+
.Then((b, s) =>
125+
{
126+
downloadGitTask.ValidationHash = s;
127+
})
128+
.Then(downloadGitTask)
129+
.Then(downloadGitLfsMd5Task)
130+
.Then((b, s) =>
131+
{
132+
downloadGitLfsTask.ValidationHash = s;
133+
})
134+
.Then(downloadGitLfsTask)
135+
.Finally((b, ex) => {
136+
result = b;
137+
exception = ex;
138+
autoResetEvent.Set();
139+
})
140+
.Start();
141+
142+
autoResetEvent.WaitOne();
143+
144+
result.Should().BeTrue();
145+
exception.Should().BeNull();
146+
}
95147
}
96148
}

0 commit comments

Comments
 (0)