Message ID | 1435234332-581-11-git-send-email-stefanha@redhat.com |
---|---|
State | New |
Headers | show |
On Thu, 06/25 13:12, Stefan Hajnoczi wrote: > The BlockJobTxn unit test verifies that both single jobs and pairs of > jobs behave as a transaction group. Either all jobs complete > successfully or the group is cancelled. > > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Fam Zheng <famz@redhat.com> > --- > tests/Makefile | 3 + > tests/test-blockjob-txn.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 194 insertions(+) > create mode 100644 tests/test-blockjob-txn.c > > diff --git a/tests/Makefile b/tests/Makefile > index eff5e11..837f30b 100644 > --- a/tests/Makefile > +++ b/tests/Makefile > @@ -45,6 +45,8 @@ check-unit-y += tests/test-thread-pool$(EXESUF) > gcov-files-test-thread-pool-y = thread-pool.c > gcov-files-test-hbitmap-y = util/hbitmap.c > check-unit-y += tests/test-hbitmap$(EXESUF) > +gcov-files-test-hbitmap-y = blockjob.c > +check-unit-y += tests/test-blockjob-txn$(EXESUF) > check-unit-y += tests/test-x86-cpuid$(EXESUF) > # all code tested by test-x86-cpuid is inside topology.h > gcov-files-test-x86-cpuid-y = > @@ -283,6 +285,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil > tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a > tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o libqemuutil.a libqemustub.a > tests/test-throttle$(EXESUF): tests/test-throttle.o $(block-obj-y) libqemuutil.a libqemustub.a > +tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(block-obj-y) libqemuutil.a libqemustub.a > tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a > tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a > tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o libqemuutil.a libqemustub.a > diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c > new file mode 100644 > index 0000000..26697da > --- /dev/null > +++ b/tests/test-blockjob-txn.c > @@ -0,0 +1,191 @@ > +/* > + * Blockjob transactions tests > + * > + * Copyright Red Hat, Inc. 2015 > + * > + * Authors: > + * Stefan Hajnoczi <stefanha@redhat.com> > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or later. > + * See the COPYING.LIB file in the top-level directory. > + */ > + > +#include <glib.h> > +#include "qapi/error.h" > +#include "qemu/main-loop.h" > +#include "block/blockjob.h" > + > +typedef struct { > + BlockJob common; > + unsigned int iterations; > + int rc; > +} TestBlockJob; > + > +static const BlockJobDriver test_block_job_driver = { > + .instance_size = sizeof(TestBlockJob), > +}; > + > +static void test_block_job_complete(BlockJob *job, void *opaque) > +{ > + BlockDriverState *bs = job->bs; > + int rc = (intptr_t)opaque; > + > + if (block_job_is_cancelled(job)) { > + rc = -ECANCELED; > + } > + > + block_job_completed(job, rc); > + bdrv_unref(bs); > +} > + > +static void coroutine_fn test_block_job_run(void *opaque) > +{ > + TestBlockJob *s = opaque; > + BlockJob *job = &s->common; > + > + while (s->iterations--) { > + block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0); > + > + if (block_job_is_cancelled(job)) { > + break; > + } > + } > + > + block_job_txn_prepare_to_complete(job->txn, job, s->rc); > + > + block_job_defer_to_main_loop(job, test_block_job_complete, > + (void *)(intptr_t)s->rc); > +} > + > +static void test_block_job_cb(void *opaque, int ret) > +{ > + *(int *)opaque = ret; > +} > + > +/* Create a block job that completes with a given return code after a given > + * number of event loop iterations. The return code is stored in the given > + * result pointer. > + */ > +static BlockJob *test_block_job_start(unsigned int iterations, int rc, > + int *result) > +{ > + BlockDriverState *bs; > + TestBlockJob *s; > + > + bs = bdrv_new(); > + s = block_job_create(&test_block_job_driver, bs, 0, test_block_job_cb, > + result, &error_abort); > + s->iterations = iterations; > + s->rc = rc; > + s->common.co = qemu_coroutine_create(test_block_job_run); > + qemu_coroutine_enter(s->common.co, s); > + return &s->common; > +} > + > +static void test_single_job(int expected) > +{ > + BlockJob *job; > + BlockJobTxn *txn; > + int result = -EINPROGRESS; > + > + txn = block_job_txn_new(); > + job = test_block_job_start(1, expected, &result); > + block_job_txn_add_job(txn, job); > + block_job_txn_begin(txn); > + > + if (expected == -ECANCELED) { > + block_job_cancel(job); > + } > + > + while (result == -EINPROGRESS) { > + aio_poll(qemu_get_aio_context(), true); > + } > + g_assert_cmpint(result, ==, expected); > +} > + > +static void test_single_job_success(void) > +{ > + test_single_job(0); > +} > + > +static void test_single_job_failure(void) > +{ > + test_single_job(-EIO); > +} > + > +static void test_single_job_cancel(void) > +{ > + test_single_job(-ECANCELED); > +} > + > +static void test_pair_jobs(int expected1, int expected2) > +{ > + BlockJob *job1; > + BlockJob *job2; > + BlockJobTxn *txn; > + int result1 = -EINPROGRESS; > + int result2 = -EINPROGRESS; > + > + txn = block_job_txn_new(); > + job1 = test_block_job_start(1, expected1, &result1); > + block_job_txn_add_job(txn, job1); > + job2 = test_block_job_start(2, expected2, &result2); > + block_job_txn_add_job(txn, job2); > + block_job_txn_begin(txn); > + > + if (expected1 == -ECANCELED) { > + block_job_cancel(job1); > + } > + if (expected2 == -ECANCELED) { > + block_job_cancel(job2); > + } > + > + while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { > + aio_poll(qemu_get_aio_context(), true); > + } > + > + /* Failure or cancellation of one job cancels the other job */ > + if (expected1 != 0) { > + expected2 = -ECANCELED; > + } else if (expected2 != 0) { > + expected1 = -ECANCELED; > + } > + > + g_assert_cmpint(result1, ==, expected1); > + g_assert_cmpint(result2, ==, expected2); > +} > + > +static void test_pair_jobs_success(void) > +{ > + test_pair_jobs(0, 0); > +} > + > +static void test_pair_jobs_failure(void) > +{ > + /* Test both orderings. The two jobs run for a different number of > + * iterations so the code path is different depending on which job fails > + * first. > + */ > + test_pair_jobs(-EIO, 0); > + test_pair_jobs(0, -EIO); > +} > + > +static void test_pair_jobs_cancel(void) > +{ > + test_pair_jobs(-ECANCELED, 0); > + test_pair_jobs(0, -ECANCELED); > +} > + > +int main(int argc, char **argv) > +{ > + qemu_init_main_loop(&error_abort); > + > + g_test_init(&argc, &argv, NULL); > + g_test_add_func("/single/success", test_single_job_success); > + g_test_add_func("/single/failure", test_single_job_failure); > + g_test_add_func("/single/cancel", test_single_job_cancel); > + g_test_add_func("/pair/success", test_pair_jobs_success); > + g_test_add_func("/pair/failure", test_pair_jobs_failure); > + g_test_add_func("/pair/cancel", test_pair_jobs_cancel); > + return g_test_run(); > +} > -- > 2.4.3 > >
diff --git a/tests/Makefile b/tests/Makefile index eff5e11..837f30b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -45,6 +45,8 @@ check-unit-y += tests/test-thread-pool$(EXESUF) gcov-files-test-thread-pool-y = thread-pool.c gcov-files-test-hbitmap-y = util/hbitmap.c check-unit-y += tests/test-hbitmap$(EXESUF) +gcov-files-test-hbitmap-y = blockjob.c +check-unit-y += tests/test-blockjob-txn$(EXESUF) check-unit-y += tests/test-x86-cpuid$(EXESUF) # all code tested by test-x86-cpuid is inside topology.h gcov-files-test-x86-cpuid-y = @@ -283,6 +285,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o libqemuutil.a libqemustub.a tests/test-throttle$(EXESUF): tests/test-throttle.o $(block-obj-y) libqemuutil.a libqemustub.a +tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o libqemuutil.a libqemustub.a diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c new file mode 100644 index 0000000..26697da --- /dev/null +++ b/tests/test-blockjob-txn.c @@ -0,0 +1,191 @@ +/* + * Blockjob transactions tests + * + * Copyright Red Hat, Inc. 2015 + * + * Authors: + * Stefan Hajnoczi <stefanha@redhat.com> + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include <glib.h> +#include "qapi/error.h" +#include "qemu/main-loop.h" +#include "block/blockjob.h" + +typedef struct { + BlockJob common; + unsigned int iterations; + int rc; +} TestBlockJob; + +static const BlockJobDriver test_block_job_driver = { + .instance_size = sizeof(TestBlockJob), +}; + +static void test_block_job_complete(BlockJob *job, void *opaque) +{ + BlockDriverState *bs = job->bs; + int rc = (intptr_t)opaque; + + if (block_job_is_cancelled(job)) { + rc = -ECANCELED; + } + + block_job_completed(job, rc); + bdrv_unref(bs); +} + +static void coroutine_fn test_block_job_run(void *opaque) +{ + TestBlockJob *s = opaque; + BlockJob *job = &s->common; + + while (s->iterations--) { + block_job_sleep_ns(job, QEMU_CLOCK_REALTIME, 0); + + if (block_job_is_cancelled(job)) { + break; + } + } + + block_job_txn_prepare_to_complete(job->txn, job, s->rc); + + block_job_defer_to_main_loop(job, test_block_job_complete, + (void *)(intptr_t)s->rc); +} + +static void test_block_job_cb(void *opaque, int ret) +{ + *(int *)opaque = ret; +} + +/* Create a block job that completes with a given return code after a given + * number of event loop iterations. The return code is stored in the given + * result pointer. + */ +static BlockJob *test_block_job_start(unsigned int iterations, int rc, + int *result) +{ + BlockDriverState *bs; + TestBlockJob *s; + + bs = bdrv_new(); + s = block_job_create(&test_block_job_driver, bs, 0, test_block_job_cb, + result, &error_abort); + s->iterations = iterations; + s->rc = rc; + s->common.co = qemu_coroutine_create(test_block_job_run); + qemu_coroutine_enter(s->common.co, s); + return &s->common; +} + +static void test_single_job(int expected) +{ + BlockJob *job; + BlockJobTxn *txn; + int result = -EINPROGRESS; + + txn = block_job_txn_new(); + job = test_block_job_start(1, expected, &result); + block_job_txn_add_job(txn, job); + block_job_txn_begin(txn); + + if (expected == -ECANCELED) { + block_job_cancel(job); + } + + while (result == -EINPROGRESS) { + aio_poll(qemu_get_aio_context(), true); + } + g_assert_cmpint(result, ==, expected); +} + +static void test_single_job_success(void) +{ + test_single_job(0); +} + +static void test_single_job_failure(void) +{ + test_single_job(-EIO); +} + +static void test_single_job_cancel(void) +{ + test_single_job(-ECANCELED); +} + +static void test_pair_jobs(int expected1, int expected2) +{ + BlockJob *job1; + BlockJob *job2; + BlockJobTxn *txn; + int result1 = -EINPROGRESS; + int result2 = -EINPROGRESS; + + txn = block_job_txn_new(); + job1 = test_block_job_start(1, expected1, &result1); + block_job_txn_add_job(txn, job1); + job2 = test_block_job_start(2, expected2, &result2); + block_job_txn_add_job(txn, job2); + block_job_txn_begin(txn); + + if (expected1 == -ECANCELED) { + block_job_cancel(job1); + } + if (expected2 == -ECANCELED) { + block_job_cancel(job2); + } + + while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { + aio_poll(qemu_get_aio_context(), true); + } + + /* Failure or cancellation of one job cancels the other job */ + if (expected1 != 0) { + expected2 = -ECANCELED; + } else if (expected2 != 0) { + expected1 = -ECANCELED; + } + + g_assert_cmpint(result1, ==, expected1); + g_assert_cmpint(result2, ==, expected2); +} + +static void test_pair_jobs_success(void) +{ + test_pair_jobs(0, 0); +} + +static void test_pair_jobs_failure(void) +{ + /* Test both orderings. The two jobs run for a different number of + * iterations so the code path is different depending on which job fails + * first. + */ + test_pair_jobs(-EIO, 0); + test_pair_jobs(0, -EIO); +} + +static void test_pair_jobs_cancel(void) +{ + test_pair_jobs(-ECANCELED, 0); + test_pair_jobs(0, -ECANCELED); +} + +int main(int argc, char **argv) +{ + qemu_init_main_loop(&error_abort); + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/single/success", test_single_job_success); + g_test_add_func("/single/failure", test_single_job_failure); + g_test_add_func("/single/cancel", test_single_job_cancel); + g_test_add_func("/pair/success", test_pair_jobs_success); + g_test_add_func("/pair/failure", test_pair_jobs_failure); + g_test_add_func("/pair/cancel", test_pair_jobs_cancel); + return g_test_run(); +}
The BlockJobTxn unit test verifies that both single jobs and pairs of jobs behave as a transaction group. Either all jobs complete successfully or the group is cancelled. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> --- tests/Makefile | 3 + tests/test-blockjob-txn.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 tests/test-blockjob-txn.c