# Path: f8a_worker/monkey_patch.py
# def _check_hung_task(self, flow_info):
#     """Remove tasks which are rotating in dispatcher for more than given time.
# 
#     :param flow_info: information about the current flow
#     """
#     node_args = flow_info['node_args']
#     flow_start_time = node_args.get('flow_start_time', 0)
#     if flow_start_time > 0:
#         now = time.time()
#         if now - flow_start_time > _SQS_MSG_LIFETIME_IN_SEC:
#             exc = NotABugFatalTaskError("Flow could not be completed in configured time limit. "
#                                         "It is being stopped forcefully. "
#                                         "Flow information: {}".format(flow_info))
#             raise self.retry(max_retries=0, exc=exc)
#     else:
#         # If message is arrived for the first time, then put current time in node arguments
#         # and consider it as starting time of the flow.
#         node_args['flow_start_time'] = time.time()


import pytest
import os
import time
from f8a_worker.monkey_patch import _check_hung_task
"""Tests covering code in start.py."""


_SQS_MSG_LIFETIME_IN_SEC = (int(os.environ.get('SQS_MSG_LIFETIME', '24')) + 1) * 60 * 60


class TestStartFunctions():
    """Test functions from start.py."""

    def test_check_hung_task(self):
        """Test _check_hung_task."""
        flow_info = {'node_args': {}}
        _check_hung_task(self, flow_info)
        assert flow_info['node_args']['flow_start_time'] is not None

        old_time = time.time() - _SQS_MSG_LIFETIME_IN_SEC
        flow_info = {'node_args': {'flow_start_time': old_time}}
        with pytest.raises(Exception):

# Path: f8a_worker/monkey_patch.py
# def _check_hung_task(self, flow_info):
#     """Remove tasks which are rotating in dispatcher for more than given time.
# 
#     :param flow_info: information about the current flow
#     """
#     node_args = flow_info['node_args']
#     flow_start_time = node_args.get('flow_start_time', 0)
#     if flow_start_time > 0:
#         now = time.time()
#         if now - flow_start_time > _SQS_MSG_LIFETIME_IN_SEC:
#             exc = NotABugFatalTaskError("Flow could not be completed in configured time limit. "
#                                         "It is being stopped forcefully. "
#                                         "Flow information: {}".format(flow_info))
#             raise self.retry(max_retries=0, exc=exc)
#     else:
#         # If message is arrived for the first time, then put current time in node arguments
#         # and consider it as starting time of the flow.
#         node_args['flow_start_time'] = time.time()


# Path: tests/test_monkey_patch.py
import pytest
import os
import time
from f8a_worker.monkey_patch import _check_hung_task

"""Tests covering code in start.py."""


_SQS_MSG_LIFETIME_IN_SEC = (int(os.environ.get('SQS_MSG_LIFETIME', '24')) + 1) * 60 * 60


class TestStartFunctions():
    """Test functions from start.py."""

    def test_check_hung_task(self):
        """Test _check_hung_task."""
        flow_info = {'node_args': {}}
        _check_hung_task(self, flow_info)
        assert flow_info['node_args']['flow_start_time'] is not None

        old_time = time.time() - _SQS_MSG_LIFETIME_IN_SEC
        flow_info = {'node_args': {'flow_start_time': old_time}}
        with pytest.raises(Exception):

            assert _check_hung_task(self, flow_info)