This commit is contained in:
Dmitriy Ryajov 2020-02-21 18:09:33 -06:00
parent 6a7f9f058c
commit 9efc08cb2f
2 changed files with 21 additions and 1 deletions

View File

@ -106,7 +106,7 @@ proc pushTo*(s: BufferStream, data: seq[byte]) {.async.} =
try: try:
await s.lock.acquire() await s.lock.acquire()
var index = 0 var index = 0
while true: while not s.closed():
while index < data.len and s.readBuf.len < s.maxSize: while index < data.len and s.readBuf.len < s.maxSize:
s.readBuf.addLast(data[index]) s.readBuf.addLast(data[index])
inc(index) inc(index)

View File

@ -440,3 +440,23 @@ suite "BufferStream":
check: check:
waitFor(pipeTest()) == true waitFor(pipeTest()) == true
test "shouldn't get stuck on close":
proc test(): Future[bool] {.async.} =
proc createMessage(tmplate: string, size: int): seq[byte] =
result = newSeq[byte](size)
for i in 0 ..< len(result):
result[i] = byte(tmplate[i mod len(tmplate)])
var stream = newBufferStream()
var message = createMessage("MESSAGE", DefaultBufferSize * 2 + 1)
var fut = stream.pushTo(message)
await stream.close()
try:
await wait(fut, 100.milliseconds)
result = true
except AsyncTimeoutError:
result = false
check:
waitFor(test()) == true