diff options
author | Marvin W <git@larma.de> | 2020-06-27 11:23:22 +0200 |
---|---|---|
committer | Marvin W <git@larma.de> | 2020-06-28 11:53:43 +0200 |
commit | 8f8018ec81d4ea6e6a5e2f3d811daa57a31f6122 (patch) | |
tree | 9bfcf2e8812293a80bdfefd6872ccb7e93a79536 /xmpp-vala/tests/testcase.vala | |
parent | 717d0b7fcaede5688a236af847d518be4a8f0c6b (diff) | |
download | dino-8f8018ec81d4ea6e6a5e2f3d811daa57a31f6122.tar.gz dino-8f8018ec81d4ea6e6a5e2f3d811daa57a31f6122.zip |
Fix async tests
Diffstat (limited to 'xmpp-vala/tests/testcase.vala')
-rw-r--r-- | xmpp-vala/tests/testcase.vala | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/xmpp-vala/tests/testcase.vala b/xmpp-vala/tests/testcase.vala index cff0c47e..5be56128 100644 --- a/xmpp-vala/tests/testcase.vala +++ b/xmpp-vala/tests/testcase.vala @@ -26,13 +26,25 @@ public abstract class Gee.TestCase : Object { private Adaptor[] adaptors = new Adaptor[0]; public delegate void TestMethod (); + public delegate void FinishCallback (); + public delegate void AsyncTestMethod (FinishCallback cb); protected TestCase (string name) { this.suite = new GLib.TestSuite (name); } public void add_test (string name, owned TestMethod test) { - var adaptor = new Adaptor (name, (owned)test, this); + var adaptor = new DefaultAdaptor (name, (owned)test, this); + this.adaptors += adaptor; + + this.suite.add (new GLib.TestCase (adaptor.name, + adaptor.set_up, + adaptor.run, + adaptor.tear_down )); + } + + public void add_async_test (string name, owned AsyncTestMethod test, int timeout = 10000) { + var adaptor = new AsyncAdaptor (name, (owned)test, this, timeout); this.adaptors += adaptor; this.suite.add (new GLib.TestCase (adaptor.name, @@ -51,13 +63,19 @@ public abstract class Gee.TestCase : Object { return this.suite; } - private class Adaptor { + private interface Adaptor : Object { + public abstract void set_up (void* fixture); + public abstract void run (void* fixture); + public abstract void tear_down (void* fixture); + } + + private class DefaultAdaptor : Object, Adaptor { [CCode (notify = false)] public string name { get; private set; } private TestMethod test; private TestCase test_case; - public Adaptor (string name, + public DefaultAdaptor (string name, owned TestMethod test, TestCase test_case) { this.name = name; @@ -77,4 +95,49 @@ public abstract class Gee.TestCase : Object { this.test_case.tear_down (); } } + + private class AsyncAdaptor : Object, Adaptor { + [CCode (notify = false)] + public string name { get; private set; } + private AsyncTestMethod test; + private TestCase test_case; + private MainLoop main_loop; + private int timeout; + + public AsyncAdaptor (string name, + owned AsyncTestMethod test, + TestCase test_case, + int timeout) { + this.name = name; + this.test = (owned)test; + this.test_case = test_case; + this.timeout = timeout; + } + + public void set_up (void* fixture) { + this.test_case.set_up (); + main_loop = new MainLoop (); + } + + public void run (void* fixture) { + this.test (finish); + Timeout.add (timeout, finish_timeout); + main_loop.run (); + } + + public void finish () { + Idle.add (() => { main_loop.quit (); return false; }); + } + + public bool finish_timeout () { + Test.fail (); + Test.message (@"Timeout of $(timeout)ms reached."); + main_loop.quit (); + return false; + } + + public void tear_down (void* fixture) { + this.test_case.tear_down (); + } + } } |