proof that refcounting can handle Nim's async (#12533)
This commit is contained in:
parent
160c07be00
commit
c52a2c3ab0
3 changed files with 99 additions and 19 deletions
73
tests/async/tasyncawait_cyclebreaker.nim
Normal file
73
tests/async/tasyncawait_cyclebreaker.nim
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
discard """
|
||||
output: "50000"
|
||||
cmd: "nim c -d:nimTypeNames -d:nimCycleBreaker $file"
|
||||
"""
|
||||
import asyncdispatch, asyncnet, nativesockets, net, strutils, os
|
||||
|
||||
var msgCount = 0
|
||||
|
||||
const
|
||||
swarmSize = 500
|
||||
messagesToSend = 100
|
||||
|
||||
var clientCount = 0
|
||||
|
||||
proc sendMessages(client: AsyncFD) {.async.} =
|
||||
for i in 0 ..< messagesToSend:
|
||||
await send(client, "Message " & $i & "\c\L")
|
||||
|
||||
proc launchSwarm(port: Port) {.async.} =
|
||||
for i in 0 ..< swarmSize:
|
||||
var sock = createAsyncNativeSocket()
|
||||
|
||||
await connect(sock, "localhost", port)
|
||||
await sendMessages(sock)
|
||||
closeSocket(sock)
|
||||
|
||||
proc readMessages(client: AsyncFD) {.async.} =
|
||||
# wrapping the AsyncFd into a AsyncSocket object
|
||||
var sockObj = newAsyncSocket(client)
|
||||
var (ipaddr, port) = sockObj.getPeerAddr()
|
||||
doAssert ipaddr == "127.0.0.1"
|
||||
(ipaddr, port) = sockObj.getLocalAddr()
|
||||
doAssert ipaddr == "127.0.0.1"
|
||||
while true:
|
||||
var line = await recvLine(sockObj)
|
||||
if line == "":
|
||||
closeSocket(client)
|
||||
clientCount.inc
|
||||
break
|
||||
else:
|
||||
if line.startswith("Message "):
|
||||
msgCount.inc
|
||||
else:
|
||||
doAssert false
|
||||
|
||||
proc createServer(port: Port) {.async.} =
|
||||
var server = createAsyncNativeSocket()
|
||||
block:
|
||||
var name: Sockaddr_in
|
||||
name.sin_family = typeof(name.sin_family)(toInt(AF_INET))
|
||||
name.sin_port = htons(uint16(port))
|
||||
name.sin_addr.s_addr = htonl(INADDR_ANY)
|
||||
if bindAddr(server.SocketHandle, cast[ptr SockAddr](addr(name)),
|
||||
sizeof(name).Socklen) < 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
discard server.SocketHandle.listen()
|
||||
while true:
|
||||
asyncCheck readMessages(await accept(server))
|
||||
|
||||
asyncCheck createServer(Port(10335))
|
||||
asyncCheck launchSwarm(Port(10335))
|
||||
while true:
|
||||
poll()
|
||||
GC_collectZct()
|
||||
|
||||
let (allocs, deallocs) = getMemCounters()
|
||||
doAssert allocs < deallocs + 1000
|
||||
|
||||
if clientCount == swarmSize: break
|
||||
|
||||
assert msgCount == swarmSize * messagesToSend
|
||||
echo msgCount
|
||||
Loading…
Add table
Add a link
Reference in a new issue