Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wait(self, timeout):
with gevent.Timeout(timeout, False):
for index in range(1000000):
try:
gevent_socket.gethostbyname('www.x%s.com' % index)
except socket.error:
pass
raise AssertionError('Timeout was not raised')
def test_connect_auto_discovery_success(self, mock_recv, mock_emit):
# setup
self.conn.connect.return_value = True
self.server_list.__len__.return_value = 0
def fake_servers(*args, **kwargs):
self.server_list.__len__.return_value = 10
return True
self.server_list.bootstrap_from_webapi.side_effect = fake_servers
# run
cm = CMClient()
with gevent.Timeout(3, False):
cm.connect(retry=1)
gevent.idle()
# verify
self.server_list.bootstrap_from_webapi.assert_called_once_with()
self.server_list.bootstrap_from_dns.assert_not_called()
self.conn.connect.assert_called_once_with((127001, 20000))
mock_emit.assert_called_once_with('connected')
mock_recv.assert_called_once_with()
def test_nested_timeout(self):
with Timeout(DELAY, False):
with Timeout(DELAY * 10, False):
sleep(DELAY * 3 * 20)
raise AssertionError('should not get there')
with Timeout(DELAY) as t1:
with Timeout(DELAY * 20) as t2:
try:
sleep(DELAY * 30)
except Timeout as ex:
assert ex is t1, (ex, t1)
assert not t1.pending, t1
assert t2.pending, t2
assert not t2.pending, t2
with Timeout(DELAY * 20) as t1:
with Timeout(DELAY) as t2:
try:
sleep(DELAY * 30)
except Timeout as ex:
assert ex is t2, (ex, t2)
assert t1.pending, t1
assert not t2.pending, t2
def test_direct_raise_instance(self):
timeout = gevent.Timeout()
try:
raise timeout
except gevent.Timeout as t:
assert timeout is t, (timeout, t)
assert not t.pending, repr(t)
def test_nonblocking_get(self):
ar = AsyncResult()
self.assertRaises(gevent.Timeout, ar.get, block=False)
self.assertRaises(gevent.Timeout, ar.get_nowait)
def restart_t1(self):
self.__timeout_t1.cancel()
self.__timeout_t1 = gevent.Timeout(self.T_1, gevent.Timeout)
self.__timeout_t1.start()
Args:
initial_alloc (int): amount of initial tokens.
name (str): human readable token name.
symbol (str): token shorthand symbol.
decimals (int): decimal places.
timeout (int): timeout in seconds for creation.
auto_register (boolean): if True(default), automatically register
the token with raiden.
Returns:
token_address_hex: the hex encoded address of the new token/token.
"""
contract_path = get_contract_path('HumanStandardToken.sol')
# Deploy a new ERC20 token
with gevent.Timeout(timeout):
token_proxy = self._chain.client.deploy_solidity_contract(
'HumanStandardToken',
compile_files_cwd([contract_path]),
dict(),
(initial_alloc, name, decimals, symbol),
contract_path=contract_path,
)
token_address_hex = hexlify(token_proxy.contract_address)
if auto_register:
self.register_token(registry_address, token_address_hex)
print("Successfully created {}the token '{}'.".format(
'and registered ' if auto_register else ' ',
name,
))
return token_address_hex
def Timeout(self, timeout):
return gevent.Timeout(timeout)
def run_api(client, request_id, api_name, *args):
service_slug = client.__name__
logger.info("[{}] {}.{}{}".format(request_id,
service_slug, api_name, args))
with gevent.Timeout(10):
if is_in_container():
call = getattr(client, api_name)
return call(*args)
else:
with client(timeout=10, fake=settings.FAKE_CLIENT) as c:
call = getattr(c, api_name)
return call(*args)
def _wait_write(self):
assert self.__writable.ready(), "Only one greenlet can be waiting on this event"
self.__writable = AsyncResult()
# timeout is because libzmq cannot be trusted to properly signal a new send event:
# this is effectively a maximum poll interval of 1s
tic = time.time()
dt = self._gevent_bug_timeout
if dt:
timeout = gevent.Timeout(seconds=dt)
else:
timeout = None
try:
if timeout:
timeout.start()
self.__writable.get(block=True)
except gevent.Timeout as t:
if t is not timeout:
raise
toc = time.time()
# gevent bug: get can raise timeout even on clean return
# don't display zmq bug warning for gevent bug (this is getting ridiculous)
if self._debug_gevent and timeout and toc-tic > dt and \
self.getsockopt(zmq.EVENTS) & zmq.POLLOUT:
print("BUG: gevent may have missed a libzmq send event on %i!" % self.FD, file=sys.stderr)
finally: