tunixsocket.nim 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import std/[assertions, net, os, osproc]
  2. # XXX: Make this test run on Windows too when we add support for Unix sockets on Windows
  3. when defined(posix) and not defined(nimNetLite):
  4. const nim = getCurrentCompilerExe()
  5. let
  6. dir = currentSourcePath().parentDir()
  7. serverPath = dir / "unixsockettest"
  8. let (_, err) = execCmdEx(nim & " c " & quoteShell(dir / "unixsockettest.nim"))
  9. doAssert err == 0
  10. let svproc = startProcess(serverPath, workingDir = dir)
  11. doAssert svproc.running()
  12. # Wait for the server to open the socket and listen from it
  13. sleep(400)
  14. block unixSocketSendRecv:
  15. let
  16. unixSocketPath = dir / "usox"
  17. socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
  18. socket.connectUnix(unixSocketPath)
  19. # for a blocking Unix socket this should never fail
  20. socket.send("data sent through the socket\c\l", maxRetries = 0)
  21. var resp: string
  22. socket.readLine(resp)
  23. doAssert resp == "Hello from server"
  24. socket.send("bye\c\l")
  25. socket.readLine(resp)
  26. doAssert resp == "bye"
  27. socket.close()
  28. svproc.close()