remove connection classes

This commit is contained in:
Stefan Allius
2024-10-02 23:40:42 +02:00
parent 39aba31bbd
commit cfe2c9cb9d
20 changed files with 582 additions and 780 deletions

View File

@@ -1,100 +0,0 @@
# test_with_pytest.py
import pytest
import asyncio
from itertools import count
from mock import patch
from app.src.async_stream import StreamPtr
from app.src.async_stream import AsyncStream, AsyncStreamServer, AsyncIfcImpl
from app.src.gen3.connection_g3 import ConnectionG3
from app.src.gen3.talent import Talent
class FakeInverter():
async def async_publ_mqtt(self) -> None:
pass # dummy funcion
async def async_create_remote(self, inv_prot: str, conn_class) -> None:
pass # dummy function
def __init__ (self):
self.remote = StreamPtr(None)
self.local = StreamPtr(None)
@pytest.fixture
def patch_async_init():
with patch.object(AsyncStream, '__init__') as conn:
yield conn
@pytest.fixture
def patch_talent_init():
with patch.object(Talent, '__init__') as conn:
yield conn
@pytest.fixture
def patch_healthy():
with patch.object(AsyncStream, 'healthy') as conn:
yield conn
@pytest.fixture
def patch_async_close():
with patch.object(AsyncStream, 'close') as conn:
yield conn
@pytest.fixture
def patch_talent_close():
with patch.object(Talent, 'close') as conn:
yield conn
class FakeReader():
def __init__(self):
self.on_recv = asyncio.Event()
async def read(self, max_len: int):
await self.on_recv.wait()
return b''
def feed_eof(self):
return
class FakeWriter():
def write(self, buf: bytes):
return
def get_extra_info(self, sel: str):
if sel == 'peername':
return 'remote.intern'
elif sel == 'sockname':
return 'sock:1234'
assert False
def is_closing(self):
return False
def close(self):
return
async def wait_closed(self):
return
def test_method_calls(patch_healthy, patch_async_close):
AsyncIfcImpl._ids = count(5)
spy3 = patch_healthy
spy4 = patch_async_close
reader = FakeReader()
writer = FakeWriter()
id_str = "id_string"
addr = ('proxy.local', 10000)
inv = FakeInverter()
ifc = AsyncStreamServer(reader, writer,
inv.async_publ_mqtt,
inv.async_create_remote,
inv.remote)
conn = ConnectionG3(addr, ifc, server_side=True, id_str=id_str)
assert 5 == conn.conn_no
assert 5 == conn.ifc.get_conn_no()
conn.healthy()
spy3.assert_called_once()
conn.close()
spy4.assert_called_once()

View File

@@ -1,105 +0,0 @@
# test_with_pytest.py
import pytest
import asyncio
from itertools import count
from mock import patch
from app.src.singleton import Singleton
from app.src.async_stream import StreamPtr
from app.src.async_stream import AsyncStream, AsyncStreamServer, AsyncIfcImpl
from app.src.gen3plus.connection_g3p import ConnectionG3P
from app.src.gen3plus.solarman_v5 import SolarmanV5
class FakeInverter():
async def async_publ_mqtt(self) -> None:
pass # dummy funcion
async def async_create_remote(self, inv_prot: str, conn_class) -> None:
pass # dummy function
def __init__ (self):
self.remote = StreamPtr(None)
self.local = StreamPtr(None)
@pytest.fixture
def patch_async_init():
with patch.object(AsyncStream, '__init__', return_value= None) as conn:
yield conn
@pytest.fixture
def patch_solarman_init():
with patch.object(SolarmanV5, '__init__') as conn:
yield conn
@pytest.fixture(scope="module", autouse=True)
def module_init():
Singleton._instances.clear()
yield
@pytest.fixture
def patch_healthy():
with patch.object(AsyncStream, 'healthy') as conn:
yield conn
@pytest.fixture
def patch_async_close():
with patch.object(AsyncStream, 'close') as conn:
yield conn
@pytest.fixture
def patch_solarman_close():
with patch.object(SolarmanV5, 'close') as conn:
yield conn
class FakeReader():
def __init__(self):
self.on_recv = asyncio.Event()
async def read(self, max_len: int):
await self.on_recv.wait()
return b''
def feed_eof(self):
return
class FakeWriter():
def write(self, buf: bytes):
return
def get_extra_info(self, sel: str):
if sel == 'peername':
return 'remote.intern'
elif sel == 'sockname':
return 'sock:1234'
assert False
def is_closing(self):
return False
def close(self):
return
async def wait_closed(self):
return
def test_method_calls(patch_healthy, patch_async_close):
AsyncIfcImpl._ids = count(5)
spy3 = patch_healthy
spy4 = patch_async_close
reader = FakeReader()
writer = FakeWriter()
addr = ('proxy.local', 10000)
inv = FakeInverter()
ifc = AsyncStreamServer(reader, writer,
inv.async_publ_mqtt,
inv.async_create_remote,
inv.remote)
conn = ConnectionG3P(addr, ifc, server_side=True, client_mode=False)
assert 5 == conn.conn_no
assert 5 == conn.ifc.get_conn_no()
conn.healthy()
spy3.assert_called_once()
conn.close()
spy4.assert_called_once()

View File

