diff options
author | Euan Torano <euantorano@gmail.com> | 2017-07-09 20:50:47 +0100 |
---|---|---|
committer | Euan Torano <euantorano@gmail.com> | 2017-07-09 20:50:47 +0100 |
commit | 3f3853945d161a2ada421530bbcd43c7ab74a2eb (patch) | |
tree | 5393142ed675766507af72cf9554ffaebd84d0dc | |
parent | a5681d03e38006655b2b6383253c701a8d5409f4 (diff) | |
download | Nim-3f3853945d161a2ada421530bbcd43c7ab74a2eb.tar.gz |
Adding test for recvline
Signed-off-by: Euan Torano <euantorano@gmail.com>
-rw-r--r-- | lib/pure/asyncnet.nim | 6 | ||||
-rw-r--r-- | tests/async/tasyncRecvLine.nim | 54 |
2 files changed, 56 insertions, 4 deletions
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim index f5c7b3e05..5de65efe0 100644 --- a/lib/pure/asyncnet.nim +++ b/lib/pure/asyncnet.nim @@ -533,15 +533,13 @@ proc recvLineInto*(socket: AsyncSocket, resString: FutureVar[string], else: var c = "" while true: - let recvFut = recv(socket, 1, flags) - c = await recvFut + c = await recv(socket, 1, flags) if c.len == 0: resString.mget.setLen(0) resString.complete() return if c == "\r": - let recvFut = recv(socket, 1, flags) # Skip \L - c = await recvFut + c = await recv(socket, 1, flags) # Skip \L assert c == "\L" addNLIfEmpty() resString.complete() diff --git a/tests/async/tasyncRecvLine.nim b/tests/async/tasyncRecvLine.nim new file mode 100644 index 000000000..5db51671e --- /dev/null +++ b/tests/async/tasyncRecvLine.nim @@ -0,0 +1,54 @@ +discard """ + file: "tasyncsend4754.nim" + output: ''' +Hello World +Hello World +''' +""" + +import asyncdispatch, asyncnet + +const recvLinePort = Port(6047) + +proc setupTestServer(): AsyncSocket = + result = newAsyncSocket() + result.setSockOpt(OptReuseAddr, true) + result.bindAddr(recvLinePort) + result.listen() + +proc testUnbuffered(): Future[void] {.async.} = + let serverSock = setupTestServer() + let serverAcceptClientFut = serverSock.accept() + + let clientSock = newAsyncSocket(buffered = false) + let clientConnectFut = clientSock.connect("localhost", recvLinePort) + + let serverAcceptedClient = await serverAcceptClientFut + await clientConnectFut + + await serverAcceptedClient.send("Hello World\c\L") + + echo await clientSock.recvLine() + + clientSock.close() + serverSock.close() + +proc testBuffered(): Future[void] {.async.} = + let serverSock = setupTestServer() + let serverAcceptClientFut = serverSock.accept() + + let clientSock = newAsyncSocket(buffered = true) + let clientConnectFut = clientSock.connect("localhost", recvLinePort) + + let serverAcceptedClient = await serverAcceptClientFut + await clientConnectFut + + await serverAcceptedClient.send("Hello World\c\L") + + echo await clientSock.recvLine() + + clientSock.close() + serverSock.close() + +waitFor testUnbuffered() +waitFor testBuffered() |