Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 2 | # Copyright (C) 2016 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 6 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | import signal |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 8 | from . import OETestDecorator, registerDecorator |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 9 | from oeqa.core.exception import OEQATimeoutError |
| 10 | |
| 11 | @registerDecorator |
| 12 | class OETimeout(OETestDecorator): |
| 13 | attrs = ('oetimeout',) |
| 14 | |
| 15 | def setUpDecorator(self): |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 16 | timeout = self.oetimeout |
| 17 | def _timeoutHandler(signum, frame): |
| 18 | raise OEQATimeoutError("Timed out after %s " |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 19 | "seconds of execution" % timeout) |
| 20 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 21 | self.logger.debug("Setting up a %d second(s) timeout" % self.oetimeout) |
| 22 | self.alarmSignal = signal.signal(signal.SIGALRM, _timeoutHandler) |
| 23 | signal.alarm(self.oetimeout) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | |
| 25 | def tearDownDecorator(self): |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 26 | signal.alarm(0) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 27 | if hasattr(self, 'alarmSignal'): |
| 28 | signal.signal(signal.SIGALRM, self.alarmSignal) |
| 29 | self.logger.debug("Removed SIGALRM handler") |