blob: f85e7d9792370999a0687fc9cc16737e67ea83c3 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001# Copyright (C) 2016 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004from . import OETestDecorator, registerDecorator
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005
6import signal
7from threading import Timer
8
9from oeqa.core.threaded import OETestRunnerThreaded
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010from oeqa.core.exception import OEQATimeoutError
11
12@registerDecorator
13class OETimeout(OETestDecorator):
14 attrs = ('oetimeout',)
15
16 def setUpDecorator(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017 self.logger.debug("Setting up a %d second(s) timeout" % self.oetimeout)
18
19 if isinstance(self.case.tc.runner, OETestRunnerThreaded):
20 self.timeouted = False
21 def _timeoutHandler():
22 self.timeouted = True
23
24 self.timer = Timer(self.oetimeout, _timeoutHandler)
25 self.timer.start()
26 else:
27 timeout = self.oetimeout
28 def _timeoutHandler(signum, frame):
29 raise OEQATimeoutError("Timed out after %s "
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030 "seconds of execution" % timeout)
31
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 self.alarmSignal = signal.signal(signal.SIGALRM, _timeoutHandler)
33 signal.alarm(self.oetimeout)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034
35 def tearDownDecorator(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050036 if isinstance(self.case.tc.runner, OETestRunnerThreaded):
37 self.timer.cancel()
38 self.logger.debug("Removed Timer handler")
39 if self.timeouted:
40 raise OEQATimeoutError("Timed out after %s "
41 "seconds of execution" % self.oetimeout)
42 else:
43 signal.alarm(0)
44 signal.signal(signal.SIGALRM, self.alarmSignal)
45 self.logger.debug("Removed SIGALRM handler")