Skip to content

Commit dbcbd36

Browse files
Marco PaganiXu Yilun
authored andcommitted
fpga: Simplify and improve fpga mgr test using deferred actions
Use deferred actions to simplify the test suite and avoid potential memory leaks when test cases fail. Remove unnecessary calls to kunit_device_unregister() since kunit devices are tied to the test context and released by a deferred action when the test is completed. Signed-off-by: Marco Pagani <[email protected]> Acked-by: Xu Yilun <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Xu Yilun <[email protected]>
1 parent 8400291 commit dbcbd36

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

drivers/fpga/tests/fpga-mgr-test.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ struct mgr_ctx {
4444
struct mgr_stats stats;
4545
};
4646

47+
/*
48+
* Wrappers to avoid cast warnings when passing action functions directly
49+
* to kunit_add_action().
50+
*/
51+
KUNIT_DEFINE_ACTION_WRAPPER(sg_free_table_wrapper, sg_free_table,
52+
struct sg_table *);
53+
54+
KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper, fpga_image_info_free,
55+
struct fpga_image_info *);
56+
4757
/**
4858
* init_test_buffer() - Allocate and initialize a test image in a buffer.
4959
* @test: KUnit test context object.
@@ -257,6 +267,9 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test)
257267
KUNIT_ASSERT_EQ(test, ret, 0);
258268
sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE);
259269

270+
ret = kunit_add_action_or_reset(test, sg_free_table_wrapper, sgt);
271+
KUNIT_ASSERT_EQ(test, ret, 0);
272+
260273
ctx->img_info->sgt = sgt;
261274

262275
ret = fpga_mgr_load(ctx->mgr, ctx->img_info);
@@ -273,13 +286,12 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test)
273286
KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1);
274287
KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_seq, ctx->stats.op_parse_header_seq + 2);
275288
KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3);
276-
277-
sg_free_table(ctx->img_info->sgt);
278289
}
279290

280291
static int fpga_mgr_test_init(struct kunit *test)
281292
{
282293
struct mgr_ctx *ctx;
294+
int ret;
283295

284296
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
285297
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
@@ -294,19 +306,14 @@ static int fpga_mgr_test_init(struct kunit *test)
294306
ctx->img_info = fpga_image_info_alloc(ctx->dev);
295307
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->img_info);
296308

309+
ret = kunit_add_action_or_reset(test, fpga_image_info_free_wrapper, ctx->img_info);
310+
KUNIT_ASSERT_EQ(test, ret, 0);
311+
297312
test->priv = ctx;
298313

299314
return 0;
300315
}
301316

302-
static void fpga_mgr_test_exit(struct kunit *test)
303-
{
304-
struct mgr_ctx *ctx = test->priv;
305-
306-
fpga_image_info_free(ctx->img_info);
307-
kunit_device_unregister(test, ctx->dev);
308-
}
309-
310317
static struct kunit_case fpga_mgr_test_cases[] = {
311318
KUNIT_CASE(fpga_mgr_test_get),
312319
KUNIT_CASE(fpga_mgr_test_lock),
@@ -318,7 +325,6 @@ static struct kunit_case fpga_mgr_test_cases[] = {
318325
static struct kunit_suite fpga_mgr_suite = {
319326
.name = "fpga_mgr",
320327
.init = fpga_mgr_test_init,
321-
.exit = fpga_mgr_test_exit,
322328
.test_cases = fpga_mgr_test_cases,
323329
};
324330

0 commit comments

Comments
 (0)