Mercurial > public > mercurial-scm > python-hglib
annotate tests/common.py @ 188:5609a21fe39a
client: fail gracefully on unexpected prompts (issue5516)
Right now, if hglib encounters an unexpected prompt, it fails with a rather
opaque "unexpected data on required channel 'L'" error message. Furthermore,
if subsequently another command is called on the same client instance, both
the client and server processes lock up hard (at least on Windows), and the
server process rapidly leaks ~2GB of memory.
Fix this by responding with an empty string to any unexpected prompt. This
will trigger an "abort: response expected" exception from the server, which
is easily handled as a CommandError, and subsequent commands sent from the
same client work as expected.
This doesn't completely resolve bug 5516, as unexpected requests on another
required channel (e.g. I) can still cause a lockup.
However, it does fix the most common case of an unexpected password prompt.
author | G?bor Stefanik <gabor.stefanik@nng.com> |
---|---|
date | Tue, 12 Sep 2017 13:16:36 -0400 |
parents | 6564544576b9 |
children | 8341f2494b3f |
rev | line source |
---|---|
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
1 import os, sys, tempfile, shutil |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
2 import unittest |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
3 |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
4 import hglib |
101
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
5 from hglib import client |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
6 |
67
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
7 def resultappender(list): |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
8 def decorator(f): |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
9 def decorated(*args, **kwargs): |
101
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
10 list.append(args[0]) |
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
11 return f(*args, **kwargs) |
67
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
12 return decorated |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
13 return decorator |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
14 |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
15 class basetest(unittest.TestCase): |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
16 def setUp(self): |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
17 self._testtmp = os.environ["TESTTMP"] = os.environ["HOME"] = \ |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
18 os.path.join(os.environ["HGTMP"], self.__class__.__name__) |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
19 |
67
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
20 self.clients = [] |
101
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
21 self._oldopen = hglib.client.hgclient.open |
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
22 # hglib.open = resultappender(self.clients)(hglib.open) |
134 | 23 c = hglib.client.hgclient |
24 c.open = resultappender(self.clients)(c.open) | |
67
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
25 |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
26 os.mkdir(self._testtmp) |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
27 os.chdir(self._testtmp) |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
28 # until we can run norepo commands in the cmdserver |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
29 os.system('hg init') |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
30 self.client = hglib.open() |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
31 |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
32 def tearDown(self): |
134 | 33 # on Windows we cannot rmtree before closing all instances |
34 # because of used files | |
101
d1a42c1d0b61
tests: hook into hgclient.open for closing open cmdservers
Idan Kamara <idankk86@gmail.com>
parents:
68
diff
changeset
|
35 hglib.client.hgclient.open = self._oldopen |
67
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
36 for client in self.clients: |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
37 if client.server is not None: |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
38 client.close() |
730c42743ba3
tests: cleanup on test finish so Windows doesn't complain about used files
Idan Kamara <idankk86@gmail.com>
parents:
58
diff
changeset
|
39 os.chdir('..') |
58
3d413c54e048
tests: be prepared for basetest.setUp not being called
Idan Kamara <idankk86@gmail.com>
parents:
7
diff
changeset
|
40 try: |
3d413c54e048
tests: be prepared for basetest.setUp not being called
Idan Kamara <idankk86@gmail.com>
parents:
7
diff
changeset
|
41 shutil.rmtree(self._testtmp) |
3d413c54e048
tests: be prepared for basetest.setUp not being called
Idan Kamara <idankk86@gmail.com>
parents:
7
diff
changeset
|
42 except AttributeError: |
3d413c54e048
tests: be prepared for basetest.setUp not being called
Idan Kamara <idankk86@gmail.com>
parents:
7
diff
changeset
|
43 pass # if our setUp was overriden |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
44 |
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
45 def append(self, path, *args): |
68
a0328b08e028
tests: open files in binary mode so new lines aren't converted
Idan Kamara <idankk86@gmail.com>
parents:
67
diff
changeset
|
46 f = open(path, 'ab') |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
47 for a in args: |
157
6564544576b9
tests: write out bytes instead of strings to test files (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
48 f.write(a.encode('latin-1')) |
7
eac8be119d81
tests: rearrange tests and use nosetests
Idan Kamara <idankk86@gmail.com>
parents:
diff
changeset
|
49 f.close() |