Skip to content

Commit

Permalink
tests: replaced static timeout by a dynamic
Browse files Browse the repository at this point in the history
- scheduler_sleep_count is called when the scheduler sleeps after doing his jobs
  • Loading branch information
franku authored and pstorz committed Jan 15, 2020
1 parent f58465f commit 1a52369
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions core/src/tests/run_on_incoming_connect_interval.cc
Expand Up @@ -119,11 +119,21 @@ TEST_F(RunOnIncomingConnectIntervalTest,
EXPECT_TRUE(find(jobs, "backup-bareos-fd-connect-2"));
}

static struct TestResults {
int job_counter{};
int scheduler_sleep_count{};
int timeout{};
std::map<std::string, int> job_names;
} test_results;

class TestTimeSource : public TimeSource {
public:
int counter = 0;
int counter{};
time_t SystemTime() override { return ++counter; }
void SleepFor(std::chrono::seconds /*wait_interval*/) override {}
void SleepFor(std::chrono::seconds /*wait_interval*/) override
{
++test_results.scheduler_sleep_count;
}
void Terminate() override {}
};

Expand All @@ -135,11 +145,6 @@ class TimeAdapter : public SchedulerTimeAdapter {
}
};

static struct TestResults {
int job_counter{};
std::map<std::string, int> job_names;
} test_results;

static void SchedulerJobCallback(JobControlRecord* jcr)
{
++test_results.job_counter;
Expand Down Expand Up @@ -220,13 +225,17 @@ static void RunSchedulerAndSimulateClientConnect(BareosDb& db)
std::make_unique<TimeAdapter>(std::make_unique<TestTimeSource>()),
SchedulerJobCallback);

std::thread scheduler_thread(scheduler_loop, &scheduler);

test_results = TestResults();

SimulateClientConnect("bareos-fd", scheduler, db);

std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::thread scheduler_thread(scheduler_loop, &scheduler);

int timeout{};
while (test_results.scheduler_sleep_count < 3 && timeout < 500) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
++timeout;
}

scheduler.Terminate();
scheduler_thread.join();
Expand All @@ -237,6 +246,9 @@ TEST_F(RunOnIncomingConnectIntervalTest, no_start_time_found_in_database)
MockDatabase db(MockDatabase::Mode::kFindNoStartTime);
RunSchedulerAndSimulateClientConnect(db);

EXPECT_TRUE(test_results.scheduler_sleep_count > 0)
<< "The scheduler did not sleep";

ASSERT_EQ(test_results.job_counter, 2) << "Not all jobs did run";

EXPECT_NE(test_results.job_names.find("backup-bareos-fd-connect"),
Expand All @@ -253,6 +265,9 @@ TEST_F(RunOnIncomingConnectIntervalTest, start_time_found_in_database)
MockDatabase db(MockDatabase::Mode::kFindStartTime);
RunSchedulerAndSimulateClientConnect(db);

EXPECT_TRUE(test_results.scheduler_sleep_count > 0)
<< "The scheduler did not sleep";

ASSERT_EQ(test_results.job_counter, 1)
<< "backup-bareos-fd-connect did not run";

Expand All @@ -266,6 +281,9 @@ TEST_F(RunOnIncomingConnectIntervalTest, start_time_found_wrong_time_string)
MockDatabase db(MockDatabase::Mode::kFindStartTimeWrongString);
RunSchedulerAndSimulateClientConnect(db);

EXPECT_TRUE(test_results.scheduler_sleep_count > 0)
<< "The scheduler did not sleep";

ASSERT_EQ(test_results.job_counter, 0)
<< "backup-bareos-fd-connect did not run";
}

0 comments on commit 1a52369

Please sign in to comment.