From 53c3c2a391092a818a8aa21025256fe91f356b77 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 14 Dec 2020 18:05:16 -0800 Subject: [PATCH] add code to call a shutdown function to allow implementations to clean up --- wsfwd/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/wsfwd/__init__.py b/wsfwd/__init__.py index dcd42a4..4ba4af2 100644 --- a/wsfwd/__init__.py +++ b/wsfwd/__init__.py @@ -3,7 +3,7 @@ import contextlib import functools import json import unittest -from unittest.mock import Mock, AsyncMock +from unittest.mock import patch, Mock, AsyncMock PIPE = object() @@ -271,6 +271,15 @@ class WSFWDCommon: async def __aexit__(self, exc_type, exc, tb): self._task.cancel() + await self.shutdown() + + async def shutdown(self): + ''' + Called to clean up resources allocated. + ''' + + pass + async def _sendcmd(self, cmd): ''' Internal function for sending the dict in cmd. This @@ -396,6 +405,16 @@ class Test(unittest.IsolatedAsyncioTestCase): def runFakeServer(self, func): return asyncio.create_task(func(self.toserver.get, self.toclient.put)) + @timeout(2) + @patch('wsfwd.WSFWDCommon.shutdown') + async def test_shutdown(self, shut): + # make sure that a with block + async with WSFWDCommon(None, None) as st: + pass + + # calls shutdown + shut.assert_called() + @timeout(2) async def test_authfail(self): async def fake_server(reader, writer):