3 # Copyright 2004-present Facebook, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
24 class FatalTests(unittest.TestCase):
26 fatal_helper_env = os.environ.get('FOLLY_FATAL_HELPER')
28 self.helper = fatal_helper_env
30 build_dir = os.path.join(os.getcwd(), 'buck-out', 'gen')
31 self.helper = os.path.join(build_dir, 'folly', 'experimental',
32 'logging', 'test', 'fatal_helper')
34 def run_helper(self, *args):
37 Check that it crashes with SIGABRT and prints nothing on stdout.
38 Returns the data printed to stderr.
42 p = subprocess.Popen(cmd,
43 stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE)
45 out, err = p.communicate()
48 self.assertEqual(status, -signal.SIGABRT)
49 self.assertEqual(out, b'')
52 def glog_crash_regex(self):
54 br'^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] test program crashing!$',
58 err = self.run_helper('--handler_style=async')
59 self.assertRegex(err, self.glog_crash_regex())
61 def test_immediate(self):
62 err = self.run_helper('--handler_style=immediate')
63 self.assertRegex(err, self.glog_crash_regex())
66 # The fatal message should be printed directly to stderr when there
67 # are no logging handlers configured.
68 err = self.run_helper('--handler_style=none')
70 br'^FATAL:.*/FatalHelper.cpp:[0-9]+: test program crashing!$',
72 self.assertRegex(err, self.glog_crash_regex())
74 def test_other_category(self):
75 err = self.run_helper('--category=foo.bar',
76 '--logging', '.=FATAL')
78 br'^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] '
79 br'crashing to category foo\.bar$',
81 self.assertRegex(err, regex)