# Path: pq/api.py

# 
# Path: pq/utils/time.py
# def format_time(dt):
#     dt = timestamp_to_datetime(dt)
#     return dt.isoformat() if dt else '?'
# 
# Path: pq/tasks/consumer.py
# def poll_time(a, b, x, lag=0):
#     a = max(a, 0)   # 0 minimum pool gap
#     b = max(a, b)   # b cannot be less than a
#     return max(a + (b-a) * (exp(x) - x - 1)/FACTOR - lag, 0)
# 
# Path: tests/app.py
# def simple_task(self, value=0):
#     return self.v0 + value


import unittest
from datetime import datetime
from unittest import mock
from pq import api
from pq.utils.time import format_time
from pq.tasks.consumer import poll_time
from tests.app import simple_task
    def test_unknown_state(self):
        self.assertEqual(api.status_string(243134), 'UNKNOWN')
        self.assertEqual(api.status_string('jhbjhbj'), 'UNKNOWN')
        self.assertEqual(api.status_string(1), 'SUCCESS')

    def test_format_time(self):
        dt = datetime.now()
        st = format_time(dt)
        self.assertIsInstance(st, str)
        timestamp = dt.timestamp()
        st2 = format_time(timestamp)
        self.assertEqual(st, st2)
        self.assertEqual(format_time(None), '?')

    def test_close(self):
        t = api.QueueApp().api()
        self.assertEqual(t.closing(), False)
        t.close()
        self.assertEqual(t.closing(), True)
        self.assertEqual(t.tasks.closing(), True)
        warn = mock.MagicMock()
        t.tasks.logger.warning = warn
        self.assertFalse(t.tasks.queue('foo'))
        self.assertEqual(warn.call_count, 1)
        self.assertEqual(
            warn.call_args[0][0],
            'Cannot queue task, task backend closing'
        )

    def test_task_not_available(self):

# Path: pq/api.py

# 
# Path: pq/utils/time.py
# def format_time(dt):
#     dt = timestamp_to_datetime(dt)
#     return dt.isoformat() if dt else '?'
# 
# Path: pq/tasks/consumer.py
# def poll_time(a, b, x, lag=0):
#     a = max(a, 0)   # 0 minimum pool gap
#     b = max(a, b)   # b cannot be less than a
#     return max(a + (b-a) * (exp(x) - x - 1)/FACTOR - lag, 0)
# 
# Path: tests/app.py
# def simple_task(self, value=0):
#     return self.v0 + value


# Path: tests/test_api.py
import unittest
from datetime import datetime
from unittest import mock
from pq import api
from pq.utils.time import format_time
from pq.tasks.consumer import poll_time
from tests.app import simple_task

    def test_unknown_state(self):
        self.assertEqual(api.status_string(243134), 'UNKNOWN')
        self.assertEqual(api.status_string('jhbjhbj'), 'UNKNOWN')
        self.assertEqual(api.status_string(1), 'SUCCESS')

    def test_format_time(self):
        dt = datetime.now()
        st = format_time(dt)
        self.assertIsInstance(st, str)
        timestamp = dt.timestamp()
        st2 = format_time(timestamp)
        self.assertEqual(st, st2)
        self.assertEqual(format_time(None), '?')

    def test_close(self):
        t = api.QueueApp().api()
        self.assertEqual(t.closing(), False)
        t.close()
        self.assertEqual(t.closing(), True)
        self.assertEqual(t.tasks.closing(), True)
        warn = mock.MagicMock()
        t.tasks.logger.warning = warn
        self.assertFalse(t.tasks.queue('foo'))
        self.assertEqual(warn.call_count, 1)
        self.assertEqual(
            warn.call_args[0][0],
            'Cannot queue task, task backend closing'
        )

    def test_task_not_available(self):

        t = api.QueueApp().api()