Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cancel waiting logic to test functions for DBMAsyncJob #14773

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions datadog_checks_base/datadog_checks/base/utils/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,10 @@ def __init__(

def cancel(self):
"""
Returns only when the async job loop is fully cancelled.
future.result() waits until the timeout (10 seconds) to see that the job loop has exited,
so we use it here to verify the async job is cancelled before exiting.

Raises:
TimeoutError if the job loop is not cancelled within 10 seconds
Send a signal to cancel the job loop asynchronously.
"""
self._cancel_event.set()

if self._job_loop_future is not None:
self._job_loop_future.result(10)

def run_job_loop(self, tags):
"""
:param tags:
Expand Down
2 changes: 2 additions & 0 deletions datadog_checks_base/tests/base/utils/db/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def test_dbm_async_job_enabled(enabled):
if enabled:
assert job._job_loop_future is not None
job.cancel()
job._job_loop_future.result()
else:
assert job._job_loop_future is None

Expand All @@ -215,6 +216,7 @@ def test_dbm_async_job_cancel(aggregator):
tags = ["hello:there"]
job.run_job_loop(tags)
job.cancel()
job._job_loop_future.result()
assert not job._job_loop_future.running(), "thread should be stopped"
# if the thread doesn't start until after the cancel signal is set then the db connection will never
# be created in the first place
Expand Down
3 changes: 3 additions & 0 deletions mysql/tests/test_query_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def test_async_job_enabled(dd_run_check, dbm_instance, activity_enabled):
check.cancel()
if activity_enabled:
assert check._query_activity._job_loop_future is not None
check._query_activity._job_loop_future.result()
else:
assert check._query_activity._job_loop_future is None

Expand All @@ -408,6 +409,8 @@ def test_async_job_cancel(aggregator, dd_run_check, dbm_instance):
check = MySql(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.cancel()
# wait for it to stop and make sure it doesn't throw any exceptions
check._query_activity._job_loop_future.result()
assert not check._query_activity._job_loop_future.running(), "activity thread should be stopped"
# if the thread doesn't start until after the cancel signal is set then the db connection will never
# be created in the first place
Expand Down
5 changes: 5 additions & 0 deletions mysql/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,9 @@ def test_async_job_cancel(aggregator, dd_run_check, dbm_instance):
mysql_check = MySql(common.CHECK_NAME, {}, [dbm_instance])
dd_run_check(mysql_check)
mysql_check.cancel()
# wait for it to stop and make sure it doesn't throw any exceptions
mysql_check._statement_samples._job_loop_future.result()
mysql_check._statement_metrics._job_loop_future.result()
assert not mysql_check._statement_samples._job_loop_future.running(), "samples thread should be stopped"
assert not mysql_check._statement_metrics._job_loop_future.running(), "metrics thread should be stopped"
assert mysql_check._statement_samples._db is None, "samples db connection should be gone"
Expand Down Expand Up @@ -844,10 +847,12 @@ def test_async_job_enabled(dd_run_check, dbm_instance, statement_samples_enabled
mysql_check.cancel()
if statement_samples_enabled:
assert mysql_check._statement_samples._job_loop_future is not None
mysql_check._statement_samples._job_loop_future.result()
else:
assert mysql_check._statement_samples._job_loop_future is None
if statement_metrics_enabled:
assert mysql_check._statement_metrics._job_loop_future is not None
mysql_check._statement_metrics._job_loop_future.result()
else:
assert mysql_check._statement_metrics._job_loop_future is None

Expand Down
6 changes: 6 additions & 0 deletions postgres/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ def run_one_check(check, db_instance):
"""
check.check(db_instance)
check.cancel()
if check.statement_samples._job_loop_future is not None:
check.statement_samples._job_loop_future.result()
if check.statement_metrics._job_loop_future is not None:
check.statement_metrics._job_loop_future.result()
if check.metadata_samples._job_loop_future is not None:
check.metadata_samples._job_loop_future.result()
3 changes: 3 additions & 0 deletions sqlserver/tests/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ def test_async_job_enabled(dd_run_check, dbm_instance, activity_enabled):
check.cancel()
if activity_enabled:
assert check.activity._job_loop_future is not None
check.activity._job_loop_future.result()
else:
assert check.activity._job_loop_future is None

Expand All @@ -741,6 +742,8 @@ def test_async_job_cancel_cancel(aggregator, dd_run_check, dbm_instance):
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.cancel()
# wait for it to stop and make sure it doesn't throw any exceptions
check.activity._job_loop_future.result()
assert not check.activity._job_loop_future.running(), "activity thread should be stopped"
# if the thread doesn't start until after the cancel signal is set then the db connection will never
# be created in the first place
Expand Down
3 changes: 3 additions & 0 deletions sqlserver/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ def test_async_job_enabled(dd_run_check, dbm_instance, statement_metrics_enabled
check.cancel()
if statement_metrics_enabled:
assert check.statement_metrics._job_loop_future is not None
check.statement_metrics._job_loop_future.result()
else:
assert check.statement_metrics._job_loop_future is None

Expand All @@ -798,6 +799,8 @@ def test_async_job_cancel_cancel(aggregator, dd_run_check, dbm_instance):
check = SQLServer(CHECK_NAME, {}, [dbm_instance])
dd_run_check(check)
check.cancel()
# wait for it to stop and make sure it doesn't throw any exceptions
check.statement_metrics._job_loop_future.result()
assert not check.statement_metrics._job_loop_future.running(), "metrics thread should be stopped"
# if the thread doesn't start until after the cancel signal is set then the db connection will never
# be created in the first place
Expand Down