]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/cephfs: make filelock_interrupt.py work with python3 32741/head
authorYan, Zheng <zyan@redhat.com>
Tue, 21 Jan 2020 02:47:51 +0000 (10:47 +0800)
committerYan, Zheng <zyan@redhat.com>
Tue, 21 Jan 2020 02:47:51 +0000 (10:47 +0800)
Fixes: https://tracker.ceph.com/issues/43513
Signed-off-by: "Yan, Zheng" <zyan@redhat.com>
qa/workunits/fs/misc/filelock_interrupt.py

index 16ed1ec3793325ff7fe76d06313567feac078856..5ebc480ba8e314f5a5a674c0dbab747fb368317a 100755 (executable)
@@ -1,10 +1,25 @@
 #!/usr/bin/python3
 
+from contextlib import contextmanager
 import errno
 import fcntl
 import signal
 import struct
 
+@contextmanager
+def timeout(seconds):
+    def timeout_handler(signum, frame):
+        raise InterruptedError
+
+    orig_handler = signal.signal(signal.SIGALRM, timeout_handler)
+    try:
+        signal.alarm(seconds)
+        yield
+    finally:
+        signal.alarm(0)
+        signal.signal(signal.SIGALRM, orig_handler)
+
+
 """
 introduced by Linux 3.15
 """
@@ -13,10 +28,6 @@ fcntl.F_OFD_SETLK = 37
 fcntl.F_OFD_SETLKW = 38
 
 
-def handler(signum, frame):
-    pass
-
-
 def main():
     f1 = open("testfile", 'w')
     f2 = open("testfile", 'w')
@@ -26,15 +37,13 @@ def main():
     """
     is flock interruptible?
     """
-    signal.signal(signal.SIGALRM, handler)
-    signal.alarm(5)
-    try:
-        fcntl.flock(f2, fcntl.LOCK_EX)
-    except IOError as e:
-        if e.errno != errno.EINTR:
-            raise
-    else:
-        raise RuntimeError("expect flock to block")
+    with timeout(5):
+        try:
+            fcntl.flock(f2, fcntl.LOCK_EX)
+        except InterruptedError:
+            pass
+        else:
+            raise RuntimeError("expect flock to block")
 
     fcntl.flock(f1, fcntl.LOCK_UN)
 
@@ -54,16 +63,14 @@ def main():
     """
     is posix lock interruptible?
     """
-    signal.signal(signal.SIGALRM, handler)
-    signal.alarm(5)
-    try:
-        lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
-        fcntl.fcntl(f2, fcntl.F_OFD_SETLKW, lockdata)
-    except IOError as e:
-        if e.errno != errno.EINTR:
-            raise
-    else:
-        raise RuntimeError("expect posix lock to block")
+    with timeout(5):
+        try:
+            lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
+            fcntl.fcntl(f2, fcntl.F_OFD_SETLKW, lockdata)
+        except InterruptedError:
+            pass
+        else:
+            raise RuntimeError("expect posix lock to block")
 
     """
     file handler 2 should still hold lock on 10~10