148 lines
4.9 KiB
C
148 lines
4.9 KiB
C
#include <unity.h>
|
|
|
|
#include "exceptions.h"
|
|
#include "metrics.h"
|
|
#include "utilities.h"
|
|
|
|
CEXCEPTION_T e;
|
|
|
|
METRICS_Registry *registry;
|
|
void setUp(void) { registry = METRICS_Registry_new(); }
|
|
void tearDown(void) { METRICS_Registry_delete(registry); }
|
|
|
|
void test_create_registry(void) { TEST_ASSERT_NOT_NULL(registry); }
|
|
|
|
void test_default_counter_value(void) { TEST_ASSERT_EQUAL(0, METRICS_Counter_value(registry, "test_counter")); }
|
|
|
|
void test_increment_counter(void) {
|
|
METRICS_Counter_inc(registry, "test_counter");
|
|
TEST_ASSERT_EQUAL(1, METRICS_Counter_value(registry, "test_counter"));
|
|
}
|
|
|
|
void test_decrement_counter(void) {
|
|
METRICS_Counter_dec(registry, "test_counter");
|
|
TEST_ASSERT_EQUAL(-1, METRICS_Counter_value(registry, "test_counter"));
|
|
}
|
|
|
|
void test_inc_dec_counter(void) {
|
|
for (int i = 0; i < 5; i++) {
|
|
METRICS_Counter_inc(registry, "test_counter");
|
|
}
|
|
TEST_ASSERT_EQUAL(5, METRICS_Counter_value(registry, "test_counter"));
|
|
for (int i = 0; i < 10; i++) {
|
|
METRICS_Counter_dec(registry, "test_counter");
|
|
}
|
|
TEST_ASSERT_EQUAL(-5, METRICS_Counter_value(registry, "test_counter"));
|
|
}
|
|
|
|
long get_value_2(void *state) {
|
|
UNUSED(state);
|
|
return 2;
|
|
}
|
|
|
|
void test_get_gauge_value(void) {
|
|
METRICS_Gauge_register(registry, "test_gauge", get_value_2, NULL);
|
|
TEST_ASSERT_EQUAL(2, METRICS_Gauge_value(registry, "test_gauge"));
|
|
}
|
|
|
|
void test_gauge_register_should_throw_exception_when_use_name_for_two_different_metric_types(void) {
|
|
METRICS_Counter_inc(registry, "test_metric");
|
|
bool exception_thrown = false;
|
|
Try { METRICS_Gauge_register(registry, "test_metric", get_value_2, NULL); }
|
|
Catch(e) { exception_thrown = true; }
|
|
TEST_ASSERT_TRUE(exception_thrown);
|
|
}
|
|
|
|
void test_getting_unregistered_gauge_value_should_throw_exception(void) {
|
|
bool exception_thrown = false;
|
|
Try { METRICS_Gauge_value(registry, "test_gauge"); }
|
|
Catch(e) { exception_thrown = true; }
|
|
TEST_ASSERT_TRUE(exception_thrown);
|
|
}
|
|
|
|
void test_adding_more_than_initial_registry_size_metrics(void) {
|
|
for (int i = 0; i < 100; i++) {
|
|
char name[20];
|
|
sprintf(name, "test_metric_%d", i);
|
|
METRICS_Counter_inc(registry, name);
|
|
}
|
|
METRICS_dump_registry(registry);
|
|
for (int i = 0; i < 100; i++) {
|
|
char name[20];
|
|
sprintf(name, "test_metric_%d", i);
|
|
TEST_ASSERT_EQUAL(1, METRICS_Counter_value(registry, name));
|
|
}
|
|
}
|
|
|
|
void test_initialize_default_registry(void) {
|
|
METRICS_INIT_DEFAULT_REGISTRY();
|
|
TEST_ASSERT_NOT_NULL(METRICS_DEFAULT_REGISTRY);
|
|
METRICS_DESTROY_DEFAULT_REGISTRY();
|
|
}
|
|
|
|
void test_inc_and_dec_counter_in_default_registry(void) {
|
|
METRICS_INIT_DEFAULT_REGISTRY();
|
|
METRICS_COUNTER_INC("test_counter");
|
|
TEST_ASSERT_EQUAL(1, METRICS_COUNTER_VALUE("test_counter"));
|
|
METRICS_COUNTER_DEC("test_counter");
|
|
TEST_ASSERT_EQUAL(0, METRICS_COUNTER_VALUE("test_counter"));
|
|
METRICS_DESTROY_DEFAULT_REGISTRY();
|
|
}
|
|
|
|
void test_gauge_in_default_registry(void) {
|
|
METRICS_INIT_DEFAULT_REGISTRY();
|
|
METRICS_GAUGE_REGISTER("test_gauge", get_value_2, NULL);
|
|
TEST_ASSERT_EQUAL(2, METRICS_GAUGE_VALUE("test_gauge"));
|
|
METRICS_DESTROY_DEFAULT_REGISTRY();
|
|
}
|
|
|
|
void test_update_histogram(void) {
|
|
METRICS_Histogram_update(registry, "test_histogram", 1);
|
|
METRICS_Histogram_update(registry, "test_histogram", 2);
|
|
}
|
|
|
|
void test_get_histogram_value(void) {
|
|
METRICS_Histogram_update(registry, "test_histogram", 1);
|
|
METRICS_Histogram_update(registry, "test_histogram", 3);
|
|
METRICS_Histogram_update(registry, "test_histogram", 5);
|
|
METRICS_Histogram_update(registry, "test_histogram", 7);
|
|
METRICS_Histogram histogram = METRICS_Histogram_value(registry, "test_histogram");
|
|
TEST_ASSERT_EQUAL(1, histogram.min);
|
|
TEST_ASSERT_EQUAL(7, histogram.max);
|
|
TEST_ASSERT_EQUAL(4, histogram.count);
|
|
TEST_ASSERT_EQUAL(4, histogram.mean);
|
|
}
|
|
|
|
void test_histogram_with_default_registry(void) {
|
|
METRICS_INIT_DEFAULT_REGISTRY();
|
|
METRICS_HISTOGRAM_UPDATE("test_histogram", 1);
|
|
METRICS_HISTOGRAM_UPDATE("test_histogram", 3);
|
|
METRICS_HISTOGRAM_UPDATE("test_histogram", 5);
|
|
METRICS_HISTOGRAM_UPDATE("test_histogram", 7);
|
|
METRICS_Histogram histogram = METRICS_HISTOGRAM_VALUE("test_histogram");
|
|
TEST_ASSERT_EQUAL(1, histogram.min);
|
|
TEST_ASSERT_EQUAL(7, histogram.max);
|
|
TEST_ASSERT_EQUAL(4, histogram.count);
|
|
TEST_ASSERT_EQUAL(4, histogram.mean);
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_create_registry);
|
|
RUN_TEST(test_default_counter_value);
|
|
RUN_TEST(test_increment_counter);
|
|
RUN_TEST(test_decrement_counter);
|
|
RUN_TEST(test_inc_dec_counter);
|
|
RUN_TEST(test_get_gauge_value);
|
|
RUN_TEST(test_gauge_register_should_throw_exception_when_use_name_for_two_different_metric_types);
|
|
RUN_TEST(test_getting_unregistered_gauge_value_should_throw_exception);
|
|
RUN_TEST(test_adding_more_than_initial_registry_size_metrics);
|
|
RUN_TEST(test_initialize_default_registry);
|
|
RUN_TEST(test_inc_and_dec_counter_in_default_registry);
|
|
RUN_TEST(test_gauge_in_default_registry);
|
|
RUN_TEST(test_update_histogram);
|
|
RUN_TEST(test_get_histogram_value);
|
|
RUN_TEST(test_histogram_with_default_registry);
|
|
return UNITY_END();
|
|
}
|