tasyncRecvLine.nim 1.2 KB

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