diff --git a/tests/unit/test_logs.c b/tests/unit/test_logs.c index 91ea1e580..1810bd515 100644 --- a/tests/unit/test_logs.c +++ b/tests/unit/test_logs.c @@ -1,4 +1,5 @@ #include "sentry_logs.h" +#include "sentry_sync.h" #include "sentry_testsupport.h" #include "sentry_envelope.h" @@ -13,7 +14,7 @@ #endif typedef struct { - uint64_t called_count; + volatile long called_count; bool has_validation_error; } transport_validation_data_t; @@ -37,7 +38,7 @@ validate_logs_envelope(sentry_envelope_t *envelope, void *data) // Only validate and count log envelopes, skip others (e.g., session) if (strcmp(type, "log") == 0) { - validation_data->called_count += 1; + sentry__atomic_fetch_and_add(&validation_data->called_count, 1); } sentry_envelope_free(envelope); @@ -65,8 +66,14 @@ SENTRY_TEST(basic_logging_functionality) TEST_CHECK_INT_EQUAL(sentry_log_info("Info message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_warn("Warning message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_error("Error message"), 0); - // Sleep to allow first batch to flush (testing batch timing behavior) - sleep_ms(20); + // Sleep up to 5s to allow first batch to flush (testing batch timing + // behavior) + for (int i = 0; + i < 250 && sentry__atomic_fetch(&validation_data.called_count) < 1; + i++) { + sleep_ms(20); + } + TEST_CHECK_INT_EQUAL(validation_data.called_count, 1); TEST_CHECK_INT_EQUAL(sentry_log_fatal("Fatal message"), 0); sentry_close(); diff --git a/tests/unit/test_metrics.c b/tests/unit/test_metrics.c index d709911e5..c341d058b 100644 --- a/tests/unit/test_metrics.c +++ b/tests/unit/test_metrics.c @@ -1,4 +1,5 @@ #include "sentry_metrics.h" +#include "sentry_sync.h" #include "sentry_testsupport.h" #include "sentry_envelope.h" @@ -13,7 +14,7 @@ #endif typedef struct { - uint64_t called_count; + volatile long called_count; bool has_validation_error; } transport_validation_data_t; @@ -36,7 +37,7 @@ validate_metrics_envelope(sentry_envelope_t *envelope, void *data) // Only validate and count metric envelopes, skip others (e.g., session) if (strcmp(type, "trace_metric") == 0) { - validation_data->called_count += 1; + sentry__atomic_fetch_and_add(&validation_data->called_count, 1); } sentry_envelope_free(envelope); @@ -146,8 +147,13 @@ SENTRY_TEST(metrics_batch) sentry_metrics_count("test.counter", 1, sentry_value_new_null()), SENTRY_METRICS_RESULT_SUCCESS); } - // Sleep to allow first batch to flush - sleep_ms(20); + // Sleep up to 5s to allow first batch to flush + for (int i = 0; + i < 250 && sentry__atomic_fetch(&validation_data.called_count) < 1; + i++) { + sleep_ms(20); + } + TEST_CHECK_INT_EQUAL(validation_data.called_count, 1); TEST_CHECK_INT_EQUAL( sentry_metrics_count("test.counter", 1, sentry_value_new_null()), SENTRY_METRICS_RESULT_SUCCESS);