Skip to content

Commit 48001e0

Browse files
authored
fix: fix parent_conn not initialize (#11)
* fix: fix parent_conn not init Signed-off-by: Zxilly <[email protected]> * test: change var type Signed-off-by: Zxilly <[email protected]> * test: set environment check Signed-off-by: Zxilly <[email protected]> * test: fix wrong password Signed-off-by: Zxilly <[email protected]> * ci: fix database setup Signed-off-by: Zxilly <[email protected]> * ci: add support for different system Signed-off-by: Zxilly <[email protected]> * ci: change password on Windows Signed-off-by: Zxilly <[email protected]> * ci: run database natively Signed-off-by: Zxilly <[email protected]> * ci: fix coverall Signed-off-by: Zxilly <[email protected]> * ci: coverage use native database Signed-off-by: Zxilly <[email protected]> * style: format with black Signed-off-by: Zxilly <[email protected]>
1 parent cfef96c commit 48001e0

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

.github/workflows/release.yml

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
python-version: [ 3.5, 3.6, 3.7, 3.8, 3.9 ]
15-
os: [ ubuntu-latest, macOS-latest ]
14+
python-version: [ 3.6, 3.7, 3.8, 3.9 ]
15+
os: [ ubuntu-latest, macOS-latest, windows-latest]
1616

1717
steps:
1818
- name: Checkout
@@ -23,12 +23,40 @@ jobs:
2323
with:
2424
python-version: ${{ matrix.python-version }}
2525

26-
- name: Setup PostgreSQL
27-
uses: Harmon758/[email protected]
28-
with:
29-
postgresql db: postgres
30-
postgresql user: postgres
31-
postgresql password: 123456
26+
- name: Set up PostgreSQL on Linux
27+
if: startsWith(runner.os, 'linux')
28+
run: |
29+
sudo systemctl start postgresql.service
30+
pg_isready
31+
sudo -u postgres psql --command="ALTER USER postgres WITH PASSWORD '123456'" --command="\du"
32+
33+
- name: Setup PostgreSQL on macOS
34+
if: startsWith(runner.os, 'macos')
35+
run: |
36+
brew services start postgresql
37+
echo "Check PostgreSQL service is running"
38+
i=10
39+
COMMAND='pg_isready'
40+
while [ $i -gt 0 ]; do
41+
echo "Check PostgreSQL service status"
42+
eval $COMMAND && break
43+
((i--))
44+
if [ $i == 0 ]; then
45+
echo "PostgreSQL service not ready, all attempts exhausted"
46+
exit 1
47+
fi
48+
echo "PostgreSQL service not ready, wait 10 more sec, attempts left: $i"
49+
sleep 10
50+
done
51+
psql --command="CREATE USER postgres PASSWORD '123456'" --command="\du" postgres
52+
53+
- name: Start PostgreSQL on Windows
54+
if: startsWith(runner.os, 'windows')
55+
run: |
56+
$pgService = Get-Service -Name postgresql*
57+
Set-Service -InputObject $pgService -Status running -StartupType automatic
58+
Start-Process -FilePath "$env:PGBIN\pg_isready" -Wait -PassThru
59+
& $env:PGBIN\psql --command="ALTER USER postgres WITH PASSWORD '123456'" --command="\du"
3260
3361
- name: Install dependencies
3462
run: |
@@ -54,13 +82,15 @@ jobs:
5482
run: |
5583
pip install -r requirements.txt
5684
pip install -r dev_requirements.txt
85+
pip install coveralls
86+
pip install coverage
5787
58-
- name: Setup PostgreSQL
59-
uses: Harmon758/[email protected]
60-
with:
61-
postgresql db: postgres
62-
postgresql user: postgres
63-
postgresql password: 123456
88+
- name: Set up PostgreSQL on Linux
89+
if: startsWith(runner.os, 'linux')
90+
run: |
91+
sudo systemctl start postgresql.service
92+
pg_isready
93+
sudo -u postgres psql --command="ALTER USER postgres WITH PASSWORD '123456'" --command="\du"
6494
6595
- name: Run tests
6696
run: coverage run -m unittest discover -s tests -t tests

postgresql_watcher/watcher.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def casbin_subscription(
11-
process_conn: connection.PipeConnection,
11+
process_conn: Pipe,
1212
host: str,
1313
user: str,
1414
password: str,
@@ -34,7 +34,6 @@ def casbin_subscription(
3434
process_conn.put(notify.payload)
3535

3636

37-
3837
class PostgresqlWatcher(object):
3938
def __init__(
4039
self,
@@ -45,21 +44,23 @@ def __init__(
4544
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME,
4645
start_process: Optional[bool] = True,
4746
):
47+
self.update_callback = None
48+
self.parent_conn = None
4849
self.host = host
4950
self.port = port
5051
self.user = user
5152
self.password = password
5253
self.channel_name = channel_name
53-
self.subscribed_process, self.parent_conn = self.create_subscriber_process(
54-
start_process
55-
)
54+
self.subscribed_process = self.create_subscriber_process(start_process)
5655

5756
def create_subscriber_process(
5857
self,
5958
start_process: Optional[bool] = True,
6059
delay: Optional[int] = 2,
6160
):
6261
parent_conn, child_conn = Pipe()
62+
if not self.parent_conn:
63+
self.parent_conn = parent_conn
6364
p = Process(
6465
target=casbin_subscription,
6566
args=(
@@ -76,16 +77,12 @@ def create_subscriber_process(
7677
if start_process:
7778
p.start()
7879
self.should_reload()
79-
return p, parent_conn
80+
return p
8081

81-
def update_callback(self):
82-
print("callback called because casbin role updated")
83-
84-
def set_update_callback(self, fn_name: Any):
85-
print("runtime is set update callback",fn_name)
82+
def set_update_callback(self, fn_name: Callable):
83+
print("runtime is set update callback", fn_name)
8684
self.update_callback = fn_name
8785

88-
8986
def update(self):
9087
conn = connect(
9188
host=self.host,
@@ -116,4 +113,4 @@ def should_reload(self):
116113
self.subscribed_process, self.parent_conn = self.create_subscriber_process(
117114
delay=10
118115
)
119-
return False
116+
return False

tests/test_postgresql_watcher.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import sys
12
import unittest
3+
from multiprocessing.connection import Pipe
4+
25
from postgresql_watcher import PostgresqlWatcher
36
from multiprocessing import connection, context
47

@@ -10,16 +13,26 @@
1013

1114

1215
def get_watcher():
13-
pg_watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD)
14-
return pg_watcher
16+
return PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD)
1517

1618

1719
pg_watcher = get_watcher()
1820

21+
try:
22+
import _winapi
23+
from _winapi import WAIT_OBJECT_0, WAIT_ABANDONED_0, WAIT_TIMEOUT, INFINITE
24+
except ImportError:
25+
if sys.platform == 'win32':
26+
raise
27+
_winapi = None
28+
1929

2030
class TestConfig(unittest.TestCase):
2131
def test_pg_watcher_init(self):
22-
assert isinstance(pg_watcher.parent_conn, connection.PipeConnection)
32+
if _winapi:
33+
assert isinstance(pg_watcher.parent_conn, connection.PipeConnection)
34+
else:
35+
assert isinstance(pg_watcher.parent_conn, connection.Connection)
2336
assert isinstance(pg_watcher.subscribed_process, context.Process)
2437

2538
def test_update_pg_watcher(self):
@@ -40,4 +53,4 @@ def _test_callback():
4053

4154

4255
if __name__ == "__main__":
43-
unittest.main()
56+
unittest.main()

0 commit comments

Comments
 (0)