xref: /qemu/tests/unit/test-write-threshold.c (revision 2e0e9cbd897078fea2ad3772106e0cbd1f0c239a)
1 /*
2  * Test block device write threshold
3  *
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.
6  *
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "block/block_int.h"
12 #include "block/write-threshold.h"
13 
14 
15 static void test_threshold_not_set_on_init(void)
16 {
17     uint64_t res;
18     BlockDriverState bs;
19     memset(&bs, 0, sizeof(bs));
20 
21     res = bdrv_write_threshold_get(&bs);
22     g_assert_cmpint(res, ==, 0);
23 }
24 
25 static void test_threshold_set_get(void)
26 {
27     uint64_t threshold = 4 * 1024 * 1024;
28     uint64_t res;
29     BlockDriverState bs;
30     memset(&bs, 0, sizeof(bs));
31 
32     bdrv_write_threshold_set(&bs, threshold);
33 
34     res = bdrv_write_threshold_get(&bs);
35     g_assert_cmpint(res, ==, threshold);
36 }
37 
38 static void test_threshold_multi_set_get(void)
39 {
40     uint64_t threshold1 = 4 * 1024 * 1024;
41     uint64_t threshold2 = 15 * 1024 * 1024;
42     uint64_t res;
43     BlockDriverState bs;
44     memset(&bs, 0, sizeof(bs));
45 
46     bdrv_write_threshold_set(&bs, threshold1);
47     bdrv_write_threshold_set(&bs, threshold2);
48     res = bdrv_write_threshold_get(&bs);
49     g_assert_cmpint(res, ==, threshold2);
50 }
51 
52 static void test_threshold_not_trigger(void)
53 {
54     uint64_t threshold = 4 * 1024 * 1024;
55     BlockDriverState bs;
56 
57     memset(&bs, 0, sizeof(bs));
58 
59     bdrv_write_threshold_set(&bs, threshold);
60     bdrv_write_threshold_check_write(&bs, 1024, 1024);
61     g_assert_cmpuint(bdrv_write_threshold_get(&bs), ==, threshold);
62 }
63 
64 
65 static void test_threshold_trigger(void)
66 {
67     uint64_t threshold = 4 * 1024 * 1024;
68     BlockDriverState bs;
69 
70     memset(&bs, 0, sizeof(bs));
71 
72     bdrv_write_threshold_set(&bs, threshold);
73     bdrv_write_threshold_check_write(&bs, threshold - 1024, 2 * 1024);
74     g_assert_cmpuint(bdrv_write_threshold_get(&bs), ==, 0);
75 }
76 
77 typedef struct TestStruct {
78     const char *name;
79     void (*func)(void);
80 } TestStruct;
81 
82 
83 int main(int argc, char **argv)
84 {
85     size_t i;
86     TestStruct tests[] = {
87         { "/write-threshold/not-set-on-init",
88           test_threshold_not_set_on_init },
89         { "/write-threshold/set-get",
90           test_threshold_set_get },
91         { "/write-threshold/multi-set-get",
92           test_threshold_multi_set_get },
93         { "/write-threshold/not-trigger",
94           test_threshold_not_trigger },
95         { "/write-threshold/trigger",
96           test_threshold_trigger },
97         { NULL, NULL }
98     };
99 
100     g_test_init(&argc, &argv, NULL);
101     for (i = 0; tests[i].name != NULL; i++) {
102         g_test_add_func(tests[i].name, tests[i].func);
103     }
104     return g_test_run();
105 }
106