[rygel] tests: Refactor HTTPResponse unit tests
- From: Zeeshan Ali Khattak <zeeshanak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [rygel] tests: Refactor HTTPResponse unit tests
- Date: Fri, 14 May 2010 18:44:34 +0000 (UTC)
commit e82309ccee6b8dff5c2bd924acf52be626b6ce7f
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date: Fri May 14 21:33:01 2010 +0300
tests: Refactor HTTPResponse unit tests
Instead of having a separate unit test for abstract HTTPResponse class,
keep the common code between unit tests of its subclasses in one module.
tests/Makefile.am | 7 +-
tests/rygel-http-response-test.vala | 189 +++++++++++-------
tests/rygel-http-response-test_live-response.vala | 1 +
...rygel-http-response-test_seekable-response.vala | 1 +
tests/rygel-http-response.vala | 1 -
tests/rygel-live-response-test.vala | 211 +------------------
tests/rygel-seekable-response-test.vala | 213 ++------------------
tests/rygel-state-machine.vala | 1 -
8 files changed, 150 insertions(+), 474 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 77092a9..7350a03 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -26,7 +26,6 @@ AM_VALAFLAGS = --disable-warnings --thread \
--pkg gio-2.0 --pkg gee-1.0 -g
check_PROGRAMS = rygel-http-item-uri-test \
- rygel-http-response-test \
rygel-live-response-test \
rygel-seekable-response-test \
rygel-http-byte-seek-test \
@@ -37,13 +36,10 @@ TESTS = $(check_PROGRAMS)
rygel_http_item_uri_test_SOURCES = rygel-http-item-uri-test.vala \
rygel-http-item-uri.vala
-rygel_http_response_test_SOURCES = rygel-http-response-test.vala \
- rygel-http-response.vala \
- rygel-state-machine.vala
-
rygel_live_response_test_SOURCES = rygel-live-response-test.vala \
rygel-live-response.vala \
rygel-http-response_live-response.vala \
+ rygel-http-response-test_live-response.vala \
rygel-state-machine_live-response.vala \
rygel-http-seek_live-response.vala \
rygel-gst-utils.vala
@@ -51,6 +47,7 @@ rygel_live_response_test_SOURCES = rygel-live-response-test.vala \
rygel_seekable_response_test_SOURCES = \
rygel-seekable-response-test.vala \
rygel-seekable-response.vala \
+ rygel-http-response-test_seekable-response.vala \
rygel-http-response_seekable-response.vala \
rygel-state-machine_seekable-response.vala
diff --git a/tests/rygel-http-response-test.vala b/tests/rygel-http-response-test.vala
index 424b08f..c280823 100644
--- a/tests/rygel-http-response-test.vala
+++ b/tests/rygel-http-response-test.vala
@@ -21,6 +21,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
+// This module contains the common code between the test cases for
+// HTTPResponse subclasses.
using Soup;
public errordomain Rygel.TestError {
@@ -28,100 +30,107 @@ public errordomain Rygel.TestError {
TIMEOUT
}
-public class Rygel.HTTPResponseTest : GLib.Object {
- private HTTPServer server;
- private HTTPClient client;
+public abstract class Rygel.HTTPResponseTest : GLib.Object {
+ private static const long MAX_BYTES = 1024;
- private MainLoop main_loop;
+ protected HTTPServer server;
+ protected HTTPClient client;
- public static int main (string[] args) {
- try {
- var test = new HTTPResponseTest ();
+ private bool server_done;
+ private bool client_done;
- test.run ();
- } catch (TestError.SKIP error) {
- return error.code;
- } catch (Error error) {
- critical ("%s", error.message);
+ private MainLoop main_loop;
- return -1;
- }
+ protected Cancellable cancellable;
+ private Error error;
- return 0;
- }
+ public HTTPResponseTest (Cancellable? cancellable = null) throws Error {
+ this.cancellable = cancellable;
- private HTTPResponseTest () throws TestError {
this.server = new HTTPServer ();
- this.client = new HTTPClient (this.server.context);
+ this.client = new HTTPClient (this.server.context,
+ this.server.uri,
+ MAX_BYTES,
+ cancellable != null);
this.main_loop = new MainLoop (null, false);
}
- private void run () throws Error {
- Error error = null;
-
- Timeout.add_seconds (3, () => {
- error = new TestError.TIMEOUT ("Timeout");
- this.main_loop.quit ();
+ public HTTPResponseTest.complete () throws Error {
+ this ();
+ }
- return false;
- });
+ public HTTPResponseTest.abort () throws Error {
+ this (new Cancellable ());
+ }
+ public virtual void run () throws Error {
+ Timeout.add_seconds (3, this.on_timeout);
this.server.message_received.connect (this.on_message_received);
+ this.server.message_aborted.connect (this.on_message_aborted);
+ if (this.cancellable == null) {
+ this.client.completed.connect (this.on_client_completed);
+ } else {
+ this.client_done = true;
+ }
- this.client.run.begin (this.server.uri);
+ this.client.run.begin ();
this.main_loop.run ();
- if (error != null) {
- throw error;
+ if (this.error != null) {
+ throw this.error;
}
}
- private void on_message_received (HTTPServer server,
- Message msg) {
- var response = new HTTPDummyResponse (this.server.context.server,
- msg,
- false,
- null);
+ internal abstract HTTPResponse create_response (Soup.Message msg)
+ throws Error;
- response.completed.connect (() => {
+ private void on_client_completed (StateMachine client) {
+ if (this.server_done) {
this.main_loop.quit ();
- });
+ }
- response.run.begin ();
+ this.client_done = true;
}
-}
-private class Rygel.HTTPDummyResponse : Rygel.HTTPResponse {
- public static const string RESPONSE_DATA = "THIS IS VALA!";
+ private void on_response_completed (StateMachine response) {
+ if (this.client_done) {
+ this.main_loop.quit ();
+ }
- public HTTPDummyResponse (Soup.Server server,
- Soup.Message msg,
- bool partial,
- Cancellable? cancellable) {
- base (server, msg, partial, cancellable);
+ this.server_done = true;
}
- public override async void run () {
- this.server.pause_message (this.msg);
+ private void on_message_received (HTTPServer server,
+ Soup.Message msg) {
+ try {
+ var response = this.create_response (msg);
- SourceFunc run_continue = run.callback;
+ response.run.begin ();
- Idle.add (() => {
- run_continue ();
+ response.completed.connect (this.on_response_completed);
+ } catch (Error error) {
+ this.error = error;
+ this.main_loop.quit ();
- return false;
- });
+ return;
+ }
+ }
- yield;
+ private void on_message_aborted (HTTPServer server,
+ Soup.Message msg) {
+ this.cancellable.cancel ();
+ }
- this.push_data (RESPONSE_DATA, RESPONSE_DATA.length);
+ private bool on_timeout () {
+ this.error = new TestError.TIMEOUT ("Timeout");
+ this.main_loop.quit ();
- this.end (false, Soup.KnownStatusCode.NONE);
+ return false;
}
}
-private class Rygel.HTTPServer : GLib.Object {
+public class Rygel.HTTPServer : GLib.Object {
private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
public GUPnP.Context context;
@@ -134,7 +143,8 @@ private class Rygel.HTTPServer : GLib.Object {
}
}
- public signal void message_received (Message message);
+ public signal void message_received (Soup.Message message);
+ public signal void message_aborted (Soup.Message message);
public HTTPServer () throws TestError {
try {
@@ -148,39 +158,78 @@ private class Rygel.HTTPServer : GLib.Object {
assert (this.context.port > 0);
this.context.server.add_handler (SERVER_PATH, this.server_cb);
+ this.context.server.request_aborted.connect (this.on_request_aborted);
}
private void server_cb (Server server,
- Message msg,
+ Soup.Message msg,
string path,
HashTable? query,
ClientContext client) {
+ this.context.server.pause_message (msg);
this.message_received (msg);
}
+
+ private void on_request_aborted (Soup.Server server,
+ Soup.Message message,
+ Soup.ClientContext client) {
+ this.message_aborted (message);
+ }
}
-private class Rygel.HTTPClient : GLib.Object {
+public class Rygel.HTTPClient : GLib.Object, StateMachine {
public GUPnP.Context context;
+ public Soup.Message msg;
+ public size_t total_bytes;
- public HTTPClient (GUPnP.Context context) {
+ public Cancellable cancellable { get; set; }
+
+ public HTTPClient (GUPnP.Context context,
+ string uri,
+ size_t total_bytes,
+ bool active) {
this.context = context;
- }
+ this.total_bytes = total_bytes;
- public async void run (string uri) {
- var msg = new Message ("HTTP", uri);
- assert (msg != null);
+ this.msg = new Soup.Message ("HTTP", uri);
+ assert (this.msg != null);
+ this.msg.response_body.set_accumulate (false);
+ if (active) {
+ this.cancellable = new Cancellable ();
+ this.cancellable.cancelled += this.on_cancelled;
+ }
+ }
+
+ public async void run () {
SourceFunc run_continue = run.callback;
+ size_t bytes_received = 0;
+
+ this.msg.got_chunk.connect ((msg, chunk) => {
+ bytes_received += chunk.length;
+
+ if (bytes_received >= this.total_bytes &&
+ this.cancellable != null) {
+ bytes_received = bytes_received.clamp (0, this.total_bytes);
- this.context.session.queue_message (msg, (m) => {
- assert (msg.response_body.length ==
- HTTPDummyResponse.RESPONSE_DATA.length);
- assert ((string) msg.response_body ==
- HTTPDummyResponse.RESPONSE_DATA);
+ this.cancellable.cancel ();
+ }
+ });
+
+ this.context.session.queue_message (this.msg, (session, msg) => {
+ assert (bytes_received == this.total_bytes);
run_continue ();
});
yield;
+
+ this.completed ();
+ }
+
+ private void on_cancelled (Cancellable cancellable) {
+ this.context.session.cancel_message (this.msg,
+ KnownStatusCode.CANCELLED);
+ this.completed ();
}
}
diff --git a/tests/rygel-http-response-test_live-response.vala b/tests/rygel-http-response-test_live-response.vala
new file mode 120000
index 0000000..0cb230f
--- /dev/null
+++ b/tests/rygel-http-response-test_live-response.vala
@@ -0,0 +1 @@
+rygel-http-response-test.vala
\ No newline at end of file
diff --git a/tests/rygel-http-response-test_seekable-response.vala b/tests/rygel-http-response-test_seekable-response.vala
new file mode 120000
index 0000000..0cb230f
--- /dev/null
+++ b/tests/rygel-http-response-test_seekable-response.vala
@@ -0,0 +1 @@
+rygel-http-response-test.vala
\ No newline at end of file
diff --git a/tests/rygel-live-response-test.vala b/tests/rygel-live-response-test.vala
index 67c50cb..078cc55 100644
--- a/tests/rygel-live-response-test.vala
+++ b/tests/rygel-live-response-test.vala
@@ -24,29 +24,12 @@
using Soup;
using Gst;
-public errordomain Rygel.TestError {
- SKIP = 77,
- TIMEOUT
-}
-
-public class Rygel.LiveResponseTest : GLib.Object {
- private static const long MAX_BYTES = 1024;
+public class Rygel.LiveResponseTest : Rygel.HTTPResponseTest {
private static const long BLOCK_SIZE = MAX_BYTES / 16;
private static const long MAX_BUFFERS = MAX_BYTES / BLOCK_SIZE;
- private HTTPServer server;
- private HTTPClient client;
-
- private bool server_done;
- private bool client_done;
-
- private MainLoop main_loop;
-
private dynamic Element src;
- private Cancellable cancellable;
- private Error error;
-
public static int main (string[] args) {
Gst.init (ref args);
@@ -67,198 +50,28 @@ public class Rygel.LiveResponseTest : GLib.Object {
return 0;
}
- private LiveResponseTest (Cancellable? cancellable = null) throws Error {
- this.cancellable = cancellable;
-
- this.server = new HTTPServer ();
- this.client = new HTTPClient (this.server.context,
- this.server.uri,
- MAX_BYTES,
- cancellable != null);
- this.main_loop = new MainLoop (null, false);
+ construct {
this.src = GstUtils.create_element ("audiotestsrc", null);
}
private LiveResponseTest.complete () throws Error {
- this ();
+ base.complete ();
this.src.blocksize = BLOCK_SIZE;
this.src.num_buffers = MAX_BUFFERS;
}
private LiveResponseTest.abort () throws Error {
- this (new Cancellable ());
- }
-
- private void run () throws Error {
- Timeout.add_seconds (3, this.on_timeout);
- this.server.message_received.connect (this.on_message_received);
- this.server.message_aborted.connect (this.on_message_aborted);
- if (this.cancellable == null) {
- this.client.completed.connect (this.on_client_completed);
- } else {
- this.client_done = true;
- }
-
- this.client.run.begin ();
-
- this.main_loop.run ();
-
- if (this.error != null) {
- throw this.error;
- }
- }
-
- private void on_client_completed (StateMachine client) {
- if (this.server_done) {
- this.main_loop.quit ();
- }
-
- this.client_done = true;
- }
-
- private void on_response_completed (StateMachine response) {
- if (this.client_done) {
- this.main_loop.quit ();
- }
-
- this.server_done = true;
- }
-
- private void on_message_received (HTTPServer server,
- Soup.Message msg) {
- try {
- var response = new LiveResponse (server.context.server,
- msg,
- "TestingLiveResponse",
- this.src,
- null,
- this.cancellable);
-
- response.run.begin ();
-
- response.completed.connect (this.on_response_completed);
- } catch (Error error) {
- this.error = error;
- this.main_loop.quit ();
-
- return;
- }
- }
-
- private void on_message_aborted (HTTPServer server,
- Soup.Message msg) {
- this.cancellable.cancel ();
- }
-
- private bool on_timeout () {
- this.error = new TestError.TIMEOUT ("Timeout");
- this.main_loop.quit ();
-
- return false;
- }
-}
-
-private class Rygel.HTTPServer : GLib.Object {
- private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
-
- public GUPnP.Context context;
-
- public string uri {
- owned get { return "http://" +
- this.context.host_ip + ":" +
- this.context.port.to_string () +
- SERVER_PATH;
- }
- }
-
- public signal void message_received (Soup.Message message);
- public signal void message_aborted (Soup.Message message);
-
- public HTTPServer () throws TestError {
- try {
- this.context = new GUPnP.Context (null, "lo", 0);
- } catch (Error error) {
- throw new TestError.SKIP ("Network context not available");
- }
-
- assert (this.context != null);
- assert (this.context.host_ip != null);
- assert (this.context.port > 0);
-
- this.context.server.add_handler (SERVER_PATH, this.server_cb);
- this.context.server.request_aborted.connect (this.on_request_aborted);
- }
-
- private void server_cb (Server server,
- Soup.Message msg,
- string path,
- HashTable? query,
- ClientContext client) {
- this.context.server.pause_message (msg);
- this.message_received (msg);
- }
-
- private void on_request_aborted (Soup.Server server,
- Soup.Message message,
- Soup.ClientContext client) {
- this.message_aborted (message);
- }
-}
-
-private class Rygel.HTTPClient : GLib.Object, StateMachine {
- public GUPnP.Context context;
- public Soup.Message msg;
- public size_t total_bytes;
-
- public Cancellable cancellable { get; set; }
-
- public HTTPClient (GUPnP.Context context,
- string uri,
- size_t total_bytes,
- bool active) {
- this.context = context;
- this.total_bytes = total_bytes;
-
- this.msg = new Soup.Message ("HTTP", uri);
- assert (this.msg != null);
- this.msg.response_body.set_accumulate (false);
-
- if (active) {
- this.cancellable = new Cancellable ();
- this.cancellable.cancelled += this.on_cancelled;
- }
- }
-
- public async void run () {
- SourceFunc run_continue = run.callback;
- size_t bytes_received = 0;
-
- this.msg.got_chunk.connect ((msg, chunk) => {
- bytes_received += chunk.length;
-
- if (bytes_received >= this.total_bytes &&
- this.cancellable != null) {
- bytes_received = bytes_received.clamp (0, this.total_bytes);
-
- this.cancellable.cancel ();
- }
- });
-
- this.context.session.queue_message (this.msg, (session, msg) => {
- assert (bytes_received == this.total_bytes);
-
- run_continue ();
- });
-
- yield;
-
- this.completed ();
+ base.abort ();
}
- private void on_cancelled (Cancellable cancellable) {
- this.context.session.cancel_message (this.msg,
- KnownStatusCode.CANCELLED);
- this.completed ();
+ internal override HTTPResponse create_response (Soup.Message msg)
+ throws Error {
+ return new LiveResponse (this.server.context.server,
+ msg,
+ "TestingLiveResponse",
+ this.src,
+ null,
+ this.cancellable);
}
}
diff --git a/tests/rygel-seekable-response-test.vala b/tests/rygel-seekable-response-test.vala
index 0f38311..3d555f4 100644
--- a/tests/rygel-seekable-response-test.vala
+++ b/tests/rygel-seekable-response-test.vala
@@ -24,11 +24,6 @@
using Soup;
using Gst;
-public errordomain Rygel.TestError {
- SKIP = 77,
- TIMEOUT
-}
-
public class Rygel.HTTPSeek : GLib.Object {
public int64 start { get; private set; }
public int64 stop { get; private set; }
@@ -42,25 +37,12 @@ public class Rygel.HTTPSeek : GLib.Object {
}
}
-public class Rygel.SeekableResponseTest : GLib.Object {
- private static const long MAX_BYTES = 1024;
+public class Rygel.SeekableResponseTest : Rygel.HTTPResponseTest {
private static string URI = "file:///tmp/rygel-dummy-test-file";
- private HTTPServer server;
- private HTTPClient client;
private File dummy_file;
- private bool server_done;
- private bool client_done;
-
- private MainLoop main_loop;
-
- private Cancellable cancellable;
- private Error error;
-
public static int main (string[] args) {
- Gst.init (ref args);
-
try {
var test = new SeekableResponseTest.complete ();
test.run ();
@@ -80,43 +62,21 @@ public class Rygel.SeekableResponseTest : GLib.Object {
private SeekableResponseTest (Cancellable? cancellable = null)
throws Error {
- this.cancellable = cancellable;
-
- this.server = new HTTPServer ();
- this.client = new HTTPClient (this.server.context,
- this.server.uri,
- MAX_BYTES,
- cancellable != null);
- this.main_loop = new MainLoop (null, false);
+ base (cancellable);
}
private SeekableResponseTest.complete () throws Error {
- this ();
+ base.complete ();
}
private SeekableResponseTest.abort () throws Error {
- this (new Cancellable ());
+ base.abort ();
}
- private void run () throws Error {
+ public override void run () throws Error {
this.create_dummy_file ();
- Timeout.add_seconds (3, this.on_timeout);
- this.server.message_received.connect (this.on_message_received);
- this.server.message_aborted.connect (this.on_message_aborted);
- if (this.cancellable == null) {
- this.client.completed.connect (this.on_client_completed);
- } else {
- this.client_done = true;
- }
-
- this.client.run.begin ();
-
- this.main_loop.run ();
-
- if (this.error != null) {
- throw this.error;
- }
+ base.run ();
this.dummy_file.delete (null);
}
@@ -129,158 +89,15 @@ public class Rygel.SeekableResponseTest : GLib.Object {
stream.write (new char[1024], 1024, null);
}
- private void on_client_completed (StateMachine client) {
- if (this.server_done) {
- this.main_loop.quit ();
- }
-
- this.client_done = true;
- }
-
- private void on_response_completed (StateMachine response) {
- if (this.client_done) {
- this.main_loop.quit ();
- }
-
- this.server_done = true;
- }
-
- private void on_message_received (HTTPServer server,
- Soup.Message msg) {
- try {
- var seek = new HTTPSeek (0, 1025);
- var response = new SeekableResponse (
- server.context.server,
- msg,
- this.dummy_file.get_uri (),
- seek,
- 1024,
- this.cancellable);
-
- response.run.begin ();
-
- response.completed.connect (this.on_response_completed);
- } catch (Error error) {
- this.error = error;
- this.main_loop.quit ();
-
- return;
- }
- }
-
- private void on_message_aborted (HTTPServer server,
- Soup.Message msg) {
- this.cancellable.cancel ();
- }
-
- private bool on_timeout () {
- this.error = new TestError.TIMEOUT ("Timeout");
- this.main_loop.quit ();
-
- return false;
- }
-}
-
-private class Rygel.HTTPServer : GLib.Object {
- private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
-
- public GUPnP.Context context;
-
- public string uri {
- owned get { return "http://" +
- this.context.host_ip + ":" +
- this.context.port.to_string () +
- SERVER_PATH;
- }
- }
-
- public signal void message_received (Soup.Message message);
- public signal void message_aborted (Soup.Message message);
-
- public HTTPServer () throws TestError {
- try {
- this.context = new GUPnP.Context (null, "lo", 0);
- } catch (Error error) {
- throw new TestError.SKIP ("Network context not available");
- }
-
- assert (this.context != null);
- assert (this.context.host_ip != null);
- assert (this.context.port > 0);
-
- this.context.server.add_handler (SERVER_PATH, this.server_cb);
- this.context.server.request_aborted.connect (this.on_request_aborted);
- }
-
- private void server_cb (Server server,
- Soup.Message msg,
- string path,
- HashTable? query,
- ClientContext client) {
- this.context.server.pause_message (msg);
- this.message_received (msg);
- }
-
- private void on_request_aborted (Soup.Server server,
- Soup.Message message,
- Soup.ClientContext client) {
- this.message_aborted (message);
- }
-}
-
-private class Rygel.HTTPClient : GLib.Object, StateMachine {
- public GUPnP.Context context;
- public Soup.Message msg;
- public size_t total_bytes;
-
- public Cancellable cancellable { get; set; }
-
- public HTTPClient (GUPnP.Context context,
- string uri,
- size_t total_bytes,
- bool active) {
- this.context = context;
- this.total_bytes = total_bytes;
-
- this.msg = new Soup.Message ("HTTP", uri);
- assert (this.msg != null);
- this.msg.response_body.set_accumulate (false);
-
- if (active) {
- this.cancellable = new Cancellable ();
- this.cancellable.cancelled += this.on_cancelled;
- }
- }
-
- public async void run () {
- SourceFunc run_continue = run.callback;
- size_t bytes_received = 0;
-
- this.msg.got_chunk.connect ((msg, chunk) => {
- bytes_received += chunk.length;
-
- if (bytes_received >= this.total_bytes &&
- this.cancellable != null) {
- bytes_received = bytes_received.clamp (0, this.total_bytes);
-
- this.cancellable.cancel ();
- }
- });
-
- this.context.session.queue_message (this.msg, (session, msg) => {
- assert (bytes_received == this.total_bytes);
-
- run_continue ();
- });
-
- yield;
-
- this.completed ();
- }
+ internal override HTTPResponse create_response (Soup.Message msg)
+ throws Error {
+ var seek = new HTTPSeek (0, 1025);
- private void on_cancelled (Cancellable cancellable) {
- this.context.session.cancel_message (this.msg,
- KnownStatusCode.CANCELLED);
- this.completed ();
+ return new SeekableResponse (this.server.context.server,
+ msg,
+ this.dummy_file.get_uri (),
+ seek,
+ 1024,
+ this.cancellable);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]