bitbake: tests: Add Timeout class

The class and exception aim to test rare cases there deadlocks are
possible.
Can be used in context managers:
with Timeout(<value>):
        do_deadlock()

(Bitbake rev: c5fcdd804d422f959a189b270d72123a50e74da6)

Signed-off-by: Pavel Zhukov <pavel@zhukoff.net>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Pavel Zhukov
2022-08-26 10:40:30 +02:00
committed by Richard Purdie
parent 0abd64b3af
commit 492ec14a37

View File

@@ -11,6 +11,7 @@ import hashlib
import tempfile import tempfile
import collections import collections
import os import os
import signal
import tarfile import tarfile
from bb.fetch2 import URI from bb.fetch2 import URI
from bb.fetch2 import FetchMethod from bb.fetch2 import FetchMethod
@@ -22,6 +23,24 @@ def skipIfNoNetwork():
return unittest.skip("network test") return unittest.skip("network test")
return lambda f: f return lambda f: f
class TestTimeout(Exception):
pass
class Timeout():
def __init__(self, seconds):
self.seconds = seconds
def handle_timeout(self, signum, frame):
raise TestTimeout("Test failed: timeout reached")
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, exc_type, exc_val, exc_tb):
signal.alarm(0)
class URITest(unittest.TestCase): class URITest(unittest.TestCase):
test_uris = { test_uris = {
"http://www.google.com/index.html" : { "http://www.google.com/index.html" : {