mirror of https://github.com/vacp2p/nim-libp2p.git
read should await for data on an empty buff
This commit is contained in:
parent
91d2f52f4e
commit
ffc3b04222
|
@ -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 size = if n > 0: n else: s.readBuf.len()
|
||||||
var index = 0
|
var index = 0
|
||||||
|
|
||||||
|
if s.readBuf.len() == 0:
|
||||||
|
await s.requestReadBytes()
|
||||||
|
|
||||||
while index < size:
|
while index < size:
|
||||||
while s.readBuf.len() > 0 and index < size:
|
while s.readBuf.len() > 0 and index < size:
|
||||||
result.add(s.popFirst())
|
result.add(s.popFirst())
|
||||||
|
|
|
@ -84,6 +84,29 @@ suite "BufferStream":
|
||||||
check:
|
check:
|
||||||
waitFor(testRead()) == true
|
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":
|
test "readExactly":
|
||||||
proc testReadExactly(): Future[bool] {.async.} =
|
proc testReadExactly(): Future[bool] {.async.} =
|
||||||
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard
|
||||||
|
|
Loading…
Reference in New Issue