Philipp Hörist pushed to branch master at gajim / python-nbxmpp


Commits:
84f80b2a by Philipp Hörist at 2023-05-28T21:56:00+02:00
new: Tests: Add decorator to catch all exceptions

- - - - -


2 changed files:

- test/lib/util.py
- test/unit/test_http.py


Changes:

=====================================
test/lib/util.py
=====================================
@@ -1,3 +1,4 @@
+import sys
 import unittest
 from unittest.mock import Mock
 
@@ -18,3 +19,29 @@ class StanzaHandlerTest(unittest.TestCase):
 
         self.dispatcher.reset_parser()
         self.dispatcher.process_data(STREAM_START)
+
+
+def raise_all_exceptions(func):
+    # Exceptions which are raised from async callbacks
+    # in GLib or GTK do not bubble up to the unittest
+    # This decorator catches all exceptions and raises them
+    # after the unittest
+    def func_wrapper(self, *args, **kwargs):
+
+        exceptions = []
+
+        def on_hook(type_, value, tback):
+            exceptions.append((type_, value, tback))
+
+        orig_excepthook = sys.excepthook
+        sys.excepthook = on_hook
+        try:
+            result = func(self)
+        finally:
+            sys.excepthook = orig_excepthook
+            if exceptions:
+                tp, value, tb = exceptions[0]
+                raise tp(value).with_traceback(tb)
+
+        return result
+    return func_wrapper


=====================================
test/unit/test_http.py
=====================================
@@ -9,6 +9,7 @@ from gi.repository import Soup
 
 from nbxmpp.const import HTTPRequestError
 from nbxmpp.http import HTTPSession
+from test.lib.util import raise_all_exceptions
 
 
 SMALL_FILE_URL = 'https://gajim.org/downloads/ci/unittest_small_file'  # 200 KB
@@ -236,6 +237,7 @@ class HTTP(unittest.TestCase):
         self.assertTrue(request4.is_finished())
         self.assertTrue(request4.is_complete())
 
+    @raise_all_exceptions
     def test_content_overflow(self):
 
         mainloop = GLib.MainLoop()
@@ -243,7 +245,6 @@ class HTTP(unittest.TestCase):
         session = HTTPSession()
         request = session.create_request()
 
-
         def _on_starting(req) -> None:
             req._received_size = 100000000000
 



View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/84f80b2a1dcb664d58938027926a73ed90460f5a

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/84f80b2a1dcb664d58938027926a73ed90460f5a
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to