2017-01-03 20:16:06 +11:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016 Michael Gratton <mike@vee.net>
|
|
|
|
|
*
|
|
|
|
|
* This software is licensed under the GNU Lesser General Public License
|
|
|
|
|
* (version 2.1 or later). See the COPYING file in this distribution.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-09 11:58:02 +11:00
|
|
|
class Geary.TimeoutManagerTest : TestCase {
|
2017-01-03 20:16:06 +11:00
|
|
|
|
|
|
|
|
// add_seconds seems to vary wildly, so needs a large epsilon
|
|
|
|
|
private const double SECONDS_EPSILON = 1.8;
|
|
|
|
|
|
2017-01-17 16:27:42 +11:00
|
|
|
private const double MILLISECONDS_EPSILON = 0.1;
|
|
|
|
|
|
2019-02-16 15:58:08 +11:00
|
|
|
|
|
|
|
|
private class WeakRefTest : GLib.Object {
|
|
|
|
|
|
|
|
|
|
public TimeoutManager test { get; private set; }
|
|
|
|
|
|
|
|
|
|
public WeakRefTest() { // Pass in an arg to ensure the closure is non-trivial
|
|
|
|
|
string arg = "my hovercraft is full of eels";
|
|
|
|
|
this.test = new TimeoutManager.milliseconds(
|
|
|
|
|
10, () => {
|
|
|
|
|
do_stuff(arg);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Pass
|
|
|
|
|
this.test.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void do_stuff(string arg) {
|
|
|
|
|
// This should never get called
|
2020-05-09 16:04:22 +10:00
|
|
|
GLib.assert(false);
|
2019-02-16 15:58:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-03 20:16:06 +11:00
|
|
|
public TimeoutManagerTest() {
|
|
|
|
|
base("Geary.TimeoutManagerTest");
|
2019-02-16 15:58:08 +11:00
|
|
|
add_test("weak_ref", callback_weak_ref);
|
2017-01-03 20:16:06 +11:00
|
|
|
add_test("start_reset", start_reset);
|
2017-01-09 09:21:09 +11:00
|
|
|
if (Test.slow()) {
|
2018-03-07 17:30:14 +11:00
|
|
|
add_test("seconds", seconds);
|
|
|
|
|
add_test("milliseconds", milliseconds);
|
|
|
|
|
add_test("repeat_forever", repeat_forever);
|
2017-01-09 09:21:09 +11:00
|
|
|
}
|
2017-01-03 20:16:06 +11:00
|
|
|
}
|
|
|
|
|
|
2019-02-16 15:58:08 +11:00
|
|
|
public void callback_weak_ref() throws GLib.Error {
|
|
|
|
|
WeakRefTest? owner = new WeakRefTest();
|
|
|
|
|
double duration = owner.test.interval;
|
|
|
|
|
GLib.WeakRef weak_ref = GLib.WeakRef(owner.test);
|
|
|
|
|
|
|
|
|
|
// Should make both objects null even though the even loop
|
|
|
|
|
// hasn't run and hence the callback hasn't been called.
|
|
|
|
|
owner = null;
|
|
|
|
|
assert_null(weak_ref.get());
|
|
|
|
|
|
|
|
|
|
// Pump the loop until the timeout has passed so that the
|
|
|
|
|
// callback can get called.
|
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
timer.start();
|
|
|
|
|
while (timer.elapsed() < (duration / 1000) * 2) {
|
|
|
|
|
this.main_loop.iteration(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:30:14 +11:00
|
|
|
public void start_reset() throws Error {
|
2017-01-03 20:16:06 +11:00
|
|
|
TimeoutManager test = new TimeoutManager.seconds(1, () => { /* noop */ });
|
|
|
|
|
assert(!test.is_running);
|
|
|
|
|
test.start();
|
|
|
|
|
assert(test.is_running);
|
|
|
|
|
test.reset();
|
|
|
|
|
assert(!test.is_running);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:30:14 +11:00
|
|
|
public void seconds() throws Error {
|
2017-01-03 20:16:06 +11:00
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
|
|
|
|
TimeoutManager test = new TimeoutManager.seconds(1, () => { timer.stop(); });
|
|
|
|
|
test.start();
|
|
|
|
|
|
|
|
|
|
timer.start();
|
|
|
|
|
while (test.is_running && timer.elapsed() < SECONDS_EPSILON) {
|
2017-11-23 09:35:51 +11:00
|
|
|
this.main_loop.iteration(true);
|
2017-01-03 20:16:06 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 16:04:22 +10:00
|
|
|
assert_within(timer.elapsed(), 1.0, SECONDS_EPSILON);
|
2017-01-03 20:16:06 +11:00
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:30:14 +11:00
|
|
|
public void milliseconds() throws Error {
|
2017-01-17 16:27:42 +11:00
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
|
|
|
|
TimeoutManager test = new TimeoutManager.milliseconds(100, () => { timer.stop(); });
|
|
|
|
|
test.start();
|
|
|
|
|
|
|
|
|
|
timer.start();
|
|
|
|
|
while (test.is_running && timer.elapsed() < 100 + MILLISECONDS_EPSILON) {
|
2017-11-23 09:35:51 +11:00
|
|
|
this.main_loop.iteration(true);
|
2017-01-17 16:27:42 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 16:04:22 +10:00
|
|
|
assert_within(timer.elapsed(), 0.1, MILLISECONDS_EPSILON);
|
2017-01-17 16:27:42 +11:00
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:30:14 +11:00
|
|
|
public void repeat_forever() throws Error {
|
2017-01-03 20:16:06 +11:00
|
|
|
Timer timer = new Timer();
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
TimeoutManager test = new TimeoutManager.seconds(1, () => { count++; });
|
|
|
|
|
test.repetition = TimeoutManager.Repeat.FOREVER;
|
|
|
|
|
test.start();
|
|
|
|
|
|
|
|
|
|
timer.start();
|
|
|
|
|
while (count < 2 && timer.elapsed() < SECONDS_EPSILON * 2) {
|
2017-11-23 09:35:51 +11:00
|
|
|
this.main_loop.iteration(true);
|
2017-01-03 20:16:06 +11:00
|
|
|
}
|
|
|
|
|
timer.stop();
|
|
|
|
|
|
2020-05-09 16:04:22 +10:00
|
|
|
assert_within(timer.elapsed(), 2.0, SECONDS_EPSILON * 2);
|
2017-01-03 20:16:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|