Release 0.41.92
[vala-gnome.git] / tests / dbus / async.test
blobcb6a541fc1e8031f0e9005a109caa0f5d8b3fea2
1 Packages: gio-2.0
2 D-Bus
4 Program: client
6 [DBus (name = "org.example.Test")]
7 interface Test : Object {
8         public abstract async void test_void () throws IOError;
9         public abstract async int test_int (int i, out int j) throws IOError;
10         public abstract async string test_string (string s, out string t) throws IOError;
13 MainLoop main_loop;
15 async void run () {
16         Test test = yield Bus.get_proxy (BusType.SESSION, "org.example.Test", "/org/example/test");
18         yield test.test_void ();
20         int j, k;
21         k = yield test.test_int (42, out j);
22         assert (j == 23);
23         assert (k == 11);
25         string t, u;
26         u = yield test.test_string ("hello", out t);
27         assert (t == "world");
28         assert (u == "vala");
30         main_loop.quit ();
33 void main () {
34         // client
35         run.begin ();
37         main_loop = new MainLoop (null, false);
38         main_loop.run ();
41 Program: server
43 [DBus (name = "org.example.Test")]
44 class Test : Object {
45         public async void test_void () {
46                 Idle.add (test_void.callback);
47                 yield;
48         }
50         public async int test_int (int i, out int j) {
51                 assert (i == 42);
52                 Idle.add (test_int.callback);
53                 yield;
54                 j = 23;
55                 return 11;
56         }
58         public async string test_string (string s, out string t) {
59                 assert (s == "hello");
60                 Idle.add (test_string.callback);
61                 yield;
62                 t = "world";
63                 return "vala";
64         }
67 MainLoop main_loop;
69 void client_exit (Pid pid, int status) {
70         // client finished, terminate server
71         assert (status == 0);
72         main_loop.quit ();
75 void main () {
76         var conn = Bus.get_sync (BusType.SESSION);
77         conn.register_object ("/org/example/test", new Test ());
79         // try to register service in session bus
80         var request_result = conn.call_sync ("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName",
81                                               new Variant ("(su)", "org.example.Test", 0x4), null, 0, -1);
82         assert ((uint) request_result.get_child_value (0) == 1);
84         // server ready, spawn client
85         Pid client_pid;
86         Process.spawn_async (null, { "test", "/dbus/async/client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid);
87         ChildWatch.add (client_pid, client_exit);
89         main_loop = new MainLoop ();
90         main_loop.run ();