diff options
| author | Mike Frysinger <vapier@google.com> | 2021-02-18 23:28:32 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2021-02-19 20:06:03 +0000 |
| commit | 057905fa1d074e6dd341822e5a6a1e49b6b97a21 (patch) | |
| tree | 4fe23676208a9c0145e08879db4f0b9b8e5268a9 /tests | |
| parent | 401c6f072564966437a74dc2f33280a85d79dc84 (diff) | |
| download | git-repo-057905fa1d074e6dd341822e5a6a1e49b6b97a21.tar.gz | |
error: fix pickling of all exceptions
Make sure all our custom exceptions can be pickled so that if they
get thrown in a multiprocess subprocess, we don't crash & hang due
to multiprocessing being unable to pickle+unpickle the exception.
Details/examples can be seen in Python reports like:
https://bugs.python.org/issue13751
Change-Id: Iddf14d3952ad4e2867cfc71891d6b6559130df4b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/297382
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_error.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_error.py b/tests/test_error.py new file mode 100644 index 00000000..82b00c24 --- /dev/null +++ b/tests/test_error.py | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | # Copyright 2021 The Android Open Source Project | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | """Unittests for the error.py module.""" | ||
| 16 | |||
| 17 | import inspect | ||
| 18 | import pickle | ||
| 19 | import unittest | ||
| 20 | |||
| 21 | import error | ||
| 22 | |||
| 23 | |||
| 24 | class PickleTests(unittest.TestCase): | ||
| 25 | """Make sure all our custom exceptions can be pickled.""" | ||
| 26 | |||
| 27 | def getExceptions(self): | ||
| 28 | """Return all our custom exceptions.""" | ||
| 29 | for name in dir(error): | ||
| 30 | cls = getattr(error, name) | ||
| 31 | if isinstance(cls, type) and issubclass(cls, Exception): | ||
| 32 | yield cls | ||
| 33 | |||
| 34 | def testExceptionLookup(self): | ||
| 35 | """Make sure our introspection logic works.""" | ||
| 36 | classes = list(self.getExceptions()) | ||
| 37 | self.assertIn(error.HookError, classes) | ||
| 38 | # Don't assert the exact number to avoid being a change-detector test. | ||
| 39 | self.assertGreater(len(classes), 10) | ||
| 40 | |||
| 41 | def testPickle(self): | ||
| 42 | """Try to pickle all the exceptions.""" | ||
| 43 | for cls in self.getExceptions(): | ||
| 44 | args = inspect.getfullargspec(cls.__init__).args[1:] | ||
| 45 | obj = cls(*args) | ||
| 46 | p = pickle.dumps(obj) | ||
| 47 | try: | ||
| 48 | newobj = pickle.loads(p) | ||
| 49 | except Exception as e: # pylint: disable=broad-except | ||
| 50 | self.fail('Class %s is unable to be pickled: %s\n' | ||
| 51 | 'Incomplete super().__init__(...) call?' % (cls, e)) | ||
| 52 | self.assertIsInstance(newobj, cls) | ||
| 53 | self.assertEqual(str(obj), str(newobj)) | ||
