'Check how many times the traceback is written in timeout x 2.5 seconds,
or timeout x 3.5 seconds if cancel is True: 1, 2 or 3 times depending
on repeat and cancel options.
Raise an error if the output doesn\'t match the expect format.'

def _check_dump_traceback_later(self, repeat, cancel, filename, loops):

    timeout_str = str(datetime.timedelta(seconds=TIMEOUT))
    code = '\n                                                import    faulthandler\n                                                import    time\n\n                                                def    func(timeout,    repeat,    cancel,    file,    loops):\n                                                                for    loop    in    range(loops):\n                                                                                faulthandler.dump_traceback_later(timeout,    repeat=repeat,    file=file)\n                                                                                if    cancel:\n                                                                                                faulthandler.cancel_dump_traceback_later()\n                                                                                time.sleep(timeout    *    5)\n                                                                                faulthandler.cancel_dump_traceback_later()\n\n                                                timeout    =    {timeout}\n                                                repeat    =    {repeat}\n                                                cancel    =    {cancel}\n                                                loops    =    {loops}\n                                                if    {has_filename}:\n                                                                file    =    open({filename},    "wb")\n                                                else:\n                                                                file    =    None\n                                                func(timeout,    repeat,    cancel,    file,    loops)\n                                                if    file    is    not    None:\n                                                                file.close()\n                                                '
    code = code.format(timeout=TIMEOUT, repeat=repeat, cancel=cancel, loops=loops, has_filename=bool(filename), filename=repr(filename))
    (trace, exitcode) = self.get_output(code, filename)
    trace = '\n'.join(trace)
    if (not cancel):
        count = loops
        if repeat:
            count *= 2
        header = ('Timeout    \\(%s\\)!\\nThread    0x[0-9a-f]+    \\(most    recent    call    first\\):\\n' % timeout_str)
        regex = expected_traceback(9, 20, header, min_count=count)
        self.assertRegex(trace, regex)
    else:
        self.assertEqual(trace, '')
    self.assertEqual(exitcode, 0)
