1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright 2017 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.
*/
errordomain AccountProcessorTestError {
TEST;
}
public class Geary.ImapEngine.AccountProcessorTest : TestCase {
public class TestOperation : AccountOperation {
public bool throw_error = false;
public bool wait_for_cancel = false;
public bool execute_called = false;
private Nonblocking.Spinlock spinlock = new Nonblocking.Spinlock();
internal TestOperation(Geary.Account account) {
base(account);
}
public override async void execute(Cancellable cancellable)
throws Error {
this.execute_called = true;
if (this.wait_for_cancel) {
yield this.spinlock.wait_async(cancellable);
}
if (this.throw_error) {
throw new AccountProcessorTestError.TEST("Failed");
}
}
}
public class OtherOperation : TestOperation {
internal OtherOperation(Geary.Account account) {
base(account);
}
}
private AccountProcessor? processor = null;
private Geary.Account? account = null;
private Geary.AccountInformation? info = null;
private uint succeeded;
private uint failed;
private uint completed;
public AccountProcessorTest() {
base("Geary.ImapEngine.AccountProcessorTest");
add_test("success", success);
add_test("failure", failure);
add_test("duplicate", duplicate);
add_test("stop", stop);
}
public override void set_up() {
this.info = new Geary.AccountInformation(
"test-info",
ServiceProvider.OTHER,
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
);
this.account = new Mock.Account(this.info);
this.processor = new AccountProcessor();
this.succeeded = 0;
this.failed = 0;
this.completed = 0;
}
public override void tear_down() {
this.processor.stop();
this.processor = null;
this.account = null;
this.info = null;
this.succeeded = 0;
this.failed = 0;
this.completed = 0;
}
public void success() throws Error {
TestOperation op = setup_operation(new TestOperation(this.account));
this.processor.enqueue(op);
assert(this.processor.waiting == 1);
execute_all();
assert(op.execute_called);
assert(this.succeeded == 1);
assert(this.failed == 0);
assert(this.completed == 1);
}
public void failure() throws Error {
TestOperation op = setup_operation(new TestOperation(this.account));
op.throw_error = true;
AccountOperation? error_op = null;
Error? error = null;
this.processor.operation_error.connect((proc, op, err) => {
error_op = op;
error = err;
});
this.processor.enqueue(op);
execute_all();
assert(this.succeeded == 0);
assert(this.failed == 1);
assert(this.completed == 1);
assert(error_op == op);
assert(error is AccountProcessorTestError.TEST);
}
public void duplicate() throws Error {
TestOperation op1 = setup_operation(new TestOperation(this.account));
TestOperation op2 = setup_operation(new TestOperation(this.account));
TestOperation op3 = setup_operation(new OtherOperation(this.account));
this.processor.enqueue(op1);
this.processor.enqueue(op2);
assert(this.processor.waiting == 1);
this.processor.enqueue(op3);
assert(this.processor.waiting == 2);
}
public void stop() throws Error {
TestOperation op1 = setup_operation(new TestOperation(this.account));
op1.wait_for_cancel = true;
TestOperation op2 = setup_operation(new OtherOperation(this.account));
this.processor.enqueue(op1);
this.processor.enqueue(op2);
while (!this.processor.is_executing) {
this.main_loop.iteration(true);
}
this.processor.stop();
while (this.main_loop.pending()) {
this.main_loop.iteration(true);
}
assert(!this.processor.is_executing);
assert(this.processor.waiting == 0);
assert(this.succeeded == 0);
assert(this.failed == 1);
assert(this.completed == 1);
}
private TestOperation setup_operation(TestOperation op) {
op.succeeded.connect(() => {
this.succeeded++;
});
op.failed.connect(() => {
this.failed++;
});
op.completed.connect(() => {
this.completed++;
});
return op;
}
private void execute_all() {
while (this.processor.is_executing || this.processor.waiting > 0) {
this.main_loop.iteration(true);
}
}
}
|