From cb63ba910df177d085e11bb8e51a17172ef42c48 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 23 Jan 2020 01:11:51 -0800 Subject: [PATCH] use stick channels to better divorce the back end... Also, properly clean up the app on one of the tests, and do a better job on another... --- solardash/__init__.py | 45 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/solardash/__init__.py b/solardash/__init__.py index 8687671..0151469 100644 --- a/solardash/__init__.py +++ b/solardash/__init__.py @@ -284,16 +284,22 @@ class Test(unittest.TestCase): async def test_staticfiles(self): runner = web.AppRunner(self._app) await runner.setup() - site = web.TCPSite(runner, 'localhost', self._webport) - await site.start() + try: + site = web.TCPSite(runner, 'localhost', self._webport) + await site.start() + + async with aiohttp.ClientSession() as session, \ + session.get(self.makeurl('')) as req: + #print(repr(req)) + self.assertEqual(req.status, 200) + body = await req.content.read() + with open(os.path.join(self.rootdir, 'index.html'), 'rb') as fp: + self.assertEqual(body, fp.read()) + + finally: + await runner.cleanup() - async with aiohttp.ClientSession() as session, \ - session.get(self.makeurl('')) as req: - #print(repr(req)) - self.assertEqual(req.status, 200) - body = await req.content.read() - with open(os.path.join(self.rootdir, 'index.html'), 'rb') as fp: - self.assertEqual(body, fp.read()) + # XXX - add test where websocket is still connected when shutting down @async_test async def test_daemon(self): @@ -301,16 +307,17 @@ class Test(unittest.TestCase): runner = web.AppRunner(self._app) await runner.setup() - site = web.TCPSite(runner, 'localhost', self._webport) - await site.start() + try: + site = web.TCPSite(runner, 'localhost', self._webport) + await site.start() - with open(os.path.join(self.tempdir, 'raineagle.0.log'), 'w', buffering=1) as fp: - fp.write('\t'.join([ 'm', '1578879268.23', '1578850464', 'Connected', '1.2740', '3.992000', 'kW', '85.575', '8.092', 'kWh' ]) + '\n') - loop.call_later(.02, fp.write, '\t'.join([ 'm', '1578879269.23', '1578850465', 'Connected', '1.0000', '3.992000', 'kW', '85.575', '8.092', 'kWh' ]) + '\n') + with open(os.path.join(self.tempdir, 'raineagle.0.log'), 'w', buffering=1) as fp: + fp.write('\t'.join([ 'm', '1578879268.23', '1578850464', 'Connected', '1.2740', '3.992000', 'kW', '85.575', '8.092', 'kWh' ]) + '\n') + loop.call_later(.02, fp.write, '\t'.join([ 'm', '1578879269.23', '1578850465', 'Connected', '1.0000', '3.992000', 'kW', '85.575', '8.092', 'kWh' ]) + '\n') + + # Test the websocket + r = [] - # Test the websocket - r = [] - try: async with aiohttp.ClientSession() as session, \ session.ws_connect(self.makeurl('solar.ws')) as ws: async for msg in ws: @@ -325,5 +332,5 @@ class Test(unittest.TestCase): self.assertEqual(r[1].type, aiohttp.WSMsgType.TEXT) self.assertEqual(r[1].data, 'ng 1.0000') - finally: - await runner.cleanup() + finally: + await runner.cleanup()