fix memory leak in timer test

This commit is contained in:
Xavier Arteaga 2020-01-27 12:27:53 +01:00 committed by Andre Puschmann
parent b88a8635f1
commit 0edd8f74d8
1 changed files with 37 additions and 31 deletions

View File

@ -196,40 +196,43 @@ int timers2_test3()
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }
static std::vector<timer_handler::unique_timer> timers2_test4_t; struct timers2_test4_context {
static srslte::tti_sync_cv timers2_test4_tti_sync1; std::vector<timer_handler::unique_timer> timers;
static srslte::tti_sync_cv timers2_test4_tti_sync2; srslte::tti_sync_cv tti_sync1;
static uint32_t duration = 1000; srslte::tti_sync_cv tti_sync2;
const uint32_t duration = 1000;
};
static void timers2_test4_thread() static void timers2_test4_thread(timers2_test4_context* ctx)
{ {
std::mt19937 mt19937(4); std::mt19937 mt19937(4);
std::uniform_real_distribution<float> real_dist(0.0f, 1.0f); std::uniform_real_distribution<float> real_dist(0.0f, 1.0f);
for (uint32_t d = 0; d < duration; d++) { for (uint32_t d = 0; d < ctx->duration; d++) {
// make random events // make random events
for (uint32_t i = 1; i < timers2_test4_t.size(); i++) { for (uint32_t i = 1; i < ctx->timers.size(); i++) {
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].run(); ctx->timers[i].run();
} }
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].stop(); ctx->timers[i].stop();
} }
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].set(static_cast<uint32_t>(duration * real_dist(mt19937))); ctx->timers[i].set(static_cast<uint32_t>(ctx->duration * real_dist(mt19937)));
timers2_test4_t[i].run(); ctx->timers[i].run();
} }
} }
// Send finished to main thread // Send finished to main thread
timers2_test4_tti_sync1.increase(); ctx->tti_sync1.increase();
// Wait to main thread to check results // Wait to main thread to check results
timers2_test4_tti_sync2.wait(); ctx->tti_sync2.wait();
} }
} }
int timers2_test4() int timers2_test4()
{ {
timers2_test4_context* ctx = new timers2_test4_context;
timer_handler timers; timer_handler timers;
uint32_t nof_timers = 32; uint32_t nof_timers = 32;
std::mt19937 mt19937(4); std::mt19937 mt19937(4);
@ -237,65 +240,68 @@ int timers2_test4()
// Generate all timers and start them // Generate all timers and start them
for (uint32_t i = 0; i < nof_timers; i++) { for (uint32_t i = 0; i < nof_timers; i++) {
timers2_test4_t.push_back(timers.get_unique_timer()); ctx->timers.push_back(timers.get_unique_timer());
timers2_test4_t[i].set(duration); ctx->timers[i].set(ctx->duration);
timers2_test4_t[i].run(); ctx->timers[i].run();
} }
// Create side thread // Create side thread
std::thread thread(timers2_test4_thread); std::thread thread(timers2_test4_thread, ctx);
for (uint32_t d = 0; d < duration; d++) { for (uint32_t d = 0; d < ctx->duration; d++) {
// make random events // make random events
for (uint32_t i = 1; i < nof_timers; i++) { for (uint32_t i = 1; i < nof_timers; i++) {
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].run(); ctx->timers[i].run();
} }
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].stop(); ctx->timers[i].stop();
} }
if (0.1f > real_dist(mt19937)) { if (0.1f > real_dist(mt19937)) {
timers2_test4_t[i].set(static_cast<uint32_t>(duration * real_dist(mt19937))); ctx->timers[i].set(static_cast<uint32_t>(ctx->duration * real_dist(mt19937)));
timers2_test4_t[i].run(); ctx->timers[i].run();
} }
} }
// first times, does not have event, it shall keep running // first times, does not have event, it shall keep running
TESTASSERT(timers2_test4_t[0].is_running()); TESTASSERT(ctx->timers[0].is_running());
// Increment time // Increment time
timers.step_all(); timers.step_all();
// wait second thread to finish events // wait second thread to finish events
timers2_test4_tti_sync1.wait(); ctx->tti_sync1.wait();
// assert no timer got wrong values // assert no timer got wrong values
for (uint32_t i = 0; i < nof_timers; i++) { for (uint32_t i = 0; i < nof_timers; i++) {
if (timers2_test4_t[i].is_running()) { if (ctx->timers[i].is_running()) {
TESTASSERT(timers2_test4_t[i].value() <= timers2_test4_t[i].duration()); TESTASSERT(ctx->timers[i].value() <= ctx->timers[i].duration());
} }
} }
// Start new TTI // Start new TTI
timers2_test4_tti_sync2.increase(); ctx->tti_sync2.increase();
} }
// Finish asynchronous thread // Finish asynchronous thread
thread.join(); thread.join();
// First timer should have expired // First timer should have expired
TESTASSERT(timers2_test4_t[0].is_expired()); TESTASSERT(ctx->timers[0].is_expired());
TESTASSERT(not timers2_test4_t[0].is_running()); TESTASSERT(not ctx->timers[0].is_running());
// Run for the maximum period // Run for the maximum period
for (uint32_t d = 0; d < duration; d++) { for (uint32_t d = 0; d < ctx->duration; d++) {
timers.step_all(); timers.step_all();
} }
// No timer should be running // No timer should be running
for (uint32_t i = 0; i < nof_timers; i++) { for (uint32_t i = 0; i < nof_timers; i++) {
TESTASSERT(not timers2_test4_t[i].is_running()); TESTASSERT(not ctx->timers[i].is_running());
} }
delete ctx;
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }