tasyncRecvLine.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. discard """
  2. file: "tasyncRecvLine.nim"
  3. output: '''
  4. Hello World
  5. Hello World
  6. '''
  7. """
  8. import asyncdispatch, asyncnet
  9. const recvLinePort = Port(6047)
  10. proc setupTestServer(): AsyncSocket =
  11. result = newAsyncSocket()
  12. result.setSockOpt(OptReuseAddr, true)
  13. result.bindAddr(recvLinePort)
  14. result.listen()
  15. proc testUnbuffered(): Future[void] {.async.} =
  16. let serverSock = setupTestServer()
  17. let serverAcceptClientFut = serverSock.accept()
  18. let clientSock = newAsyncSocket(buffered = false)
  19. let clientConnectFut = clientSock.connect("localhost", recvLinePort)
  20. let serverAcceptedClient = await serverAcceptClientFut
  21. await clientConnectFut
  22. await serverAcceptedClient.send("Hello World\c\L")
  23. echo await clientSock.recvLine()
  24. clientSock.close()
  25. serverSock.close()
  26. proc testBuffered(): Future[void] {.async.} =
  27. let serverSock = setupTestServer()
  28. let serverAcceptClientFut = serverSock.accept()
  29. let clientSock = newAsyncSocket(buffered = true)
  30. let clientConnectFut = clientSock.connect("localhost", recvLinePort)
  31. let serverAcceptedClient = await serverAcceptClientFut
  32. await clientConnectFut
  33. await serverAcceptedClient.send("Hello World\c\L")
  34. echo await clientSock.recvLine()
  35. clientSock.close()
  36. serverSock.close()
  37. waitFor testUnbuffered()
  38. waitFor testBuffered()