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