summaryrefslogtreecommitdiffstats
path: root/src/stats/test-stats-common.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:51:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:51:24 +0000
commitf7548d6d28c313cf80e6f3ef89aed16a19815df1 (patch)
treea3f6f2a3f247293bee59ecd28e8cd8ceb6ca064a /src/stats/test-stats-common.c
parentInitial commit. (diff)
downloaddovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.tar.xz
dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.zip
Adding upstream version 1:2.3.19.1+dfsg1.upstream/1%2.3.19.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/stats/test-stats-common.c')
-rw-r--r--src/stats/test-stats-common.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/stats/test-stats-common.c b/src/stats/test-stats-common.c
new file mode 100644
index 0000000..3e307a9
--- /dev/null
+++ b/src/stats/test-stats-common.c
@@ -0,0 +1,99 @@
+/* Copyright (c) 2019 Dovecot authors, see the included COPYING file */
+
+#include "test-stats-common.h"
+#include <time.h>
+#include <unistd.h>
+
+struct event_category test_category = {
+ .name = "test",
+};
+
+struct event_category child_test_category = {
+ .name = "child",
+ .parent = &test_category,
+};
+
+pool_t test_pool;
+struct stats_metrics *stats_metrics = NULL;
+time_t stats_startup_time;
+
+static bool callback_added = FALSE;
+
+static struct stats_settings *read_settings(const char *settings)
+{
+ struct istream *is = test_istream_create(settings);
+ const char *error;
+ struct setting_parser_context *ctx =
+ settings_parser_init(test_pool, &stats_setting_parser_info, 0);
+ if (settings_parse_stream_read(ctx, is) < 0)
+ i_fatal("Failed to parse settings: %s",
+ settings_parser_get_error(ctx));
+ if (!settings_parser_check(ctx, test_pool, &error))
+ i_fatal("Failed to parse settings: %s",
+ error);
+ struct stats_settings *set = settings_parser_get(ctx);
+ settings_parser_deinit(&ctx);
+ i_stream_unref(&is);
+ return set;
+}
+
+void test_init(const char *settings_blob)
+{
+ if (!callback_added) {
+ event_register_callback(test_stats_callback);
+ callback_added = TRUE;
+ }
+
+ stats_event_categories_init();
+ test_pool = pool_alloconly_create(MEMPOOL_GROWING"test pool", 2048);
+ stats_startup_time = time(NULL);
+
+ /* register test categories */
+ stats_event_category_register(test_category.name, NULL);
+ stats_event_category_register(child_test_category.name,
+ &test_category);
+ struct stats_settings *set = read_settings(settings_blob);
+ stats_metrics = stats_metrics_init(set);
+}
+
+void test_deinit(void)
+{
+ stats_metrics_deinit(&stats_metrics);
+ stats_event_categories_deinit();
+ pool_unref(&test_pool);
+}
+
+void test_event_send(struct event *event)
+{
+ struct failure_context ctx = {
+ .type = LOG_TYPE_DEBUG,
+ };
+
+ usleep(1); /* make sure duration>0 always */
+ event_send(event, &ctx, "hello");
+}
+
+uint64_t get_stats_dist_field(const char *metric_name, enum stats_dist_field field)
+{
+ struct stats_metrics_iter *iter =
+ stats_metrics_iterate_init(stats_metrics);
+ const struct metric *metric;
+ while((metric = stats_metrics_iterate(iter)) != NULL)
+ if (strcmp(metric->name, metric_name) == 0)
+ break;
+
+ /* bug in test if not found */
+ i_assert(metric != NULL);
+
+ stats_metrics_iterate_deinit(&iter);
+
+ switch(field) {
+ case STATS_DIST_COUNT:
+ return stats_dist_get_count(metric->duration_stats);
+ case STATS_DIST_SUM:
+ return stats_dist_get_sum(metric->duration_stats);
+ default:
+ i_unreached();
+ }
+}
+