Mercurial > hg
view tests/test-extensions-wrapfunction.py @ 36523:e7411fb7ba7f
wireprotoserver: ability to run an SSH server until an event is set
It seems useful to be able to start an SSH protocol server that
won't run forever and won't call sys.exit() when it stops. This
could be used to facilitate intra-process testing of the SSH
protocol, for example.
We teach the server function to loop until a threading.Event is set
and invent a new API to run the server until an event is set. It also
won't sys.exit() afterwards.
There aren't many callers of serve_forever(). So we could refactor
them relatively easily. But I was lazy.
threading.Event might be a bit heavyweight. An alternative would be
a list whose only elements is changed. We can't use a simple scalar
value like a bool or int because those types are immutable. Events
are what you use in systems programming for this use case, so the
use of threading.Event seems justified.
Differential Revision: https://phab.mercurial-scm.org/D2461
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 24 Feb 2018 12:07:21 -0800 |
parents | 82bd4c5a81e5 |
children | ac865f020b99 |
line wrap: on
line source
from __future__ import absolute_import, print_function from mercurial import extensions def genwrapper(x): def f(orig, *args, **kwds): return [x] + orig(*args, **kwds) f.x = x return f def getid(wrapper): return getattr(wrapper, 'x', '-') wrappers = [genwrapper(i) for i in range(5)] class dummyclass(object): def getstack(self): return ['orig'] dummy = dummyclass() def batchwrap(wrappers): for w in wrappers: extensions.wrapfunction(dummy, 'getstack', w) print('wrap %d: %s' % (getid(w), dummy.getstack())) def batchunwrap(wrappers): for w in wrappers: result = None try: result = extensions.unwrapfunction(dummy, 'getstack', w) msg = str(dummy.getstack()) except (ValueError, IndexError) as e: msg = e.__class__.__name__ print('unwrap %s: %s: %s' % (getid(w), getid(result), msg)) batchwrap(wrappers + [wrappers[0]]) batchunwrap([(wrappers[i] if i >= 0 else None) for i in [3, None, 0, 4, 0, 2, 1, None]]) wrap0 = extensions.wrappedfunction(dummy, 'getstack', wrappers[0]) wrap1 = extensions.wrappedfunction(dummy, 'getstack', wrappers[1]) # Use them in a different order from how they were created to check that # the wrapping happens in __enter__, not in __init__ print('context manager', dummy.getstack()) with wrap1: print('context manager', dummy.getstack()) with wrap0: print('context manager', dummy.getstack()) # Bad programmer forgets to unwrap the function, but the context # managers still unwrap their wrappings. extensions.wrapfunction(dummy, 'getstack', wrappers[2]) print('context manager', dummy.getstack()) print('context manager', dummy.getstack()) print('context manager', dummy.getstack()) # Wrap callable object which has no __name__ class callableobj(object): def __call__(self): return ['orig'] dummy.cobj = callableobj() extensions.wrapfunction(dummy, 'cobj', wrappers[0]) print('wrap callable object', dummy.cobj())