[vala] D-Bus: Test async client and server support



commit 1c75e80762a780f8585925177331378eadee042d
Author: Jürg Billeter <j bitron ch>
Date:   Sun Sep 13 18:10:14 2009 +0200

    D-Bus: Test async client and server support

 tests/Makefile.am     |    1 +
 tests/dbus/async.test |   89 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ad9bae5..a3f0535 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -45,6 +45,7 @@ TESTS = \
 	errors/errors.test \
 	dbus/basic-types.test \
 	dbus/arrays.test \
+	dbus/async.test \
 	$(NULL)
 
 EXTRA_DIST = \
diff --git a/tests/dbus/async.test b/tests/dbus/async.test
new file mode 100644
index 0000000..74e5346
--- /dev/null
+++ b/tests/dbus/async.test
@@ -0,0 +1,89 @@
+Packages: gio-2.0 dbus-glib-1
+
+Program: client
+
+[DBus (name = "org.example.Test")]
+interface Test : Object {
+	public abstract async int test_int (int i);
+	public abstract async string test_string (string s);
+}
+
+MainLoop main_loop;
+
+async void run (Test test) {
+	int k;
+	k = yield test.test_int (42);
+	assert (k == 11);
+
+	string u;
+	u = yield test.test_string ("hello");
+	assert (u == "vala");
+
+	main_loop.quit ();
+}
+
+void main () {
+	var conn = DBus.Bus.get (DBus.BusType.SESSION);
+
+	// client
+	var test = (Test) conn.get_object ("org.example.Test", "/org/example/test");
+
+	run.begin (test);
+
+	main_loop = new MainLoop (null, false);
+	main_loop.run ();
+}
+
+Program: server
+
+[DBus (name = "org.example.Test")]
+class Test : Object {
+	public async int test_int (int i) {
+		assert (i == 42);
+		Idle.add (test_int.callback);
+		yield;
+		return 11;
+	}
+
+	public async string test_string (string s) {
+		assert (s == "hello");
+		Idle.add (test_string.callback);
+		yield;
+		return "vala";
+	}
+}
+
+MainLoop main_loop;
+int exit_status;
+
+void client_exit (Pid pid, int status) {
+	// client finished, terminate server
+	if (status != 0) {
+		exit_status = 1;
+	}
+	main_loop.quit ();
+}
+
+int main () {
+	var conn = DBus.Bus.get (DBus.BusType.SESSION);
+	dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
+
+	// try to register service in session bus
+	uint request_name_result = bus.request_name ("org.example.Test", (uint) 0);
+	if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
+		// start server
+		var server = new Test ();
+		conn.register_object ("/org/example/test", server);
+
+		// server ready, spawn client
+		Pid client_pid;
+		Process.spawn_async (null, { "client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid);
+		ChildWatch.add (client_pid, client_exit);
+
+		main_loop = new MainLoop (null, false);
+		main_loop.run ();
+	} else {
+		exit_status = 1;
+	}
+	return exit_status;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]