diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-07-22 23:32:49 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-07-22 23:32:49 +0100 |
commit | 5310a3044fa4187274e2bfe59de68f394a81c89d (patch) | |
tree | eab220ba823fb2f706ff6abab7b52cdb982821e3 /tests/run | |
parent | b839e42e92edf6acfca73768cbdd9c7595ca8797 (diff) | |
download | Nim-5310a3044fa4187274e2bfe59de68f394a81c89d.tar.gz |
Many fixes for asynchronous sockets. Asyncio should now work well with buffered and unbuffered plain and ssl sockets. Added asyncio
test to the test suite.
Diffstat (limited to 'tests/run')
-rw-r--r-- | tests/run/tasynciossl.nim | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/run/tasynciossl.nim b/tests/run/tasynciossl.nim new file mode 100644 index 000000000..a0308aebc --- /dev/null +++ b/tests/run/tasynciossl.nim @@ -0,0 +1,89 @@ +discard """ + file: "tasynciossl.nim" + cmd: "nimrod cc --hints:on --define:ssl $# $#" + output: "20000" +""" +import sockets, asyncio, strutils, times + +var disp = newDispatcher() +var msgCount = 0 + +when defined(ssl): + var ctx = newContext(verifyMode = CVerifyNone, + certFile = "mycert.pem", keyFile = "mycert.pem") + + var ctx1 = newContext(verifyMode = CVerifyNone) + +const + swarmSize = 50 + messagesToSend = 100 + +proc swarmConnect(s: PAsyncSocket, arg: PObject) {.nimcall.} = + #echo("Connected") + for i in 1..messagesToSend: + s.send("Message " & $i & "\c\L") + s.close() + +proc serverRead(s: PAsyncSocket, arg: PObject) {.nimcall.} = + var line = "" + assert s.recvLine(line) + if line != "": + #echo(line) + if line.startsWith("Message "): + msgCount.inc() + else: + assert(false) + else: + s.close() + +proc serverAccept(s: PAsyncSocket, arg: Pobject) {.nimcall.} = + var client: PAsyncSocket + new(client) + s.accept(client) + client.handleRead = serverRead + disp.register(client) + +proc launchSwarm(disp: var PDispatcher, port: TPort, count: int, + buffered = true, useSSL = false) = + for i in 1..count: + var client = AsyncSocket() + when defined(ssl): + if useSSL: + ctx1.wrapSocket(client) + client.handleConnect = swarmConnect + disp.register(client) + client.connect("localhost", port) + +proc createSwarm(port: TPort, buffered = true, useSSL = false) = + var server = AsyncSocket() + when defined(ssl): + if useSSL: + ctx.wrapSocket(server) + server.handleAccept = serverAccept + disp.register(server) + server.bindAddr(port) + server.listen() + disp.launchSwarm(port, swarmSize, buffered, useSSL) + +when defined(ssl): + const serverCount = 4 +else: + const serverCount = 2 + +createSwarm(TPort(10235)) +createSwarm(TPort(10236), false) + +when defined(ssl): + createSwarm(TPort(10237), true, true) + createSwarm(TPort(10238), false, true) + +var startTime = epochTime() +while true: + if epochTime() - startTime >= 300.0: + break + if not disp.poll(): break + if disp.len == serverCount: + break + +assert msgCount == (swarmSize * messagesToSend) * serverCount +echo(msgCount) \ No newline at end of file |