@@ -1,6 +1,7 @@
# test_with_pytest.py
import pytest
import asyncio
import sys,gc
from mock import patch
from enum import Enum
@@ -8,8 +9,8 @@ from app.src.infos import Infos
from app.src.config import Config
from app.src.inverter import Inverter
from app.src.singleton import Singleton
from app.src.gen3.connection_g3 import ConnectionG3
from app.src.gen3.inverter_g3 import InverterG3
from app.src.async_stream import AsyncStream
from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname
@@ -42,11 +43,6 @@ def module_init():
Singleton._instances.clear()
yield
@pytest.fixture
def patch_conn_init():
with patch.object(ConnectionG3, '__init__', return_value= None) as conn:
yield conn
class FakeReader():
def __init__(self):
self.on_recv = asyncio.Event()
@@ -98,14 +94,28 @@ def patch_open_connection():
with patch.object(asyncio, 'open_connection', new_open) as conn:
yield conn
@pytest.fixture
def patch_healthy():
with patch.object(AsyncStream, 'healthy') as conn:
yield conn
def test_method_calls():
def test_method_calls(patch_healthy):
spy = patch_healthy
reader = FakeReader()
writer = FakeWriter()
addr = ('proxy.local', 10000)
with InverterG3(reader, writer, addr) as inverter:
assert inverter.local.stream
assert inverter.local.ifc
for inv in Inverter:
inv.healthy()
del inv
spy.assert_called_once()
del inverter
cnt = 0
for inv in Inverter:
cnt += 1
assert cnt == 0
@pytest.mark.asyncio
async def test_remote_conn(config_conn, patch_open_connection):
@@ -117,6 +127,13 @@ async def test_remote_conn(config_conn, patch_open_connection):
await inverter.async_create_remote()
await asyncio.sleep(0)
assert inverter.remote.stream
del inverter
cnt = 0
for inv in Inverter:
print(f'Inverter refs:{gc.get_referrers(inv)}')
cnt += 1
assert cnt == 0
@pytest.mark.asyncio
async def test_remote_except(config_conn, patch_open_connection):
@@ -136,6 +153,13 @@ async def test_remote_except(config_conn, patch_open_connection):
await inverter.async_create_remote()
await asyncio.sleep(0)
assert inverter.remote.stream==None
del inverter
cnt = 0
for inv in Inverter:
print(f'Inverter refs:{gc.get_referrers(inv)}')
cnt += 1
assert cnt == 0
@pytest.mark.asyncio
async def test_mqtt_publish(config_conn, patch_open_connection):

View File

@@ -8,7 +8,6 @@ from app.src.infos import Infos
from app.src.config import Config
from app.src.inverter import Inverter
from app.src.singleton import Singleton
from app.src.gen3plus.connection_g3p import ConnectionG3P
from app.src.gen3plus.inverter_g3p import InverterG3P
from app.tests.test_modbus_tcp import patch_mqtt_err, patch_mqtt_except, test_port, test_hostname
@@ -43,11 +42,6 @@ def module_init():
Singleton._instances.clear()
yield
@pytest.fixture
def patch_conn_init():
with patch.object(ConnectionG3P, '__init__', return_value= None) as conn:
yield conn
class FakeReader():
def __init__(self):
self.on_recv = asyncio.Event()

View File

@@ -40,7 +40,7 @@ class FakeIfc(AsyncIfcImpl):
class MemoryStream(SolarmanV5):
def __init__(self, msg, chunks = (0,), server_side: bool = True):
_ifc = FakeIfc()
super().__init__(('test.local', 1234), server_side, client_mode=False, ifc=_ifc)
super().__init__(('test.local', 1234), _ifc, server_side, client_mode=False)
if server_side:
self.mb.timeout = 0.4 # overwrite for faster testing
self.mb_first_timeout = 0.5
@@ -1240,9 +1240,9 @@ def test_build_logger_modell(config_tsun_allow_all, device_ind_msg):
def test_msg_iterator():
Message._registry.clear()
m1 = SolarmanV5(('test1.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m2 = SolarmanV5(('test2.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m3 = SolarmanV5(('test3.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m1 = SolarmanV5(('test1.local', 1234), ifc=AsyncIfcImpl(), server_side=True, client_mode=False)
m2 = SolarmanV5(('test2.local', 1234), ifc=AsyncIfcImpl(), server_side=True, client_mode=False)
m3 = SolarmanV5(('test3.local', 1234), ifc=AsyncIfcImpl(), server_side=True, client_mode=False)
m3.close()
del m3
test1 = 0
@@ -1260,7 +1260,7 @@ def test_msg_iterator():
assert test2 == 1
def test_proxy_counter():
m = SolarmanV5(('test.local', 1234), server_side=True, client_mode=False, ifc=AsyncIfcImpl())
m = SolarmanV5(('test.local', 1234), ifc=AsyncIfcImpl(), server_side=True, client_mode=False)
assert m.new_data == {}
m.db.stat['proxy']['Unknown_Msg'] = 0
Infos.new_stat_data['proxy'] = False

View File

@@ -24,7 +24,7 @@ class FakeIfc(AsyncIfcImpl):
class MemoryStream(Talent):
def __init__(self, msg, chunks = (0,), server_side: bool = True):
self.ifc = FakeIfc()
super().__init__(('test.local', 1234), server_side, self.ifc)
super().__init__(('test.local', 1234), self.ifc, server_side)
if server_side:
self.mb.timeout = 0.4 # overwrite for faster testing
self.mb_first_timeout = 0.5
@@ -1641,9 +1641,9 @@ def test_ctrl_byte():
def test_msg_iterator():
m1 = Talent(('test1.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m2 = Talent(('test2.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m3 = Talent(('test3.local', 1234), server_side=True, ifc=AsyncIfcImpl())
m1 = Talent(('test1.local', 1234), ifc=AsyncIfcImpl(), server_side=True)
m2 = Talent(('test2.local', 1234), ifc=AsyncIfcImpl(), server_side=True)
m3 = Talent(('test3.local', 1234), ifc=AsyncIfcImpl(), server_side=True)
m3.close()
del m3
test1 = 0