# Path: pgctl/daemontools.py
# class SvStat(
#         namedtuple('SvStat', ['state', 'pid', 'exitcode', 'seconds', 'process'])
# ):
#     __slots__ = ()
#     UNSUPERVISED = 'could not get status, supervisor is down'
#     INVALID = 'no such service'
# 
#     def __repr__(self):
#         format = '{0.state}'
#         if self.pid is not None:
#             format += ' (pid {0.pid})'
#         if self.exitcode is not None:
#             format += ' (exitcode {0.exitcode})'
#         if self.seconds is not None:
#             format += ' {0.seconds} seconds'
#         if self.process is not None:
#             format += ', {0.process}'
# 
#         return format.format(self)


import os
import pytest
from py._path.local import LocalPath as Path
from testing import norm
from testing.assertions import assert_svstat
from testing.assertions import wait_for
from testing.subprocess import assert_command
from pgctl.daemontools import SvStat
    from unittest.mock import patch, ANY
        from testfixtures import Comparison as C
        from pgctl.daemontools import svstat

pytestmark = pytest.mark.usefixtures('in_example_dir')
SLOW_STARTUP_TIME = 6


@pytest.fixture
def service_name():
    yield 'slow-startup'


def assert_it_times_out_regardless_force(is_force):
    assert_command(
        ('pgctl', 'start') + (('--force',) if is_force else ()),
        '''\
''',
        '''\
[pgctl] Starting: slow-startup
[pgctl] ERROR: service 'slow-startup' failed to start after {TIME} seconds, its status is up (pid {PID}) {TIME} seconds
==> playground/slow-startup/logs/current <==
{TIMESTAMP} waiting {TIME} seconds to become ready
[pgctl] Stopping: slow-startup
[pgctl] Stopped: slow-startup
[pgctl]
[pgctl] There might be useful information further up in the log; you can view it by running:
[pgctl]     less +G playground/slow-startup/logs/current
[pgctl] ERROR: Some services failed to start: slow-startup
''',
        1,
        norm=norm.pgctl,
    )

# Path: pgctl/daemontools.py
# class SvStat(
#         namedtuple('SvStat', ['state', 'pid', 'exitcode', 'seconds', 'process'])
# ):
#     __slots__ = ()
#     UNSUPERVISED = 'could not get status, supervisor is down'
#     INVALID = 'no such service'
# 
#     def __repr__(self):
#         format = '{0.state}'
#         if self.pid is not None:
#             format += ' (pid {0.pid})'
#         if self.exitcode is not None:
#             format += ' (exitcode {0.exitcode})'
#         if self.seconds is not None:
#             format += ' {0.seconds} seconds'
#         if self.process is not None:
#             format += ', {0.process}'
# 
#         return format.format(self)


# Path: tests/spec/slow_startup.py
import os
import pytest
from py._path.local import LocalPath as Path
from testing import norm
from testing.assertions import assert_svstat
from testing.assertions import wait_for
from testing.subprocess import assert_command
from pgctl.daemontools import SvStat
    from unittest.mock import patch, ANY
        from testfixtures import Comparison as C
        from pgctl.daemontools import svstat


pytestmark = pytest.mark.usefixtures('in_example_dir')
SLOW_STARTUP_TIME = 6


@pytest.fixture
def service_name():
    yield 'slow-startup'


def assert_it_times_out_regardless_force(is_force):
    assert_command(
        ('pgctl', 'start') + (('--force',) if is_force else ()),
        '''\
''',
        '''\
[pgctl] Starting: slow-startup
[pgctl] ERROR: service 'slow-startup' failed to start after {TIME} seconds, its status is up (pid {PID}) {TIME} seconds
==> playground/slow-startup/logs/current <==
{TIMESTAMP} waiting {TIME} seconds to become ready
[pgctl] Stopping: slow-startup
[pgctl] Stopped: slow-startup
[pgctl]
[pgctl] There might be useful information further up in the log; you can view it by running:
[pgctl]     less +G playground/slow-startup/logs/current
[pgctl] ERROR: Some services failed to start: slow-startup
''',
        1,
        norm=norm.pgctl,
    )

    assert_svstat('playground/slow-startup', state=SvStat.UNSUPERVISED)