# 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: pgctl/subprocess.py
# def set_defaults(func, **defaults):
#     def wrapped(cmd, **kwargs):
# PIPE = subprocess.PIPE
# STDOUT = subprocess.STDOUT
# 
# Path: pgctl/subprocess.py
# PIPE = subprocess.PIPE
# 
# Path: pgctl/subprocess.py
# def set_defaults(func, **defaults):
#     def wrapped(cmd, **kwargs):
# PIPE = subprocess.PIPE
# STDOUT = subprocess.STDOUT


import json
import os
import subprocess
import pytest
        import fcntl
                        import time
from unittest import mock
from testing import norm
from testing import pty
from testing.assertions import assert_svstat
from testing.assertions import wait_for
from testing.subprocess import assert_command
from testing.subprocess import ctrl_c
from testing.subprocess import run
from pgctl.daemontools import SvStat
from pgctl.subprocess import check_call
from pgctl.subprocess import PIPE
from pgctl.subprocess import Popen
        from testfixtures import StringComparison as S
            from testing import copy_example
        with open(os.path.join('slow-startup', 'timeout-ready'), 'w') as f:
            f.write('10\n')
        with mock.patch.dict(
            os.environ,
            {'PGCTL_FORCE_ENABLE_LOG_VIEWER': '1'},
        ):
            subprocess.check_call(('pgctl', 'start', 'slow-startup'))

        output = capfd.readouterr()

        # Don't want to assert the entire embedded log viewer output, just
        # testing for a few strings that should be present.
        assert '[pgctl] Starting: slow-startup\n' in output.err
        assert 'Still starting: slow-startup\n' in output.err
        assert '[pgctl] All services have started\n' in output.err

        # Make sure there's a log line that looks like:
        # ║[slow-startup] 2021-11-02 10:39:51.406518500  waiting 2 seconds to become rea║
        assert any(
            '[slow-startup]' in line and 'waiting 2 seconds to become re' in line
            for line in output.err.splitlines()
        )


class DescribeStop:

    def it_does_stop(self, in_example_dir):
        check_call(('pgctl', 'start', 'sleep'))
        check_call(('pgctl', 'stop', 'sleep'))


# 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: pgctl/subprocess.py
# def set_defaults(func, **defaults):
#     def wrapped(cmd, **kwargs):
# PIPE = subprocess.PIPE
# STDOUT = subprocess.STDOUT
# 
# Path: pgctl/subprocess.py
# PIPE = subprocess.PIPE
# 
# Path: pgctl/subprocess.py
# def set_defaults(func, **defaults):
#     def wrapped(cmd, **kwargs):
# PIPE = subprocess.PIPE
# STDOUT = subprocess.STDOUT


# Path: tests/spec/examples.py
import json
import os
import subprocess
import pytest
        import fcntl
                        import time
from unittest import mock
from testing import norm
from testing import pty
from testing.assertions import assert_svstat
from testing.assertions import wait_for
from testing.subprocess import assert_command
from testing.subprocess import ctrl_c
from testing.subprocess import run
from pgctl.daemontools import SvStat
from pgctl.subprocess import check_call
from pgctl.subprocess import PIPE
from pgctl.subprocess import Popen
        from testfixtures import StringComparison as S
            from testing import copy_example

        with open(os.path.join('slow-startup', 'timeout-ready'), 'w') as f:
            f.write('10\n')
        with mock.patch.dict(
            os.environ,
            {'PGCTL_FORCE_ENABLE_LOG_VIEWER': '1'},
        ):
            subprocess.check_call(('pgctl', 'start', 'slow-startup'))

        output = capfd.readouterr()

        # Don't want to assert the entire embedded log viewer output, just
        # testing for a few strings that should be present.
        assert '[pgctl] Starting: slow-startup\n' in output.err
        assert 'Still starting: slow-startup\n' in output.err
        assert '[pgctl] All services have started\n' in output.err

        # Make sure there's a log line that looks like:
        # ║[slow-startup] 2021-11-02 10:39:51.406518500  waiting 2 seconds to become rea║
        assert any(
            '[slow-startup]' in line and 'waiting 2 seconds to become re' in line
            for line in output.err.splitlines()
        )


class DescribeStop:

    def it_does_stop(self, in_example_dir):
        check_call(('pgctl', 'start', 'sleep'))
        check_call(('pgctl', 'stop', 'sleep'))


        assert_svstat('playground/sleep', state=SvStat.UNSUPERVISED)