| 1 |
Test UI worker interaction |
| 2 |
|
| 3 |
$ cat > t.py <<EOF |
| 4 |
> import sys |
| 5 |
> import time |
| 6 |
> from mercurial import ( |
| 7 |
> error, |
| 8 |
> registrar, |
| 9 |
> ui as uimod, |
| 10 |
> worker, |
| 11 |
> ) |
| 12 |
> sys.unraisablehook = lambda x: None |
| 13 |
> def abort(ui, args): |
| 14 |
> if args[0] == 0: |
| 15 |
> # by first worker for test stability |
| 16 |
> raise error.Abort(b'known exception') |
| 17 |
> return runme(ui, []) |
| 18 |
> def exc(ui, args): |
| 19 |
> if args[0] == 0: |
| 20 |
> # by first worker for test stability |
| 21 |
> raise Exception('unknown exception') |
| 22 |
> return runme(ui, []) |
| 23 |
> def runme(ui, args): |
| 24 |
> for arg in args: |
| 25 |
> ui.status(b'run\n') |
| 26 |
> yield 1, arg |
| 27 |
> time.sleep(0.1) # easier to trigger killworkers code path |
| 28 |
> functable = { |
| 29 |
> b'abort': abort, |
| 30 |
> b'exc': exc, |
| 31 |
> b'runme': runme, |
| 32 |
> } |
| 33 |
> cmdtable = {} |
| 34 |
> command = registrar.command(cmdtable) |
| 35 |
> @command(b'test', [], b'hg test [COST] [FUNC]') |
| 36 |
> def t(ui, repo, cost=1.0, func=b'runme'): |
| 37 |
> cost = float(cost) |
| 38 |
> func = functable[func] |
| 39 |
> ui.status(b'start\n') |
| 40 |
> runs = worker.worker(ui, cost, func, (ui,), range(8)) |
| 41 |
> for n, i in runs: |
| 42 |
> pass |
| 43 |
> ui.status(b'done\n') |
| 44 |
> EOF |
| 45 |
$ abspath=`pwd`/t.py |
| 46 |
$ hg init |
| 47 |
|
| 48 |
Run tests with worker enable by forcing a heigh cost |
| 49 |
|
| 50 |
$ hg --config "extensions.t=$abspath" test 100000.0 |
| 51 |
start |
| 52 |
run |
| 53 |
run |
| 54 |
run |
| 55 |
run |
| 56 |
run |
| 57 |
run |
| 58 |
run |
| 59 |
run |
| 60 |
done |
| 61 |
|
| 62 |
Run tests without worker by forcing a low cost |
| 63 |
|
| 64 |
$ hg --config "extensions.t=$abspath" test 0.0000001 |
| 65 |
start |
| 66 |
run |
| 67 |
run |
| 68 |
run |
| 69 |
run |
| 70 |
run |
| 71 |
run |
| 72 |
run |
| 73 |
run |
| 74 |
done |
| 75 |
|
| 76 |
#if no-windows |
| 77 |
|
| 78 |
Known exception should be caught, but printed if --traceback is enabled |
| 79 |
|
| 80 |
$ hg --config "extensions.t=$abspath" --config 'worker.numcpus=8' \ |
| 81 |
> test 100000.0 abort 2>&1 |
| 82 |
start |
| 83 |
abort: known exception |
| 84 |
[255] |
| 85 |
|
| 86 |
$ hg --config "extensions.t=$abspath" --config 'worker.numcpus=8' \ |
| 87 |
> test 100000.0 abort --traceback 2>&1 | grep -E '(WorkerError|Abort)' |
| 88 |
raise error.Abort(b'known exception') |
| 89 |
mercurial.error.Abort: known exception |
| 90 |
raise error.WorkerError(status) |
| 91 |
mercurial.error.WorkerError: 255 |
| 92 |
|
| 93 |
Traceback must be printed for unknown exceptions |
| 94 |
|
| 95 |
$ hg --config "extensions.t=$abspath" --config 'worker.numcpus=8' \ |
| 96 |
> test 100000.0 exc 2>&1 | grep '^Exception' |
| 97 |
Exception: unknown exception |
| 98 |
|
| 99 |
Workers should not do cleanups in all cases |
| 100 |
|
| 101 |
$ cat > $TESTTMP/detectcleanup.py <<EOF |
| 102 |
> import atexit |
| 103 |
> import os |
| 104 |
> import sys |
| 105 |
> import time |
| 106 |
> sys.unraisablehook = lambda x: None |
| 107 |
> oldfork = os.fork |
| 108 |
> count = 0 |
| 109 |
> parentpid = os.getpid() |
| 110 |
> def delayedfork(): |
| 111 |
> global count |
| 112 |
> count += 1 |
| 113 |
> pid = oldfork() |
| 114 |
> # make it easier to test SIGTERM hitting other workers when they have |
| 115 |
> # not set up error handling yet. |
| 116 |
> if count > 1 and pid == 0: |
| 117 |
> time.sleep(0.1) |
| 118 |
> return pid |
| 119 |
> os.fork = delayedfork |
| 120 |
> def cleanup(): |
| 121 |
> if os.getpid() != parentpid: |
| 122 |
> os.write(1, 'should never happen\n') |
| 123 |
> atexit.register(cleanup) |
| 124 |
> EOF |
| 125 |
|
| 126 |
$ hg --config "extensions.t=$abspath" --config worker.numcpus=8 --config \ |
| 127 |
> "extensions.d=$TESTTMP/detectcleanup.py" test 100000 abort |
| 128 |
start |
| 129 |
abort: known exception |
| 130 |
[255] |
| 131 |
|
| 132 |
Do not crash on partially read result |
| 133 |
|
| 134 |
$ cat > $TESTTMP/detecttruncated.py <<EOF |
| 135 |
> import os |
| 136 |
> import sys |
| 137 |
> import time |
| 138 |
> sys.unraisablehook = lambda x: None |
| 139 |
> oldwrite = os.write |
| 140 |
> def splitwrite(fd, string): |
| 141 |
> ret = oldwrite(fd, string[:9]) |
| 142 |
> if ret == 9: |
| 143 |
> time.sleep(0.1) |
| 144 |
> ret += oldwrite(fd, string[9:]) |
| 145 |
> return ret |
| 146 |
> os.write = splitwrite |
| 147 |
> EOF |
| 148 |
|
| 149 |
$ hg --config "extensions.t=$abspath" --config worker.numcpus=8 --config \ |
| 150 |
> "extensions.d=$TESTTMP/detecttruncated.py" test 100000.0 |
| 151 |
start |
| 152 |
run |
| 153 |
run |
| 154 |
run |
| 155 |
run |
| 156 |
run |
| 157 |
run |
| 158 |
run |
| 159 |
run |
| 160 |
done |
| 161 |
|
| 162 |
#endif |