Skip to content

Commit 3f48972

Browse files
committed
Use enum for process types
1 parent 23790b2 commit 3f48972

File tree

5 files changed

+52
-46
lines changed

5 files changed

+52
-46
lines changed

Dockerfile.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ FROM postgres:${PG_VERSION}-alpine
22

33
ENV PYTHON=python${PYTHON_VERSION}
44
RUN if [ "${PYTHON_VERSION}" = "2" ] ; then \
5-
apk add --no-cache curl python2 python2-dev build-base py-virtualenv py-pip; \
5+
apk add --no-cache curl python2 python2-dev build-base musl-dev py-virtualenv py-pip; \
66
fi
77
RUN if [ "${PYTHON_VERSION}" = "3" ] ; then \
8-
apk add --no-cache curl python3 python3-dev build-base py-virtualenv; \
8+
apk add --no-cache curl python3 python3-dev build-base musl-dev py-virtualenv; \
99
fi
1010
ENV LANG=C.UTF-8
1111

testgres/connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, node, dbname=None, username=None, password=None):
3434
username = username or default_username()
3535

3636
self._node = node
37-
self._backend_pid = 0
37+
self._pid = 0
3838

3939
self._connection = pglib.connect(
4040
database=dbname,
@@ -54,10 +54,10 @@ def connection(self):
5454
return self._connection
5555

5656
@property
57-
def backend_pid(self):
58-
if not self._backend_pid:
59-
self._backend_pid = self.execute("select pg_backend_pid();")[0][0]
60-
return self._backend_pid
57+
def pid(self):
58+
if not self._pid:
59+
self._pid = self.execute("select pg_backend_pid();")[0][0]
60+
return self._pid
6161

6262
@property
6363
def cursor(self):

testgres/enums.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,18 @@ def __bool__(self):
3535

3636
# for Python 2.x
3737
__nonzero__ = __bool__
38+
39+
40+
class ProcessType(Enum):
41+
"""
42+
Types of postgres processes
43+
"""
44+
Checkpointer = 'postgres: checkpointer'
45+
BackgroundWriter = 'postgres: background writer'
46+
WalWriter = 'postgres: walwriter'
47+
AutovacuumLauncher = 'postgres: autovacuum launcher'
48+
StatsCollector = 'postgres: stats collector'
49+
LogicalReplicationLauncher = 'postgres: logical replication launcher'
50+
WalReceiver = 'postgres: walreceiver'
51+
WalSender = 'postgres: walsender'
52+
Startup = 'postgres: startup'

testgres/node.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from six import raise_from
1717
from tempfile import mkstemp, mkdtemp
1818

19-
from .enums import NodeStatus
19+
from .enums import NodeStatus, ProcessType
2020

2121
from .cache import cached_initdb
2222

@@ -465,25 +465,13 @@ def get_auxiliary_pids(self):
465465
result = {}
466466
for child in children:
467467
line = child.cmdline()[0]
468-
if line.startswith('postgres: checkpointer'):
469-
result['checkpointer'] = child.pid
470-
elif line.startswith('postgres: background writer'):
471-
result['bgwriter'] = child.pid
472-
elif line.startswith('postgres: walwriter'):
473-
result['walwriter'] = child.pid
474-
elif line.startswith('postgres: autovacuum launcher'):
475-
result['autovacuum_launcher'] = child.pid
476-
elif line.startswith('postgres: stats collector'):
477-
result['stats'] = child.pid
478-
elif line.startswith('postgres: logical replication launcher'):
479-
result['logical_replication_launcher'] = child.pid
480-
elif line.startswith('postgres: walreceiver'):
481-
result['walreceiver'] = child.pid
482-
elif line.startswith('postgres: walsender'):
483-
result.setdefault('walsenders', [])
484-
result['walsenders'].append(child.pid)
485-
elif line.startswith('postgres: startup'):
486-
result['startup'] = child.pid
468+
for ptype in ProcessType:
469+
if ptype == ProcessType.WalSender \
470+
and line.startswith(ptype.value):
471+
result.setdefault(ptype, [])
472+
result[ptype].append(child.pid)
473+
elif line.startswith(ptype.value):
474+
result[ptype] = child.pid
487475

488476
return result
489477

tests/test_simple.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
from testgres import bound_ports
4242
from testgres.utils import pg_version_ge
43+
from testgres.enums import ProcessType
4344

4445

4546
def util_exists(util):
@@ -709,28 +710,28 @@ def test_pids(self):
709710
psutil = None
710711

711712
master_processes = (
712-
'checkpointer',
713-
'bgwriter',
714-
'walwriter',
715-
'autovacuum_launcher',
716-
'stats',
717-
'logical_replication_launcher',
718-
'walsenders',
713+
ProcessType.Checkpointer,
714+
ProcessType.BackgroundWriter,
715+
ProcessType.WalWriter,
716+
ProcessType.AutovacuumLauncher,
717+
ProcessType.StatsCollector,
718+
ProcessType.LogicalReplicationLauncher,
719+
ProcessType.WalSender,
719720
)
720721
repl_processes = (
721-
'startup',
722-
'checkpointer',
723-
'bgwriter',
724-
'stats',
725-
'walreceiver',
722+
ProcessType.Startup,
723+
ProcessType.Checkpointer,
724+
ProcessType.BackgroundWriter,
725+
ProcessType.StatsCollector,
726+
ProcessType.WalReceiver,
726727
)
727728

728729
with get_new_node('master') as master:
729730
master.init().start()
730731

731732
self.assertIsNotNone(master.pid)
732733
with master.connect() as con:
733-
self.assertTrue(con.backend_pid > 0)
734+
self.assertTrue(con.pid > 0)
734735

735736
with master.backup() as backup:
736737
with backup.spawn_replica('repl', True) as repl:
@@ -740,14 +741,16 @@ def test_pids(self):
740741
self.assertIsNone(repl.auxiliary_pids)
741742
else:
742743
master_pids = master.auxiliary_pids
743-
for name in master_processes:
744-
self.assertTrue(name in master_pids)
745-
self.assertTrue(len(master_pids['walsenders']) == 1)
744+
for ptype in master_processes:
745+
self.assertIn(ptype, master_pids)
746+
self.assertTrue(len(master_pids[ProcessType.WalSender]) == 1)
746747

747748
repl_pids = repl.auxiliary_pids
748-
for name in repl_processes:
749-
self.assertTrue(name in repl_pids)
750-
self.assertTrue(repl.get_walsender_pid() == master_pids['walsenders'][0])
749+
for ptype in repl_processes:
750+
self.assertIn(ptype, repl_pids)
751+
752+
sender_pid = master_pids[ProcessType.WalSender][0]
753+
self.assertTrue(repl.get_walsender_pid() == sender_pid)
751754

752755

753756
if __name__ == '__main__':

0 commit comments

Comments
 (0)