def send(): b = BatchedSend(interval=3) b.start(stream) for i in range(0, 10000, 2): b.send(i) b.send(i + 1) yield gen.sleep(0.00001 * random.randint(1, 10))
async def test_serializers(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval="10ms", serializers=["msgpack"]) b.start(comm) b.send({"x": to_serialize(123)}) b.send({"x": to_serialize("hello")}) await asyncio.sleep(0.100) b.send({"x": to_serialize(lambda x: x + 1)}) with captured_logger("distributed.protocol") as sio: await asyncio.sleep(0.100) value = sio.getvalue() assert "serialize" in value assert "type" in value assert "function" in value msg = await comm.read() assert list(msg) == [{"x": 123}, {"x": "hello"}] with pytest.raises(TimeoutError): msg = await asyncio.wait_for(comm.read(), 0.1)
def test_BatchedSend(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) assert str(len(b.buffer)) in str(b) assert str(len(b.buffer)) in repr(b) b.start(stream) yield gen.sleep(0.020) b.send('hello') b.send('hello') b.send('world') yield gen.sleep(0.020) b.send('HELLO') b.send('HELLO') result = yield read(stream) assert result == ['hello', 'hello', 'world'] result = yield read(stream) assert result == ['HELLO', 'HELLO'] assert b.byte_count > 1
def send(): b = BatchedSend(interval=3) b.start(comm) for i in range(0, 10000, 2): b.send(i) b.send(i + 1) yield gen.sleep(0.00001 * random.randint(1, 10))
def test_serializers(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval="10ms", serializers=["msgpack"]) b.start(comm) b.send({"x": to_serialize(123)}) b.send({"x": to_serialize("hello")}) yield gen.sleep(0.100) b.send({"x": to_serialize(lambda x: x + 1)}) with captured_logger("distributed.protocol") as sio: yield gen.sleep(0.100) value = sio.getvalue() assert "serialize" in value assert "type" in value assert "function" in value msg = yield comm.read() assert list(msg) == [{"x": 123}, {"x": "hello"}] with pytest.raises(gen.TimeoutError): msg = yield gen.with_timeout(timedelta(milliseconds=100), comm.read())
def run_traffic_jam(nsends, nbytes): # This test eats `nsends * nbytes` bytes in RAM np = pytest.importorskip('numpy') from distributed.protocol import to_serialize data = bytes(np.random.randint(0, 255, size=(nbytes,)).astype('u1').data) with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=0.01) b.start(comm) msg = {'x': to_serialize(data)} for i in range(nsends): b.send(assoc(msg, 'i', i)) if np.random.random() > 0.5: yield gen.sleep(0.001) results = [] count = 0 while len(results) < nsends: # If this times out then I think it's a backpressure issue # Somehow we're able to flood the socket so that the receiving end # loses some of our messages L = yield gen.with_timeout(timedelta(seconds=5), comm.read()) count += 1 results.extend(r['i'] for r in L) assert count == b.batch_count == e.count assert b.message_count == nsends assert results == list(range(nsends)) comm.close() # external closing yield b.close()
def test_serializers(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval='10ms', serializers=['msgpack']) b.start(comm) b.send({'x': to_serialize(123)}) b.send({'x': to_serialize('hello')}) yield gen.sleep(0.100) b.send({'x': to_serialize(lambda x: x + 1)}) with captured_logger('distributed.protocol') as sio: yield gen.sleep(0.100) value = sio.getvalue() assert 'serialize' in value assert 'type' in value assert 'function' in value msg = yield comm.read() assert list(msg) == [{'x': 123}, {'x': 'hello'}] with pytest.raises(gen.TimeoutError): msg = yield gen.with_timeout(timedelta(milliseconds=100), comm.read())
def run_traffic_jam(nsends, nbytes): # This test eats `nsends * nbytes` bytes in RAM np = pytest.importorskip('numpy') from distributed.protocol import to_serialize data = bytes(np.random.randint(0, 255, size=(nbytes, )).astype('u1').data) with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=0.01) b.start(comm) msg = {'x': to_serialize(data)} for i in range(nsends): b.send(assoc(msg, 'i', i)) if np.random.random() > 0.5: yield gen.sleep(0.001) results = [] count = 0 while len(results) < nsends: # If this times out then I think it's a backpressure issue # Somehow we're able to flood the socket so that the receiving end # loses some of our messages L = yield gen.with_timeout(timedelta(seconds=5), comm.read()) count += 1 results.extend(r['i'] for r in L) assert count == b.batch_count == e.count assert b.message_count == nsends assert results == list(range(nsends)) comm.close() # external closing yield b.close()
def test_serializers(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval='10ms', serializers=['msgpack']) b.start(comm) b.send({'x': to_serialize(123)}) b.send({'x': to_serialize('hello')}) yield gen.sleep(0.100) b.send({'x': to_serialize(lambda x: x + 1)}) with captured_logger('distributed.protocol') as sio: yield gen.sleep(0.100) value = sio.getvalue() assert 'serialize' in value assert 'type' in value assert 'function' in value msg = yield comm.read() assert msg == [{'x': 123}, {'x': 'hello'}] with pytest.raises(gen.TimeoutError): msg = yield gen.with_timeout(timedelta(milliseconds=100), comm.read())
async def run_traffic_jam(nsends, nbytes): # This test eats `nsends * nbytes` bytes in RAM np = pytest.importorskip("numpy") from distributed.protocol import to_serialize data = bytes(np.random.randint(0, 255, size=(nbytes, )).astype("u1").data) async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=0.01) b.start(comm) msg = {"x": to_serialize(data)} for i in range(nsends): b.send(assoc(msg, "i", i)) if np.random.random() > 0.5: await asyncio.sleep(0.001) results = [] count = 0 while len(results) < nsends: # If this times out then I think it's a backpressure issue # Somehow we're able to flood the socket so that the receiving end # loses some of our messages L = await asyncio.wait_for(comm.read(), 5) count += 1 results.extend(r["i"] for r in L) assert count == b.batch_count == e.count assert b.message_count == nsends assert results == list(range(nsends)) await comm.close() # external closing await b.close()
async def send(): b = BatchedSend(interval=3) b.start(comm) for i in range(0, 10000, 2): b.send(i) b.send(i + 1) await asyncio.sleep(0.00001 * random.randint(1, 10))
async def test_close_twice(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.start(comm) await b.close() await b.close()
def test_close_twice(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) yield b.close() yield b.close()
def test_close_twice(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) yield b.close() yield b.close()
def test_close_closed(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send(123) comm.close() # external closing yield b.close()
def test_send_after_stream_finish(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) yield b.last_send b.send('hello') result = yield read(stream); assert result == ['hello']
def test_send_before_start(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.send('hello') b.send('world') b.start(comm) result = yield comm.read() assert result == ('hello', 'world')
def test_send_before_start(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.send('hello') b.send('world') b.start(comm) result = yield comm.read() assert result == ['hello', 'world']
def test_send_after_stream_finish(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) yield b.last_send b.send('hello') result = yield read(stream) assert result == ['hello']
def test_close_closed(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) b.send(123) stream.close() # external closing yield b.close(ignore_closed=True)
async def test_send_before_start(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.send("hello") b.send("world") b.start(comm) result = await comm.read() assert result == ("hello", "world")
def test_send_before_start(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.send('hello') b.send('world') b.start(stream) result = yield read(stream); assert result == ['hello', 'world']
def test_send_after_stream_start(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send('hello') b.send('world') result = yield comm.read() if len(result) < 2: result += yield comm.read() assert result == ['hello', 'world']
async def test_close_closed(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send(123) await comm.close() # external closing await b.close() assert "closed" in repr(b) assert "closed" in str(b)
def test_close_closed(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send(123) comm.close() # external closing yield b.close() assert 'closed' in repr(b) assert 'closed' in str(b)
async def test_send_after_stream_start(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send("hello") b.send("world") result = await comm.read() if len(result) < 2: result += await comm.read() assert result == ("hello", "world")
def test_send_after_stream_start(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) b.send('hello') b.send('world') result = yield comm.read() if len(result) < 2: result += yield comm.read() assert result == ('hello', 'world')
async def test_handles_exceptions(): # Ensure that we properly handle exceptions in BatchedSend. # https://github.com/pangeo-data/pangeo/issues/788 # mentioned in https://github.com/dask/distributed/issues/4080, but # possibly distinct. # # The reported issues (https://github.com/tornadoweb/tornado/pull/2008) # claim that the BufferError *should* only happen when the application # is incorrectly using threads. I haven't been able to construct an # actual example, so we mock IOStream.write to raise and ensure that # BufferedSend handles things correctly. We don't (yet) test that # any *users* of BatchedSend correctly handle BatchedSend dropping # messages. async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.start(comm) await asyncio.sleep(0.020) orig = comm.stream.write n = 0 def raise_buffererror(*args, **kwargs): nonlocal n n += 1 if n == 1: raise BufferError("bad!") elif n == 2: orig(*args, **kwargs) else: raise CommClosedError with mock.patch.object(comm.stream, "write", wraps=raise_buffererror): b.send("hello") b.send("hello") b.send("world") await asyncio.sleep(0.020) result = await comm.read() assert result == ("hello", "hello", "world") b.send("raises when flushed") await asyncio.sleep(0.020) # CommClosedError hit in callback with pytest.raises(CommClosedError): b.send("raises when sent")
def test_send_before_close(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) b.start(comm) cnt = int(e.count) b.send('hello') yield b.close() # close immediately after sending assert not b.buffer start = time() while e.count != cnt + 1: yield gen.sleep(0.01) assert time() < start + 5 with pytest.raises(CommClosedError): b.send('123')
async def test_send_before_close(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) b.start(comm) cnt = int(e.count) b.send("hello") await b.close() # close immediately after sending assert not b.buffer start = time() while e.count != cnt + 1: await asyncio.sleep(0.01) assert time() < start + 5 with pytest.raises(CommClosedError): b.send("123")
def test_BatchedSend(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) yield b.last_send yield gen.sleep(0.020) b.send('hello') b.send('hello') b.send('world') yield gen.sleep(0.020) b.send('HELLO') b.send('HELLO') result = yield read(stream); assert result == ['hello', 'hello', 'world'] result = yield read(stream); assert result == ['HELLO', 'HELLO']
def test_send_before_close(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) cnt = int(e.count) b.send('hello') yield b.close() # close immediately after sending assert not b.buffer start = time() while e.count != cnt + 1: yield gen.sleep(0.01) assert time() < start + 5 with pytest.raises(StreamClosedError): b.send('123')
async def test_serializers(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval="10ms", serializers=["msgpack"]) b.start(comm) b.send({"x": to_serialize(123)}) b.send({"x": to_serialize("hello")}) await asyncio.sleep(0.100) b.send({"x": to_serialize(lambda x: x + 1)}) with captured_logger("distributed.protocol") as sio: await asyncio.sleep(0.100) value = sio.getvalue() assert "serialize" in value assert "type" in value assert "function" in value assert comm.closed()
def test_BatchedSend(): with echo_server() as e: client = TCPClient() stream = yield client.connect('127.0.0.1', e.port) b = BatchedSend(interval=10) b.start(stream) yield b.last_send yield gen.sleep(0.020) b.send('hello') b.send('hello') b.send('world') yield gen.sleep(0.020) b.send('HELLO') b.send('HELLO') result = yield read(stream) assert result == ['hello', 'hello', 'world'] result = yield read(stream) assert result == ['HELLO', 'HELLO']
def test_BatchedSend(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) assert str(len(b.buffer)) in str(b) assert str(len(b.buffer)) in repr(b) b.start(comm) yield gen.sleep(0.020) b.send('hello') b.send('hello') b.send('world') yield gen.sleep(0.020) b.send('HELLO') b.send('HELLO') result = yield comm.read() assert result == ('hello', 'hello', 'world') result = yield comm.read() assert result == ('HELLO', 'HELLO') assert b.byte_count > 1
def test_BatchedSend(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) assert str(len(b.buffer)) in str(b) assert str(len(b.buffer)) in repr(b) b.start(comm) yield gen.sleep(0.020) b.send("hello") b.send("hello") b.send("world") yield gen.sleep(0.020) b.send("HELLO") b.send("HELLO") result = yield comm.read() assert result == ("hello", "hello", "world") result = yield comm.read() assert result == ("HELLO", "HELLO") assert b.byte_count > 1
async def test_BatchedSend(): async with EchoServer() as e: comm = await connect(e.address) b = BatchedSend(interval=10) assert str(len(b.buffer)) in str(b) assert str(len(b.buffer)) in repr(b) b.start(comm) await asyncio.sleep(0.020) b.send("hello") b.send("hello") b.send("world") await asyncio.sleep(0.020) b.send("HELLO") b.send("HELLO") result = await comm.read() assert result == ("hello", "hello", "world") result = await comm.read() assert result == ("HELLO", "HELLO") assert b.byte_count > 1
def test_BatchedSend(): with echo_server() as e: comm = yield connect(e.address) b = BatchedSend(interval=10) assert str(len(b.buffer)) in str(b) assert str(len(b.buffer)) in repr(b) b.start(comm) yield gen.sleep(0.020) b.send('hello') b.send('hello') b.send('world') yield gen.sleep(0.020) b.send('HELLO') b.send('HELLO') result = yield comm.read() assert result == ['hello', 'hello', 'world'] result = yield comm.read() assert result == ['HELLO', 'HELLO'] assert b.byte_count > 1