Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Blockjob tests |
| 3 | * |
| 4 | * Copyright Igalia, S.L. 2016 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Alberto Garcia <berto@igalia.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "qapi/error.h" |
| 15 | #include "qemu/main-loop.h" |
John Snow | c87621e | 2016-10-27 12:07:00 -0400 | [diff] [blame] | 16 | #include "block/blockjob_int.h" |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 17 | #include "sysemu/block-backend.h" |
| 18 | |
| 19 | static const BlockJobDriver test_block_job_driver = { |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 20 | .job_driver = { |
| 21 | .instance_size = sizeof(BlockJob), |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 22 | .free = block_job_free, |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 23 | .user_resume = block_job_user_resume, |
Kevin Wolf | b69f777 | 2018-04-20 17:00:29 +0200 | [diff] [blame] | 24 | .drain = block_job_drain, |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 25 | }, |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static void block_job_cb(void *opaque, int ret) |
| 29 | { |
| 30 | } |
| 31 | |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 32 | static BlockJob *mk_job(BlockBackend *blk, const char *id, |
| 33 | const BlockJobDriver *drv, bool should_succeed, |
| 34 | int flags) |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 35 | { |
| 36 | BlockJob *job; |
| 37 | Error *errp = NULL; |
| 38 | |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 39 | job = block_job_create(id, drv, NULL, blk_bs(blk), |
| 40 | 0, BLK_PERM_ALL, 0, flags, block_job_cb, |
Kevin Wolf | c6cc12b | 2017-01-16 17:18:09 +0100 | [diff] [blame] | 41 | NULL, &errp); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 42 | if (should_succeed) { |
| 43 | g_assert_null(errp); |
| 44 | g_assert_nonnull(job); |
| 45 | if (id) { |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 46 | g_assert_cmpstr(job->job.id, ==, id); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 47 | } else { |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 48 | g_assert_cmpstr(job->job.id, ==, blk_name(blk)); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 49 | } |
| 50 | } else { |
| 51 | g_assert_nonnull(errp); |
| 52 | g_assert_null(job); |
| 53 | error_free(errp); |
| 54 | } |
| 55 | |
| 56 | return job; |
| 57 | } |
| 58 | |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 59 | static BlockJob *do_test_id(BlockBackend *blk, const char *id, |
| 60 | bool should_succeed) |
| 61 | { |
| 62 | return mk_job(blk, id, &test_block_job_driver, |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 63 | should_succeed, JOB_DEFAULT); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 66 | /* This creates a BlockBackend (optionally with a name) with a |
| 67 | * BlockDriverState inserted. */ |
| 68 | static BlockBackend *create_blk(const char *name) |
| 69 | { |
Kevin Wolf | 2807c0c | 2017-02-09 15:48:04 +0100 | [diff] [blame] | 70 | /* No I/O is performed on this device */ |
Kevin Wolf | 6d0eb64 | 2017-01-20 17:07:26 +0100 | [diff] [blame] | 71 | BlockBackend *blk = blk_new(0, BLK_PERM_ALL); |
Kevin Wolf | d185cf0 | 2017-01-16 17:17:38 +0100 | [diff] [blame] | 72 | BlockDriverState *bs; |
| 73 | |
| 74 | bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort); |
| 75 | g_assert_nonnull(bs); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 76 | |
Kevin Wolf | d708642 | 2017-01-13 19:02:32 +0100 | [diff] [blame] | 77 | blk_insert_bs(blk, bs, &error_abort); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 78 | bdrv_unref(bs); |
| 79 | |
| 80 | if (name) { |
| 81 | Error *errp = NULL; |
| 82 | monitor_add_blk(blk, name, &errp); |
| 83 | g_assert_null(errp); |
| 84 | } |
| 85 | |
| 86 | return blk; |
| 87 | } |
| 88 | |
| 89 | /* This destroys the backend */ |
| 90 | static void destroy_blk(BlockBackend *blk) |
| 91 | { |
| 92 | if (blk_name(blk)[0] != '\0') { |
| 93 | monitor_remove_blk(blk); |
| 94 | } |
| 95 | |
| 96 | blk_remove_bs(blk); |
| 97 | blk_unref(blk); |
| 98 | } |
| 99 | |
| 100 | static void test_job_ids(void) |
| 101 | { |
| 102 | BlockBackend *blk[3]; |
| 103 | BlockJob *job[3]; |
| 104 | |
| 105 | blk[0] = create_blk(NULL); |
| 106 | blk[1] = create_blk("drive1"); |
| 107 | blk[2] = create_blk("drive2"); |
| 108 | |
| 109 | /* No job ID provided and the block backend has no name */ |
| 110 | job[0] = do_test_id(blk[0], NULL, false); |
| 111 | |
| 112 | /* These are all invalid job IDs */ |
| 113 | job[0] = do_test_id(blk[0], "0id", false); |
| 114 | job[0] = do_test_id(blk[0], "", false); |
| 115 | job[0] = do_test_id(blk[0], " ", false); |
| 116 | job[0] = do_test_id(blk[0], "123", false); |
| 117 | job[0] = do_test_id(blk[0], "_id", false); |
| 118 | job[0] = do_test_id(blk[0], "-id", false); |
| 119 | job[0] = do_test_id(blk[0], ".id", false); |
| 120 | job[0] = do_test_id(blk[0], "#id", false); |
| 121 | |
| 122 | /* This one is valid */ |
| 123 | job[0] = do_test_id(blk[0], "id0", true); |
| 124 | |
| 125 | /* We cannot have two jobs in the same BDS */ |
| 126 | do_test_id(blk[0], "id1", false); |
| 127 | |
| 128 | /* Duplicate job IDs are not allowed */ |
| 129 | job[1] = do_test_id(blk[1], "id0", false); |
| 130 | |
| 131 | /* But once job[0] finishes we can reuse its ID */ |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 132 | job_early_fail(&job[0]->job); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 133 | job[1] = do_test_id(blk[1], "id0", true); |
| 134 | |
| 135 | /* No job ID specified, defaults to the backend name ('drive1') */ |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 136 | job_early_fail(&job[1]->job); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 137 | job[1] = do_test_id(blk[1], NULL, true); |
| 138 | |
| 139 | /* Duplicate job ID */ |
| 140 | job[2] = do_test_id(blk[2], "drive1", false); |
| 141 | |
| 142 | /* The ID of job[2] would default to 'drive2' but it is already in use */ |
| 143 | job[0] = do_test_id(blk[0], "drive2", true); |
| 144 | job[2] = do_test_id(blk[2], NULL, false); |
| 145 | |
| 146 | /* This one is valid */ |
| 147 | job[2] = do_test_id(blk[2], "id_2", true); |
| 148 | |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 149 | job_early_fail(&job[0]->job); |
| 150 | job_early_fail(&job[1]->job); |
| 151 | job_early_fail(&job[2]->job); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 152 | |
| 153 | destroy_blk(blk[0]); |
| 154 | destroy_blk(blk[1]); |
| 155 | destroy_blk(blk[2]); |
| 156 | } |
| 157 | |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 158 | typedef struct CancelJob { |
| 159 | BlockJob common; |
| 160 | BlockBackend *blk; |
| 161 | bool should_converge; |
| 162 | bool should_complete; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 163 | } CancelJob; |
| 164 | |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 165 | static void cancel_job_complete(Job *job, Error **errp) |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 166 | { |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 167 | CancelJob *s = container_of(job, CancelJob, common.job); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 168 | s->should_complete = true; |
| 169 | } |
| 170 | |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 171 | static int coroutine_fn cancel_job_run(Job *job, Error **errp) |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 172 | { |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 173 | CancelJob *s = container_of(job, CancelJob, common.job); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 174 | |
| 175 | while (!s->should_complete) { |
Kevin Wolf | daa7f2f | 2018-04-17 12:56:07 +0200 | [diff] [blame] | 176 | if (job_is_cancelled(&s->common.job)) { |
John Snow | eb23654 | 2018-08-29 21:57:31 -0400 | [diff] [blame] | 177 | return 0; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 180 | if (!job_is_ready(&s->common.job) && s->should_converge) { |
Kevin Wolf | 2e1795b | 2018-04-25 14:56:09 +0200 | [diff] [blame] | 181 | job_transition_to_ready(&s->common.job); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 184 | job_sleep_ns(&s->common.job, 100000); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 185 | } |
| 186 | |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 187 | return 0; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static const BlockJobDriver test_cancel_driver = { |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 191 | .job_driver = { |
| 192 | .instance_size = sizeof(CancelJob), |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 193 | .free = block_job_free, |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 194 | .user_resume = block_job_user_resume, |
Kevin Wolf | b69f777 | 2018-04-20 17:00:29 +0200 | [diff] [blame] | 195 | .drain = block_job_drain, |
John Snow | f67432a | 2018-08-29 21:57:26 -0400 | [diff] [blame] | 196 | .run = cancel_job_run, |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 197 | .complete = cancel_job_complete, |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 198 | }, |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 199 | }; |
| 200 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 201 | static CancelJob *create_common(Job **pjob) |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 202 | { |
| 203 | BlockBackend *blk; |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 204 | Job *job; |
| 205 | BlockJob *bjob; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 206 | CancelJob *s; |
| 207 | |
| 208 | blk = create_blk(NULL); |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 209 | bjob = mk_job(blk, "Steve", &test_cancel_driver, true, |
| 210 | JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS); |
| 211 | job = &bjob->job; |
| 212 | job_ref(job); |
| 213 | assert(job->status == JOB_STATUS_CREATED); |
| 214 | s = container_of(bjob, CancelJob, common); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 215 | s->blk = blk; |
| 216 | |
| 217 | *pjob = job; |
| 218 | return s; |
| 219 | } |
| 220 | |
| 221 | static void cancel_common(CancelJob *s) |
| 222 | { |
| 223 | BlockJob *job = &s->common; |
| 224 | BlockBackend *blk = s->blk; |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 225 | JobStatus sts = job->job.status; |
Kevin Wolf | 30c070a | 2018-08-17 17:29:08 +0200 | [diff] [blame] | 226 | AioContext *ctx; |
| 227 | |
| 228 | ctx = job->job.aio_context; |
| 229 | aio_context_acquire(ctx); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 230 | |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 231 | job_cancel_sync(&job->job); |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 232 | if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) { |
Kevin Wolf | 5f9a6a0 | 2018-04-24 17:10:12 +0200 | [diff] [blame] | 233 | Job *dummy = &job->job; |
| 234 | job_dismiss(&dummy, &error_abort); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 235 | } |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 236 | assert(job->job.status == JOB_STATUS_NULL); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 237 | job_unref(&job->job); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 238 | destroy_blk(blk); |
Kevin Wolf | 30c070a | 2018-08-17 17:29:08 +0200 | [diff] [blame] | 239 | |
| 240 | aio_context_release(ctx); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void test_cancel_created(void) |
| 244 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 245 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 246 | CancelJob *s; |
| 247 | |
| 248 | s = create_common(&job); |
| 249 | cancel_common(s); |
| 250 | } |
| 251 | |
| 252 | static void test_cancel_running(void) |
| 253 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 254 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 255 | CancelJob *s; |
| 256 | |
| 257 | s = create_common(&job); |
| 258 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 259 | job_start(job); |
| 260 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 261 | |
| 262 | cancel_common(s); |
| 263 | } |
| 264 | |
| 265 | static void test_cancel_paused(void) |
| 266 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 267 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 268 | CancelJob *s; |
| 269 | |
| 270 | s = create_common(&job); |
| 271 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 272 | job_start(job); |
| 273 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 274 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 275 | job_user_pause(job, &error_abort); |
| 276 | job_enter(job); |
| 277 | assert(job->status == JOB_STATUS_PAUSED); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 278 | |
| 279 | cancel_common(s); |
| 280 | } |
| 281 | |
| 282 | static void test_cancel_ready(void) |
| 283 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 284 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 285 | CancelJob *s; |
| 286 | |
| 287 | s = create_common(&job); |
| 288 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 289 | job_start(job); |
| 290 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 291 | |
| 292 | s->should_converge = true; |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 293 | job_enter(job); |
| 294 | assert(job->status == JOB_STATUS_READY); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 295 | |
| 296 | cancel_common(s); |
| 297 | } |
| 298 | |
| 299 | static void test_cancel_standby(void) |
| 300 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 301 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 302 | CancelJob *s; |
| 303 | |
| 304 | s = create_common(&job); |
| 305 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 306 | job_start(job); |
| 307 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 308 | |
| 309 | s->should_converge = true; |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 310 | job_enter(job); |
| 311 | assert(job->status == JOB_STATUS_READY); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 312 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 313 | job_user_pause(job, &error_abort); |
| 314 | job_enter(job); |
| 315 | assert(job->status == JOB_STATUS_STANDBY); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 316 | |
| 317 | cancel_common(s); |
| 318 | } |
| 319 | |
| 320 | static void test_cancel_pending(void) |
| 321 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 322 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 323 | CancelJob *s; |
| 324 | |
| 325 | s = create_common(&job); |
| 326 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 327 | job_start(job); |
| 328 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 329 | |
| 330 | s->should_converge = true; |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 331 | job_enter(job); |
| 332 | assert(job->status == JOB_STATUS_READY); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 333 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 334 | job_complete(job, &error_abort); |
| 335 | job_enter(job); |
John Snow | 977d26f | 2018-09-06 09:02:18 -0400 | [diff] [blame] | 336 | while (!job->deferred_to_main_loop) { |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 337 | aio_poll(qemu_get_aio_context(), true); |
| 338 | } |
John Snow | 977d26f | 2018-09-06 09:02:18 -0400 | [diff] [blame] | 339 | assert(job->status == JOB_STATUS_READY); |
| 340 | aio_poll(qemu_get_aio_context(), true); |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 341 | assert(job->status == JOB_STATUS_PENDING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 342 | |
| 343 | cancel_common(s); |
| 344 | } |
| 345 | |
| 346 | static void test_cancel_concluded(void) |
| 347 | { |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 348 | Job *job; |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 349 | CancelJob *s; |
| 350 | |
| 351 | s = create_common(&job); |
| 352 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 353 | job_start(job); |
| 354 | assert(job->status == JOB_STATUS_RUNNING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 355 | |
| 356 | s->should_converge = true; |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 357 | job_enter(job); |
| 358 | assert(job->status == JOB_STATUS_READY); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 359 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 360 | job_complete(job, &error_abort); |
| 361 | job_enter(job); |
John Snow | 977d26f | 2018-09-06 09:02:18 -0400 | [diff] [blame] | 362 | while (!job->deferred_to_main_loop) { |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 363 | aio_poll(qemu_get_aio_context(), true); |
| 364 | } |
John Snow | 977d26f | 2018-09-06 09:02:18 -0400 | [diff] [blame] | 365 | assert(job->status == JOB_STATUS_READY); |
| 366 | aio_poll(qemu_get_aio_context(), true); |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 367 | assert(job->status == JOB_STATUS_PENDING); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 368 | |
John Snow | 0cc4643 | 2018-09-06 09:02:17 -0400 | [diff] [blame] | 369 | job_finalize(job, &error_abort); |
| 370 | assert(job->status == JOB_STATUS_CONCLUDED); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 371 | |
| 372 | cancel_common(s); |
| 373 | } |
| 374 | |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 375 | int main(int argc, char **argv) |
| 376 | { |
| 377 | qemu_init_main_loop(&error_abort); |
Kevin Wolf | d185cf0 | 2017-01-16 17:17:38 +0100 | [diff] [blame] | 378 | bdrv_init(); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 379 | |
| 380 | g_test_init(&argc, &argv, NULL); |
| 381 | g_test_add_func("/blockjob/ids", test_job_ids); |
John Snow | fb367e0 | 2018-03-10 03:27:46 -0500 | [diff] [blame] | 382 | g_test_add_func("/blockjob/cancel/created", test_cancel_created); |
| 383 | g_test_add_func("/blockjob/cancel/running", test_cancel_running); |
| 384 | g_test_add_func("/blockjob/cancel/paused", test_cancel_paused); |
| 385 | g_test_add_func("/blockjob/cancel/ready", test_cancel_ready); |
| 386 | g_test_add_func("/blockjob/cancel/standby", test_cancel_standby); |
| 387 | g_test_add_func("/blockjob/cancel/pending", test_cancel_pending); |
| 388 | g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded); |
Alberto Garcia | 9ef8112 | 2016-07-29 17:31:41 +0300 | [diff] [blame] | 389 | return g_test_run(); |
| 390 | } |