def receive(tmpfile, data, size, offset=0): with open(tmpfile, "wb") as f: f.write(b"x" * offset) src = io.BytesIO(data) op = directio.Receive(tmpfile, src, size, offset=offset) op.run() with open(tmpfile, "rb") as f: f.seek(offset) return f.read(size)
def test_receive_no_size(tmpfile, data, offset): with open(tmpfile, "wb") as f: f.write(b"x" * offset) src = io.BytesIO(data) op = directio.Receive(tmpfile, src, offset=offset) op.run() with io.open(tmpfile, "rb") as f: f.seek(offset) assert f.read(len(data)) == data
def test_receive_padd_to_block_size(tmpfile): with open(tmpfile, "wb") as f: f.write(b"x" * 400) size = 200 offset = 300 padding = BLOCKSIZE - size - offset src = io.BytesIO(b"y" * size) op = directio.Receive(tmpfile, src, size, offset=offset) op.run() with open(tmpfile, "rb") as f: # Data before offset is not modified. assert f.read(300) == b"x" * offset # Data after offset is modifed, flie extended. assert f.read(200) == b"y" * size # File padded to block size with zeroes. assert f.read() == b"\0" * padding
def test_receive_flush(tmpfile, monkeypatch, extra, calls): # This would be much cleaner when we add backend object implementing flush. fsync = os.fsync fsync_calls = [0] def counted_fsync(fd): fsync_calls[0] += 1 fsync(fd) monkeypatch.setattr("os.fsync", counted_fsync) data = b"x" * 1024**2 * 2 with open(tmpfile, "wb") as f: f.write(data) size = len(data) src = io.BytesIO(b"X" * size) op = directio.Receive(tmpfile, src, size, **extra) op.run() with io.open(tmpfile, "rb") as f: assert f.read() == src.getvalue() assert fsync_calls[0] == calls
def download_disk(adapter, estimated_size, size, dest, bufsize): op = directio.Receive(dest, adapter, size=size, buffersize=bufsize) with progress(op, estimated_size): op.run() adapter.finish()
def receive_unbuffered(tmpfile, chunks, size, bufsize): src = util.UnbufferedStream(chunks) op = directio.Receive(tmpfile, src, size, buffersize=bufsize) op.run() with open(tmpfile, "rb") as f: return f.read()
def test_recv_repr(tmpfile): op = directio.Receive(tmpfile, None, 100, offset=42) rep = repr(op) assert "Receive" in rep assert "size=100 offset=42 done=0" in rep