Changeset 7eb4233 in mod_gnutls
- Timestamp:
- Nov 28, 2020, 3:29:24 PM (18 months ago)
- Branches:
- asyncio, master
- Children:
- 65e66c9
- Parents:
- 32e62a3
- Location:
- test
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
test/mgstest/services.py
r32e62a3 r7eb4233 15 15 """Handling services needed for mod_gnutls tests""" 16 16 17 import asyncio 17 18 import os 18 import subprocess19 19 20 from contextlib import contextmanager20 from contextlib import asynccontextmanager 21 21 from pathlib import Path 22 from time import sleep23 22 24 23 … … 58 57 self._step = int(os.environ.get('TEST_SERVICE_WAIT', 250)) / 1000 59 58 60 def start(self):59 async def start(self): 61 60 """Start the service""" 62 61 if not self.condition(): … … 64 63 return 65 64 print(f'Starting: {self.start_command}') 66 self.process = subprocess.Popen(self.start_command, 67 env=self.process_env, 68 close_fds=True) 65 self.process = await asyncio.create_subprocess_exec( 66 *self.start_command, env=self.process_env, close_fds=True) 69 67 self.returncode = None 70 68 71 def stop(self):69 async def stop(self): 72 70 """Order the service to stop""" 73 71 if not self.condition(): 74 72 # skip 75 73 return 76 if not self.process or self.process. poll():74 if not self.process or self.process.returncode is not None: 77 75 # process either never started or already stopped 78 76 return … … 80 78 if self.stop_command: 81 79 print(f'Stopping: {self.stop_command}') 82 subprocess.run(self.stop_command, check=True, env=self.process_env) 80 stop = await asyncio.create_subprocess_exec( 81 *self.stop_command, env=self.process_env) 82 await stop.wait() 83 83 else: 84 84 print(f'Stopping (SIGTERM): {self.start_command}') 85 85 self.process.terminate() 86 86 87 def wait(self, timeout=None):87 async def wait(self, timeout=None): 88 88 """Wait for the process to terminate. 89 89 90 90 Sets returncode to the process' return code and returns it. 91 91 92 WARNING: Calling this method without a timeout or calling 93 stop() first will hang. An expired timeout will raise a 94 subprocess.TimeoutExpired exception. 92 WARNING: Calling this method without calling stop() first will 93 hang, unless the service stops on its own. 95 94 96 95 """ 97 96 if self.process: 98 self.process.wait(timeout=timeout)97 await self.process.wait() 99 98 self.returncode = self.process.returncode 100 99 self.process = None 101 100 return self.returncode 102 101 103 def wait_ready(self, timeout=None):102 async def wait_ready(self, timeout=None): 104 103 """Wait for the started service to be ready. 105 104 … … 114 113 115 114 """ 115 if not self.condition(): 116 # skip 117 return None 116 118 if not self.check: 117 119 return None … … 119 121 slept = 0 120 122 while not timeout or slept < timeout: 121 if self.process and self.process. poll():123 if self.process and self.process.returncode is not None: 122 124 return self.process.returncode 123 125 if self.check(): 124 126 return None 125 127 else: 126 sleep(self._step)128 await asyncio.sleep(self._step) 127 129 slept = slept + self._step 128 130 # TODO: A custom ServiceException or something would be nicer … … 130 132 raise TimeoutError('Waiting for service timed out!') 131 133 132 @ contextmanager133 def run(self):134 @asynccontextmanager 135 async def run(self, ready_timeout=None): 134 136 """Context manager to start and stop a service. Note that entering the 135 137 context does not call TestService.wait_ready() on the service, … … 138 140 """ 139 141 try: 140 self.start() 141 # TODO: with async execution we could also call 142 # wait_ready() here 142 await self.start() 143 await self.wait_ready(timeout=ready_timeout) 143 144 yield self 144 145 finally: 145 self.stop() 146 # TODO: this would really benefit from async execution 147 self.wait() 146 await self.stop() 147 await self.wait() 148 148 149 149 -
test/runtest.py
r32e62a3 r7eb4233 16 16 # limitations under the License. 17 17 18 import asyncio 18 19 import contextlib 19 20 import os … … 85 86 86 87 87 def main(args):88 async def main(args): 88 89 # The Automake environment always provides srcdir, the default is 89 90 # for manual use. … … 161 162 f'http://127.0.0.1:{os.environ["MSVA_PORT"]}' 162 163 163 with contextlib.ExitStack() as service_stack:164 async with contextlib.AsyncExitStack() as service_stack: 164 165 if cleanup_callback: 165 166 service_stack.callback(cleanup_callback) 166 167 service_stack.enter_context( 167 168 lockfile('test.lock', nolock='MGS_NETNS_ACTIVE' in os.environ)) 168 service_stack.enter_context(ocsp.run())169 service_stack.enter_context(backend.run())170 service_stack.enter_context(msva.run())171 169 172 170 # TEST_SERVICE_MAX_WAIT is in milliseconds 173 171 wait_timeout = \ 174 172 int(os.environ.get('TEST_SERVICE_MAX_WAIT', 10000)) / 1000 175 for s in bg_services: 176 if s.condition(): 177 s.wait_ready(timeout=wait_timeout) 173 await asyncio.wait( 174 {asyncio.create_task( 175 service_stack.enter_async_context( 176 s.run(ready_timeout=wait_timeout))) 177 for s in bg_services}) 178 178 179 179 # special case: expected to fail in a few cases 180 service_stack.enter_context(apache.run())181 failed = a pache.wait_ready()180 await service_stack.enter_async_context(apache.run()) 181 failed = await apache.wait_ready() 182 182 if os.path.exists(os.path.join(testdir, 'fail.server')): 183 183 if failed: … … 252 252 stack.enter_context(contextlib.closing(args.log_connection)) 253 253 stack.enter_context(contextlib.closing(args.log_responses)) 254 main(args)254 asyncio.run(main(args))
Note: See TracChangeset
for help on using the changeset viewer.