| 1 | /* |
| 2 | * Unit tests for the rate policy object API. |
| 3 | * |
| 4 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: LGPL-2.1-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <common/payload-view.hpp> |
| 11 | #include <common/payload.hpp> |
| 12 | |
| 13 | #include <lttng/action/rate-policy-internal.hpp> |
| 14 | #include <lttng/action/rate-policy.h> |
| 15 | |
| 16 | #include <inttypes.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <tap/tap.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | /* For error.h. */ |
| 23 | int lttng_opt_quiet = 1; |
| 24 | int lttng_opt_verbose; |
| 25 | int lttng_opt_mi; |
| 26 | |
| 27 | #define NUM_TESTS 42 |
| 28 | |
| 29 | static void test_rate_policy_every_n() |
| 30 | { |
| 31 | enum lttng_rate_policy_status status; |
| 32 | struct lttng_rate_policy *policy_a = nullptr; /* Interval of 100. */ |
| 33 | struct lttng_rate_policy *policy_b = nullptr; /* Interval of 100 */ |
| 34 | struct lttng_rate_policy *policy_c = nullptr; /* Interval of 1 */ |
| 35 | struct lttng_rate_policy *policy_from_buffer = nullptr; |
| 36 | uint64_t interval_a_b = 100; |
| 37 | uint64_t interval_c = 1; |
| 38 | uint64_t interval_query = 0; |
| 39 | struct lttng_payload payload; |
| 40 | |
| 41 | lttng_payload_init(&payload); |
| 42 | |
| 43 | policy_a = lttng_rate_policy_every_n_create(interval_a_b); |
| 44 | policy_b = lttng_rate_policy_every_n_create(interval_a_b); |
| 45 | policy_c = lttng_rate_policy_every_n_create(interval_c); |
| 46 | ok(policy_a != nullptr, "Rate policy every n A created: interval: %" PRIu64, interval_a_b); |
| 47 | ok(policy_b != nullptr, "Rate policy every n B created: interval: %" PRIu64, interval_a_b); |
| 48 | ok(policy_c != nullptr, "Rate policy every n C created: interval: %" PRIu64, interval_c); |
| 49 | |
| 50 | ok(LTTNG_RATE_POLICY_TYPE_EVERY_N == lttng_rate_policy_get_type(policy_a), |
| 51 | "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N"); |
| 52 | |
| 53 | /* Getter tests */ |
| 54 | status = lttng_rate_policy_every_n_get_interval(nullptr, nullptr); |
| 55 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
| 56 | |
| 57 | status = lttng_rate_policy_every_n_get_interval(nullptr, &interval_query); |
| 58 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
| 59 | |
| 60 | status = lttng_rate_policy_every_n_get_interval(policy_a, nullptr); |
| 61 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID"); |
| 62 | |
| 63 | status = lttng_rate_policy_every_n_get_interval(policy_a, &interval_query); |
| 64 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_a_b, |
| 65 | " Getting interval A"); |
| 66 | |
| 67 | status = lttng_rate_policy_every_n_get_interval(policy_b, &interval_query); |
| 68 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_a_b, |
| 69 | " Getting interval B"); |
| 70 | |
| 71 | status = lttng_rate_policy_every_n_get_interval(policy_c, &interval_query); |
| 72 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_c, |
| 73 | " Getting interval C"); |
| 74 | |
| 75 | /* is_equal tests */ |
| 76 | /* TODO: this is the behaviour introduced by the |
| 77 | * lttng_condition_is_equal back in 2017 do we want to fix this and |
| 78 | * return true if both are NULL? |
| 79 | */ |
| 80 | ok(false == lttng_rate_policy_is_equal(nullptr, nullptr), "is equal (NULL,NULL)"); |
| 81 | ok(false == lttng_rate_policy_is_equal(policy_a, nullptr), "is equal (object, NULL)"); |
| 82 | ok(false == lttng_rate_policy_is_equal(nullptr, policy_a), " is equal (NULL, object)"); |
| 83 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)"); |
| 84 | |
| 85 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), "is equal (object A, object B"); |
| 86 | ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), "is equal (object B, object A"); |
| 87 | |
| 88 | ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), |
| 89 | "is equal (object A, object C)"); |
| 90 | ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), |
| 91 | "is equal (object C, object A)"); |
| 92 | |
| 93 | /* Serialization and create_from buffer. */ |
| 94 | ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); |
| 95 | { |
| 96 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
| 97 | |
| 98 | ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 && |
| 99 | policy_from_buffer != nullptr, |
| 100 | "Deserializing"); |
| 101 | } |
| 102 | |
| 103 | ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), |
| 104 | "serialized and from buffer are equal"); |
| 105 | |
| 106 | lttng_rate_policy_destroy(policy_a); |
| 107 | lttng_rate_policy_destroy(policy_b); |
| 108 | lttng_rate_policy_destroy(policy_c); |
| 109 | lttng_rate_policy_destroy(policy_from_buffer); |
| 110 | lttng_payload_reset(&payload); |
| 111 | } |
| 112 | |
| 113 | static void test_rate_policy_once_after_n() |
| 114 | { |
| 115 | enum lttng_rate_policy_status status; |
| 116 | struct lttng_rate_policy *policy_a = nullptr; /* Threshold of 100. */ |
| 117 | struct lttng_rate_policy *policy_b = nullptr; /* threshold of 100 */ |
| 118 | struct lttng_rate_policy *policy_c = nullptr; /* threshold of 1 */ |
| 119 | struct lttng_rate_policy *policy_from_buffer = nullptr; |
| 120 | uint64_t threshold_a_b = 100; |
| 121 | uint64_t threshold_c = 1; |
| 122 | uint64_t threshold_query = 0; |
| 123 | struct lttng_payload payload; |
| 124 | |
| 125 | lttng_payload_init(&payload); |
| 126 | |
| 127 | policy_a = lttng_rate_policy_once_after_n_create(threshold_a_b); |
| 128 | policy_b = lttng_rate_policy_once_after_n_create(threshold_a_b); |
| 129 | policy_c = lttng_rate_policy_once_after_n_create(threshold_c); |
| 130 | ok(policy_a != nullptr, |
| 131 | "Rate policy every n A created: threshold: %" PRIu64, |
| 132 | threshold_a_b); |
| 133 | ok(policy_b != nullptr, |
| 134 | "Rate policy every n B created: threshold: %" PRIu64, |
| 135 | threshold_a_b); |
| 136 | ok(policy_c != nullptr, "Rate policy every n C created: threshold: %" PRIu64, threshold_c); |
| 137 | |
| 138 | ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N == lttng_rate_policy_get_type(policy_a), |
| 139 | "Type is LTTNG_RATE_POLICY_TYPE_once_after_n"); |
| 140 | |
| 141 | /* Getter tests */ |
| 142 | status = lttng_rate_policy_once_after_n_get_threshold(nullptr, nullptr); |
| 143 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
| 144 | |
| 145 | status = lttng_rate_policy_once_after_n_get_threshold(nullptr, &threshold_query); |
| 146 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
| 147 | |
| 148 | status = lttng_rate_policy_once_after_n_get_threshold(policy_a, nullptr); |
| 149 | ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID"); |
| 150 | |
| 151 | status = lttng_rate_policy_once_after_n_get_threshold(policy_a, &threshold_query); |
| 152 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_a_b, |
| 153 | " Getting threshold A"); |
| 154 | |
| 155 | status = lttng_rate_policy_once_after_n_get_threshold(policy_b, &threshold_query); |
| 156 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_a_b, |
| 157 | " Getting threshold B"); |
| 158 | |
| 159 | status = lttng_rate_policy_once_after_n_get_threshold(policy_c, &threshold_query); |
| 160 | ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_c, |
| 161 | " Getting threshold C"); |
| 162 | |
| 163 | /* is_equal tests */ |
| 164 | ok(false == lttng_rate_policy_is_equal(nullptr, nullptr), "is equal (NULL,NULL)"); |
| 165 | ok(false == lttng_rate_policy_is_equal(policy_a, nullptr), "is equal (object, NULL)"); |
| 166 | ok(false == lttng_rate_policy_is_equal(nullptr, policy_a), " is equal (NULL, object)"); |
| 167 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)"); |
| 168 | |
| 169 | ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), "is equal (object A, object B"); |
| 170 | ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), "is equal (object B, object A"); |
| 171 | |
| 172 | ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), |
| 173 | "is equal (object A, object C)"); |
| 174 | ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), |
| 175 | "is equal (object C, object A)"); |
| 176 | |
| 177 | /* Serialization and create_from buffer. */ |
| 178 | ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); |
| 179 | { |
| 180 | struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1); |
| 181 | |
| 182 | ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 && |
| 183 | policy_from_buffer != nullptr, |
| 184 | "Deserializing"); |
| 185 | } |
| 186 | |
| 187 | ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), |
| 188 | "serialized and from buffer are equal"); |
| 189 | |
| 190 | lttng_rate_policy_destroy(policy_a); |
| 191 | lttng_rate_policy_destroy(policy_b); |
| 192 | lttng_rate_policy_destroy(policy_c); |
| 193 | lttng_rate_policy_destroy(policy_from_buffer); |
| 194 | lttng_payload_reset(&payload); |
| 195 | } |
| 196 | |
| 197 | int main() |
| 198 | { |
| 199 | plan_tests(NUM_TESTS); |
| 200 | test_rate_policy_every_n(); |
| 201 | test_rate_policy_once_after_n(); |
| 202 | return exit_status(); |
| 203 | } |