tests/test-worker.t
author Jun Wu <quark@fb.com>
Sat, 22 Apr 2017 17:00:50 -0700
changeset 32113 9f0c055eebae
parent 32112 31763785094b
child 32114 44a98a2ea431
permissions -rw-r--r--
test-worker: capture tracebacks more reliably The traceback test may have traceback caused by SIGTERM. Instead of grepping "Traceback", explicitly grep the exception we care about. This makes the test less flaky.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     1
Test UI worker interaction
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     2
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     3
  $ cat > t.py <<EOF
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     4
  > from __future__ import absolute_import, print_function
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     5
  > from mercurial import (
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     6
  >     cmdutil,
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
     7
  >     error,
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     8
  >     ui as uimod,
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
     9
  >     worker,
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    10
  > )
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    11
  > def abort(ui, args):
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    12
  >     if args[0] == 0:
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    13
  >         # by first worker for test stability
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    14
  >         raise error.Abort('known exception')
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    15
  >     return runme(ui, [])
32043
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    16
  > def exc(ui, args):
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    17
  >     if args[0] == 0:
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    18
  >         # by first worker for test stability
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    19
  >         raise Exception('unknown exception')
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    20
  >     return runme(ui, [])
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    21
  > def runme(ui, args):
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    22
  >     for arg in args:
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    23
  >         ui.status('run\n')
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    24
  >         yield 1, arg
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    25
  > functable = {
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    26
  >     'abort': abort,
32043
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    27
  >     'exc': exc,
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    28
  >     'runme': runme,
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    29
  > }
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    30
  > cmdtable = {}
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    31
  > command = cmdutil.command(cmdtable)
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    32
  > @command('test', [], 'hg test [COST] [FUNC]')
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    33
  > def t(ui, repo, cost=1.0, func='runme'):
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    34
  >     cost = float(cost)
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    35
  >     func = functable[func]
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    36
  >     ui.status('start\n')
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    37
  >     runs = worker.worker(ui, cost, func, (ui,), range(8))
31696
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    38
  >     for n, i in runs:
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    39
  >         pass
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    40
  >     ui.status('done\n')
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    41
  > EOF
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    42
  $ abspath=`pwd`/t.py
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    43
  $ hg init
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    44
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    45
Run tests with worker enable by forcing a heigh cost
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    46
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    47
  $ hg --config "extensions.t=$abspath" test 100000.0
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    48
  start
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    49
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    50
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    51
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    52
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    53
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    54
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    55
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    56
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    57
  done
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    58
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    59
Run tests without worker by forcing a low cost
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    60
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    61
  $ hg --config "extensions.t=$abspath" test 0.0000001
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    62
  start
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    63
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    64
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    65
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    66
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    67
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    68
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    69
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    70
  run
9d3d56aa1a9f worker: flush ui buffers before running the worker
David Soria Parra <davidsp@fb.com>
parents:
diff changeset
    71
  done
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    72
32061
6e0368b6e0bb test-worker: disable tests of forked workers on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 32043
diff changeset
    73
#if no-windows
6e0368b6e0bb test-worker: disable tests of forked workers on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 32043
diff changeset
    74
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    75
Known exception should be caught, but printed if --traceback is enabled
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    76
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    77
  $ hg --config "extensions.t=$abspath" --config 'worker.numcpus=2' \
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    78
  > test 100000.0 abort
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    79
  start
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    80
  abort: known exception
32042
8f8ad0139b8b worker: propagate exit code to main process
Yuya Nishihara <yuya@tcha.org>
parents: 32041
diff changeset
    81
  [255]
32041
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    82
38963a53ab0d dispatch: print traceback in scmutil.callcatch() if --traceback specified
Yuya Nishihara <yuya@tcha.org>
parents: 31696
diff changeset
    83
  $ hg --config "extensions.t=$abspath" --config 'worker.numcpus=2' \
32113
9f0c055eebae test-worker: capture tracebacks more reliably
Jun Wu <quark@fb.com>
parents: 32112
diff changeset
    84
  > test 100000.0 abort --traceback 2>&1 | egrep '^(SystemExit|Abort)'
9f0c055eebae test-worker: capture tracebacks more reliably
Jun Wu <quark@fb.com>
parents: 32112
diff changeset
    85
  Abort: known exception
9f0c055eebae test-worker: capture tracebacks more reliably
Jun Wu <quark@fb.com>
parents: 32112
diff changeset
    86
  SystemExit: 255
32043
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    87
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    88
Traceback must be printed for unknown exceptions
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    89
b844d0d367e2 worker: print traceback for uncaught exception unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 32042
diff changeset
    90
  $ hg --config "extensions.t=$abspath" --config 'worker.numcpus=2' \
32113
9f0c055eebae test-worker: capture tracebacks more reliably
Jun Wu <quark@fb.com>
parents: 32112
diff changeset
    91
  > test 100000.0 exc 2>&1 | grep '^Exception'
9f0c055eebae test-worker: capture tracebacks more reliably
Jun Wu <quark@fb.com>
parents: 32112
diff changeset
    92
  Exception: unknown exception
32061
6e0368b6e0bb test-worker: disable tests of forked workers on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 32043
diff changeset
    93
32112
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    94
Workers should not do cleanups in all cases
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    95
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    96
  $ cat > $TESTTMP/detectcleanup.py <<EOF
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    97
  > from __future__ import absolute_import
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    98
  > import atexit
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
    99
  > import os
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   100
  > import time
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   101
  > oldfork = os.fork
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   102
  > count = 0
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   103
  > parentpid = os.getpid()
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   104
  > def delayedfork():
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   105
  >     global count
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   106
  >     count += 1
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   107
  >     pid = oldfork()
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   108
  >     # make it easier to test SIGTERM hitting other workers when they have
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   109
  >     # not set up error handling yet.
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   110
  >     if count > 1 and pid == 0:
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   111
  >         time.sleep(0.1)
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   112
  >     return pid
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   113
  > os.fork = delayedfork
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   114
  > def cleanup():
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   115
  >     if os.getpid() != parentpid:
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   116
  >         os.write(1, 'should never happen\n')
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   117
  > atexit.register(cleanup)
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   118
  > EOF
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   119
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   120
  $ hg --config "extensions.t=$abspath" --config worker.numcpus=8 --config \
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   121
  > "extensions.d=$TESTTMP/detectcleanup.py" test 100000 abort
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   122
  start
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   123
  abort: known exception
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   124
  [255]
31763785094b worker: rewrite error handling so os._exit covers all cases
Jun Wu <quark@fb.com>
parents: 32061
diff changeset
   125
32061
6e0368b6e0bb test-worker: disable tests of forked workers on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 32043
diff changeset
   126
#endif