[hyena] [Hyena.Downloader] Add a couple tests
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hyena] [Hyena.Downloader] Add a couple tests
- Date: Thu, 2 Sep 2010 16:17:07 +0000 (UTC)
commit 79289f9aab361c4758c49712d6f840ac3e869818
Author: Gabriel Burt <gabriel burt gmail com>
Date: Thu Sep 2 11:16:08 2010 -0500
[Hyena.Downloader] Add a couple tests
Hyena/Hyena.Downloader/HttpDownloaderState.cs | 11 +++-
Hyena/Hyena.Downloader/Tests/HttpTestServer.cs | 60 ++++++++++++---
Hyena/Hyena.Downloader/Tests/Tests.cs | 94 ++++++++++++++++++++++++
Hyena/Makefile.am | 1 +
4 files changed, 152 insertions(+), 14 deletions(-)
---
diff --git a/Hyena/Hyena.Downloader/HttpDownloaderState.cs b/Hyena/Hyena.Downloader/HttpDownloaderState.cs
index ec4e1d6..5828d20 100644
--- a/Hyena/Hyena.Downloader/HttpDownloaderState.cs
+++ b/Hyena/Hyena.Downloader/HttpDownloaderState.cs
@@ -42,5 +42,14 @@ namespace Hyena.Downloader
public string ContentType { get; internal set; }
public string CharacterSet { get; internal set; }
public Exception FailureException { get; internal set; }
+
+ public override string ToString ()
+ {
+ if (Working) {
+ return String.Format ("HttpDownloaderState: working ({0}% complete)", PercentComplete * 100.0);
+ } else {
+ return String.Format ("HttpDownloaderState: finished, {0}", Success ? "successful" : "error: " + FailureException.Message);
+ }
+ }
}
-}
\ No newline at end of file
+}
diff --git a/Hyena/Hyena.Downloader/Tests/HttpTestServer.cs b/Hyena/Hyena.Downloader/Tests/HttpTestServer.cs
index 8135185..7201669 100644
--- a/Hyena/Hyena.Downloader/Tests/HttpTestServer.cs
+++ b/Hyena/Hyena.Downloader/Tests/HttpTestServer.cs
@@ -35,7 +35,7 @@ using System.Security.Cryptography;
namespace Hyena.Downloader.Tests
{
- internal class HttpTestServer
+ internal class HttpTestServer : IDisposable
{
private class Resource
{
@@ -45,11 +45,19 @@ namespace Hyena.Downloader.Tests
}
private List<Resource> resources = new List<Resource> ();
+ private bool stop_requested;
private bool running;
+ private bool serving;
+ private HttpListener listener;
public int ResourceCount { get; set; }
public int MinResourceSize { get; set; }
public int MaxResourceSize { get; set; }
+ public bool IsServing { get { return serving; } }
+
+ public bool Debug = true;
+
+ public string BaseUrl { get { return "http://localhost:8080/"; } }
public HttpTestServer ()
{
@@ -60,22 +68,47 @@ namespace Hyena.Downloader.Tests
public void Run ()
{
+ stop_requested = false;
+ running = true;
GenerateStaticContent ();
ServeStaticContent ();
+ running = false;
+ serving = false;
+ }
+
+ public void Dispose ()
+ {
+ Stop ();
+ }
+
+ public void Stop ()
+ {
+ lock (this) {
+ if (!running)
+ return;
+
+ stop_requested = true;
+ listener.Abort ();
+
+ // busy wait, oh well
+ if (Debug) Console.WriteLine ("waiting for server to stop");
+ while (running) {}
+ if (Debug) Console.WriteLine (" > done waiting for server to stop");
+ }
}
private void ServeStaticContent ()
{
- Console.WriteLine ();
- Console.WriteLine ("Serving static content...");
+ if (Debug) Console.WriteLine ();
+ if (Debug) Console.WriteLine ("Serving static content...");
- var listener = new HttpListener ();
- listener.Prefixes.Add ("http://localhost:8080/");
+ listener = new HttpListener ();
+ listener.Prefixes.Add (BaseUrl);
listener.Start ();
-
- running = true;
- while (running) {
+ serving = true;
+
+ while (!stop_requested) {
var async_result = listener.BeginGetContext (result => {
var context = listener.EndGetContext (result);
var response = context.Response;
@@ -86,14 +119,14 @@ namespace Hyena.Downloader.Tests
response.ProtocolVersion = new Version ("1.1");
try {
- Console.WriteLine ("Serving: {0}", path);
+ if (Debug) Console.WriteLine ("Serving: {0}", path);
if (path == "/") {
ServeString (response, resources.Count.ToString () + "\n");
return;
} else if (path == "/shutdown") {
ServeString (response, "Goodbye\n");
lock (this) {
- running = false;
+ stop_requested = true;
}
}
@@ -142,6 +175,7 @@ namespace Hyena.Downloader.Tests
private void GenerateStaticContent ()
{
+ resources.Clear ();
var random = new Random ();
var root = "/tmp/hyena-download-test-server";
@@ -159,7 +193,7 @@ namespace Hyena.Downloader.Tests
Length = random.Next (MinResourceSize, MaxResourceSize + 1)
};
- Console.WriteLine ();
+ if (Debug) Console.WriteLine ();
using (var stream = File.OpenWrite (resource.Path)) {
var buffer = new byte[32 << 10];
@@ -176,7 +210,7 @@ namespace Hyena.Downloader.Tests
written += buffer_length;
md5.TransformBlock (buffer, 0, buffer_length, null, 0);
- Console.Write ("\rCreating resource: {0} ({1:0.00} MB): [{2}/{3}] {4:0.0}% ",
+ if (Debug) Console.Write ("\rCreating resource: {0} ({1:0.00} MB): [{2}/{3}] {4:0.0}% ",
resource.Path, resource.Length / 1024.0 / 1024.0,
i + 1, ResourceCount,
written / (double)resource.Length * 100.0);
@@ -192,4 +226,4 @@ namespace Hyena.Downloader.Tests
}
}
-#endif
\ No newline at end of file
+#endif
diff --git a/Hyena/Hyena.Downloader/Tests/Tests.cs b/Hyena/Hyena.Downloader/Tests/Tests.cs
new file mode 100644
index 0000000..76b4a40
--- /dev/null
+++ b/Hyena/Hyena.Downloader/Tests/Tests.cs
@@ -0,0 +1,94 @@
+//
+// Tests.cs
+//
+// Author:
+// Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2010 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if ENABLE_TESTS
+
+using System;
+using System.IO;
+using System.Linq;
+using NUnit.Framework;
+using Hyena;
+
+namespace Hyena.Downloader.Tests
+{
+ [TestFixture]
+ public class Tests
+ {
+ [Test]
+ public void SimpleDownload ()
+ {
+ StartServer ();
+
+ new HttpStringDownloader () {
+ Uri = new Uri (server.BaseUrl),
+ Finished = (d) => {
+ Assert.IsTrue (d.State.Success);
+ Assert.AreEqual (server.ResourceCount.ToString () + "\n", d.Content);
+ }
+ }.StartSync ();
+
+ var f = new HttpFileDownloader () { Uri = new Uri (server.BaseUrl + "/1") };
+ f.FileFinished += (d) => {
+ Assert.IsTrue (d.State.Success);
+ var size = new System.IO.FileInfo (d.LocalPath).Length;
+ Assert.IsTrue (size <= server.MaxResourceSize);
+ Assert.IsTrue (size >= server.MinResourceSize);
+ };
+ f.StartSync ();
+ }
+
+ [Test]
+ public void DownloadManager ()
+ {
+ }
+
+ private void StartServer ()
+ {
+ server.Stop ();
+ new System.Threading.Thread (server.Run).Start ();
+ while (!server.IsServing) {}
+ }
+
+ private HttpTestServer server;
+
+ [TestFixtureSetUp]
+ public void Setup ()
+ {
+ server = new HttpTestServer ();
+ server.Debug = false;
+ }
+
+ [TestFixtureTearDown]
+ public void Teardown ()
+ {
+ server.Dispose ();
+ }
+ }
+}
+
+#endif
diff --git a/Hyena/Makefile.am b/Hyena/Makefile.am
index 4fba02a..e4301f4 100644
--- a/Hyena/Makefile.am
+++ b/Hyena/Makefile.am
@@ -44,6 +44,7 @@ SOURCES = \
Hyena.Downloader/HttpFileDownloader.cs \
Hyena.Downloader/HttpStringDownloader.cs \
Hyena.Downloader/Tests/HttpTestServer.cs \
+ Hyena.Downloader/Tests/Tests.cs \
Hyena.Jobs/Job.cs \
Hyena.Jobs/JobExtensions.cs \
Hyena.Jobs/PriorityHints.cs \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]