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