read should await for data on an empty buff

This commit is contained in:
Dmitriy Ryajov 2020-02-29 17:20:18 +01:00
parent 91d2f52f4e
commit ffc3b04222
2 changed files with 27 additions and 0 deletions

View File

@ -132,6 +132,10 @@ method read*(s: BufferStream, n = -1): Future[seq[byte]] {.async.} =
##
var size = if n > 0: n else: s.readBuf.len()
var index = 0
if s.readBuf.len() == 0:
await s.requestReadBytes()
while index < size:
while s.readBuf.len() > 0 and index < size:
result.add(s.popFirst())

View File

@ -84,6 +84,29 @@ suite "BufferStream":
check:
waitFor(testRead()) == true
test "read all from small buffer":
proc testRead(): Future[bool] {.async.} =
proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
discard
let buff = newBufferStream(writeHandler, 4)
check buff.len == 0
proc reader() {.async.} =
var size = 0
while size != 5:
var msg = await buff.read()
size += msg.len
check size == 5
var fut = reader()
await buff.pushTo(cast[seq[byte]](@"12345"))
await fut
result = true
check:
waitFor(testRead()) == true
test "readExactly":
proc testReadExactly(): Future[bool] {.async.} =
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard