Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_waiters(self):
c = coros.Channel()
w1 = eventlet.spawn(c.wait)
w2 = eventlet.spawn(c.wait)
w3 = eventlet.spawn(c.wait)
sleep(0)
self.assertEqual(c.waiting(), 3)
s1 = eventlet.spawn(c.send, 1)
s2 = eventlet.spawn(c.send, 2)
s3 = eventlet.spawn(c.send, 3)
sleep(0) # this gets all the sends into a waiting state
self.assertEqual(c.waiting(), 0)
s1.wait()
s2.wait()
s3.wait()
# NOTE: we don't guarantee that waiters are served in order
results = sorted([w1.wait(), w2.wait(), w3.wait()])
self.assertEqual(results, [1, 2, 3])
of the rpc publisher
"""
def faux_cast_go(context, topic, msg):
self.published.append((topic, msg))
def faux_cast_wait(context, topic, msg):
self.stubs.Set(oslo_rpc, 'cast', faux_cast_go)
# Sleep to simulate concurrency and allow other threads to work
eventlet.sleep(0)
self.published.append((topic, msg))
self.stubs.Set(oslo_rpc, 'cast', faux_cast_wait)
publisher = rpc.RPCPublisher(network_utils.urlsplit('rpc://'))
job1 = eventlet.spawn(publisher.publish_samples, None, self.test_data)
job2 = eventlet.spawn(publisher.publish_samples, None, self.test_data)
job1.wait()
job2.wait()
self.assertEqual(publisher.policy, 'default')
self.assertEqual(len(self.published), 2)
self.assertEqual(len(publisher.local_queue), 0)
def main():
app = URLMap()
app['/echo'] = handle
static_path = os.path.join(os.path.split(os.path.abspath(__file__))[0], 'static')
app['/'] = static.Cling(static_path)
# run an example app from the command line
listener = eventlet.listen(('localhost', 8013))
print "\nListening http://localhost:8013/ in your websocket-capable browser.\n"
eventlet.spawn(listen_tcp)
wsgi.server(listener, app)
def test_waiters_that_cancel(self):
q = coros.queue()
def do_receive(q, evt):
eventlet.Timeout(0, RuntimeError())
try:
result = q.wait()
evt.send(result)
except RuntimeError:
evt.send('timed out')
evt = Event()
spawn(do_receive, q, evt)
self.assertEqual(evt.wait(), 'timed out')
q.send('hi')
self.assertEqual(q.wait(), 'hi')
def accept(return_code, expected_timestamp):
try:
with Timeout(3):
sock, addr = bindsock.accept()
inc = sock.makefile('rb')
out = sock.makefile('wb')
out.write('HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n' %
return_code)
out.flush()
self.assertEqual(inc.readline(),
'PUT /sda1/123/a/c HTTP/1.1\r\n')
headers = {}
line = inc.readline()
while line and line != '\r\n':
headers[line.split(':')[0].lower()] = \
line.split(':')[1].strip()
line = inc.readline()
self.assertEqual(headers['x-delete-timestamp'],
expected_timestamp)
self.sent = 0
self.received = 0
self.etag = etag
self.body = body
self.headers = headers or {}
self.expect_headers = expect_headers or {}
self.timestamp = timestamp
self.connection_id = connection_id
self.give_send = give_send
if 'slow' in kwargs and isinstance(kwargs['slow'], list):
try:
self._next_sleep = kwargs['slow'].pop(0)
except IndexError:
self._next_sleep = None
# be nice to trixy bits with node_iter's
eventlet.sleep()
resp = req.get_response(self.object_controller)
self.assertEquals(resp.status_int, 204)
objfile = os.path.join(
self.testdir, 'sda1',
storage_directory(diskfile.DATADIR, 'p',
hash_path('a', 'c', 'o')),
timestamp + '.ts')
self.assert_(os.path.isfile(objfile))
self.assertEquals(1, calls_made[0])
self.assertEquals(len(os.listdir(os.path.dirname(objfile))), 1)
# The following request should return a 404, as the object should
# already have been deleted, but it should have also performed a
# container update because the timestamp is newer, and a tombstone
# file should also exist with this timestamp.
sleep(.00001)
timestamp = normalize_timestamp(time())
req = Request.blank('/sda1/p/a/c/o',
environ={'REQUEST_METHOD': 'DELETE'},
headers={'X-Timestamp': timestamp})
resp = req.get_response(self.object_controller)
self.assertEquals(resp.status_int, 404)
objfile = os.path.join(
self.testdir, 'sda1',
storage_directory(diskfile.DATADIR, 'p',
hash_path('a', 'c', 'o')),
timestamp + '.ts')
self.assert_(os.path.isfile(objfile))
self.assertEquals(2, calls_made[0])
self.assertEquals(len(os.listdir(os.path.dirname(objfile))), 1)
# The following request should return a 404, as the object should
def test_wait_for_call():
mock = Mock()
def call_after(seconds):
eventlet.sleep(seconds)
mock.method()
# should not raise
eventlet.spawn(call_after, 0)
with wait_for_call(1, mock.method):
pass
mock.reset_mock()
with pytest.raises(eventlet.Timeout):
eventlet.spawn(call_after, 1)
with wait_for_call(0, mock.method):
pass # pragma: no cover
running it.
Kills any previously-running server.
"""
if self.killer:
greenthread.kill(self.killer)
eventlet.sleep(0)
app = self.config.make_wsgi_app()
new_kwargs = dict(max_size=128,
log=self.logfile)
new_kwargs.update(kwargs)
sock = eventlet.listen(('localhost', 0))
self.port = sock.getsockname()[1]
self.killer = eventlet.spawn_n(wsgi.server, sock, app, **new_kwargs)
def test_chunked_content_length_mismatch_zero(self):
listener = listen(('localhost', 0))
port = listener.getsockname()[1]
killer = spawn(wsgi.server, listener, self.object_controller,
NullLogger())
sock = connect_tcp(('localhost', port))
fd = sock.makefile()
fd.write('PUT /sda1/p/a/c/o HTTP/1.1\r\nHost: localhost\r\n'
'Content-Type: text/plain\r\n'
'Connection: close\r\nX-Timestamp: %s\r\n'
'Content-Length: 0\r\n'
'Transfer-Encoding: chunked\r\n\r\n'
'2\r\noh\r\n4\r\n hai\r\n0\r\n\r\n' % normalize_timestamp(
1.0))
fd.flush()
headers = readuntil2crlfs(fd)
exp = 'HTTP/1.1 201'
self.assertEquals(headers[:len(exp)], exp)
sock = connect_tcp(('localhost', port))
fd = sock.makefile()