2 * Test block device write threshold
4 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
5 * See the COPYING.LIB file in the top-level directory.
9 #include "qemu/osdep.h"
11 #include "block/block_int.h"
12 #include "block/write-threshold.h"
15 static void test_threshold_not_set_on_init(void)
19 memset(&bs
, 0, sizeof(bs
));
21 g_assert(!bdrv_write_threshold_is_set(&bs
));
23 res
= bdrv_write_threshold_get(&bs
);
24 g_assert_cmpint(res
, ==, 0);
27 static void test_threshold_set_get(void)
29 uint64_t threshold
= 4 * 1024 * 1024;
32 memset(&bs
, 0, sizeof(bs
));
34 bdrv_write_threshold_set(&bs
, threshold
);
36 g_assert(bdrv_write_threshold_is_set(&bs
));
38 res
= bdrv_write_threshold_get(&bs
);
39 g_assert_cmpint(res
, ==, threshold
);
42 static void test_threshold_multi_set_get(void)
44 uint64_t threshold1
= 4 * 1024 * 1024;
45 uint64_t threshold2
= 15 * 1024 * 1024;
48 memset(&bs
, 0, sizeof(bs
));
50 bdrv_write_threshold_set(&bs
, threshold1
);
51 bdrv_write_threshold_set(&bs
, threshold2
);
52 res
= bdrv_write_threshold_get(&bs
);
53 g_assert_cmpint(res
, ==, threshold2
);
56 static void test_threshold_not_trigger(void)
59 uint64_t threshold
= 4 * 1024 * 1024;
61 BdrvTrackedRequest req
;
63 memset(&bs
, 0, sizeof(bs
));
64 memset(&req
, 0, sizeof(req
));
68 bdrv_write_threshold_set(&bs
, threshold
);
69 amount
= bdrv_write_threshold_exceeded(&bs
, &req
);
70 g_assert_cmpuint(amount
, ==, 0);
74 static void test_threshold_trigger(void)
77 uint64_t threshold
= 4 * 1024 * 1024;
79 BdrvTrackedRequest req
;
81 memset(&bs
, 0, sizeof(bs
));
82 memset(&req
, 0, sizeof(req
));
83 req
.offset
= (4 * 1024 * 1024) - 1024;
86 bdrv_write_threshold_set(&bs
, threshold
);
87 amount
= bdrv_write_threshold_exceeded(&bs
, &req
);
88 g_assert_cmpuint(amount
, >=, 1024);
91 typedef struct TestStruct
{
97 int main(int argc
, char **argv
)
100 TestStruct tests
[] = {
101 { "/write-threshold/not-set-on-init",
102 test_threshold_not_set_on_init
},
103 { "/write-threshold/set-get",
104 test_threshold_set_get
},
105 { "/write-threshold/multi-set-get",
106 test_threshold_multi_set_get
},
107 { "/write-threshold/not-trigger",
108 test_threshold_not_trigger
},
109 { "/write-threshold/trigger",
110 test_threshold_trigger
},
114 g_test_init(&argc
, &argv
, NULL
);
115 for (i
= 0; tests
[i
].name
!= NULL
; i
++) {
116 g_test_add_func(tests
[i
].name
, tests
[i
].func
);