jobs: use job locks also in the unit tests

Add missing job synchronization in the unit tests, with
explicit locks.

We are deliberately using _locked functions wrapped by a guard
instead of a normal call because the normal call will be removed
in future, as the only usage is limited to the tests.

In other words, if a function like job_pause() is/will be only used
in tests to avoid:

WITH_JOB_LOCK_GUARD(){
    job_pause_locked();
}

then it is not worth keeping job_pause(), and just use the guard.

Note: at this stage, job_{lock/unlock} and job lock guard macros
are *nop*.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20220926093214.506243-10-eesposit@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Emanuele Giuseppe Esposito 2022-09-26 05:32:02 -04:00 committed by Kevin Wolf
parent 9624112441
commit 191e7af394
4 changed files with 140 additions and 83 deletions

View file

@ -211,8 +211,11 @@ static CancelJob *create_common(Job **pjob)
bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
job = &bjob->job;
job_ref(job);
assert(job->status == JOB_STATUS_CREATED);
WITH_JOB_LOCK_GUARD() {
job_ref_locked(job);
assert(job->status == JOB_STATUS_CREATED);
}
s = container_of(bjob, CancelJob, common);
s->blk = blk;
@ -231,12 +234,14 @@ static void cancel_common(CancelJob *s)
aio_context_acquire(ctx);
job_cancel_sync(&job->job, true);
if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
Job *dummy = &job->job;
job_dismiss(&dummy, &error_abort);
WITH_JOB_LOCK_GUARD() {
if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
Job *dummy = &job->job;
job_dismiss_locked(&dummy, &error_abort);
}
assert(job->job.status == JOB_STATUS_NULL);
job_unref_locked(&job->job);
}
assert(job->job.status == JOB_STATUS_NULL);
job_unref(&job->job);
destroy_blk(blk);
aio_context_release(ctx);
@ -251,6 +256,13 @@ static void test_cancel_created(void)
cancel_common(s);
}
static void assert_job_status_is(Job *job, int status)
{
WITH_JOB_LOCK_GUARD() {
assert(job->status == status);
}
}
static void test_cancel_running(void)
{
Job *job;
@ -259,7 +271,7 @@ static void test_cancel_running(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
assert_job_status_is(job, JOB_STATUS_RUNNING);
cancel_common(s);
}
@ -272,11 +284,12 @@ static void test_cancel_paused(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
job_user_pause(job, &error_abort);
WITH_JOB_LOCK_GUARD() {
assert(job->status == JOB_STATUS_RUNNING);
job_user_pause_locked(job, &error_abort);
}
job_enter(job);
assert(job->status == JOB_STATUS_PAUSED);
assert_job_status_is(job, JOB_STATUS_PAUSED);
cancel_common(s);
}
@ -289,11 +302,11 @@ static void test_cancel_ready(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
assert_job_status_is(job, JOB_STATUS_RUNNING);
s->should_converge = true;
job_enter(job);
assert(job->status == JOB_STATUS_READY);
assert_job_status_is(job, JOB_STATUS_READY);
cancel_common(s);
}
@ -306,15 +319,16 @@ static void test_cancel_standby(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
assert_job_status_is(job, JOB_STATUS_RUNNING);
s->should_converge = true;
job_enter(job);
assert(job->status == JOB_STATUS_READY);
job_user_pause(job, &error_abort);
WITH_JOB_LOCK_GUARD() {
assert(job->status == JOB_STATUS_READY);
job_user_pause_locked(job, &error_abort);
}
job_enter(job);
assert(job->status == JOB_STATUS_STANDBY);
assert_job_status_is(job, JOB_STATUS_STANDBY);
cancel_common(s);
}
@ -327,20 +341,21 @@ static void test_cancel_pending(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
assert_job_status_is(job, JOB_STATUS_RUNNING);
s->should_converge = true;
job_enter(job);
assert(job->status == JOB_STATUS_READY);
job_complete(job, &error_abort);
WITH_JOB_LOCK_GUARD() {
assert(job->status == JOB_STATUS_READY);
job_complete_locked(job, &error_abort);
}
job_enter(job);
while (!job->deferred_to_main_loop) {
aio_poll(qemu_get_aio_context(), true);
}
assert(job->status == JOB_STATUS_READY);
assert_job_status_is(job, JOB_STATUS_READY);
aio_poll(qemu_get_aio_context(), true);
assert(job->status == JOB_STATUS_PENDING);
assert_job_status_is(job, JOB_STATUS_PENDING);
cancel_common(s);
}
@ -353,25 +368,28 @@ static void test_cancel_concluded(void)
s = create_common(&job);
job_start(job);
assert(job->status == JOB_STATUS_RUNNING);
assert_job_status_is(job, JOB_STATUS_RUNNING);
s->should_converge = true;
job_enter(job);
assert(job->status == JOB_STATUS_READY);
job_complete(job, &error_abort);
WITH_JOB_LOCK_GUARD() {
assert(job->status == JOB_STATUS_READY);
job_complete_locked(job, &error_abort);
}
job_enter(job);
while (!job->deferred_to_main_loop) {
aio_poll(qemu_get_aio_context(), true);
}
assert(job->status == JOB_STATUS_READY);
assert_job_status_is(job, JOB_STATUS_READY);
aio_poll(qemu_get_aio_context(), true);
assert(job->status == JOB_STATUS_PENDING);
assert_job_status_is(job, JOB_STATUS_PENDING);
aio_context_acquire(job->aio_context);
job_finalize(job, &error_abort);
WITH_JOB_LOCK_GUARD() {
job_finalize_locked(job, &error_abort);
}
aio_context_release(job->aio_context);
assert(job->status == JOB_STATUS_CONCLUDED);
assert_job_status_is(job, JOB_STATUS_CONCLUDED);
cancel_common(s);
}
@ -459,36 +477,43 @@ static void test_complete_in_standby(void)
bjob = mk_job(blk, "job", &test_yielding_driver, true,
JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
job = &bjob->job;
assert(job->status == JOB_STATUS_CREATED);
assert_job_status_is(job, JOB_STATUS_CREATED);
/* Wait for the job to become READY */
job_start(job);
aio_context_acquire(ctx);
/*
* Here we are waiting for the status to change, so don't bother
* protecting the read every time.
*/
AIO_WAIT_WHILE(ctx, job->status != JOB_STATUS_READY);
aio_context_release(ctx);
/* Begin the drained section, pausing the job */
bdrv_drain_all_begin();
assert(job->status == JOB_STATUS_STANDBY);
assert_job_status_is(job, JOB_STATUS_STANDBY);
/* Lock the IO thread to prevent the job from being run */
aio_context_acquire(ctx);
/* This will schedule the job to resume it */
bdrv_drain_all_end();
/* But the job cannot run, so it will remain on standby */
assert(job->status == JOB_STATUS_STANDBY);
WITH_JOB_LOCK_GUARD() {
/* But the job cannot run, so it will remain on standby */
assert(job->status == JOB_STATUS_STANDBY);
/* Even though the job is on standby, this should work */
job_complete(job, &error_abort);
/* Even though the job is on standby, this should work */
job_complete_locked(job, &error_abort);
/* The test is done now, clean up. */
job_finish_sync(job, NULL, &error_abort);
assert(job->status == JOB_STATUS_PENDING);
/* The test is done now, clean up. */
job_finish_sync_locked(job, NULL, &error_abort);
assert(job->status == JOB_STATUS_PENDING);
job_finalize(job, &error_abort);
assert(job->status == JOB_STATUS_CONCLUDED);
job_finalize_locked(job, &error_abort);
assert(job->status == JOB_STATUS_CONCLUDED);
job_dismiss(&job, &error_abort);
job_dismiss_locked(&job, &error_abort);
}
destroy_blk(blk);
aio_context_release(ctx);