Implemented 'await connect' fully. Fixed a buffering and 'reqReg' bug.

* reqReq did not reset lastReq to reqNil, this caused problems in the
new tasyncitermacro test. (await accept stopped accepting requests after
some arbitrary amount have been accepted).
* Rewritten and moved tasyncitermacro to tests/run/ so that the tester
actually tests whether it works at runtime.
* tasynciterraw is now a copy of tasyncitermacro after macro expansion.
It will be used as a control test, useful if the macro starts generating
incorrect code.
This commit is contained in:
Dominik Picheta 2013-08-03 14:19:17 +01:00
commit b3469c43a6
5 changed files with 325 additions and 141 deletions

View file

@ -1,42 +0,0 @@
import sockets, asyncio, strutils
proc auth3(client: PAsyncSocket) {.async.} =
await send(client, "Auth\c\L")
proc auth2(client: PAsyncSocket) {.async.} =
await auth3(client)
proc auth(client: PAsyncSocket) {.async.} =
await auth2(client)
proc processRequest(client: PAsyncSocket, test: string, closeSock: bool = true) {.async.} =
assert test == "ahha"
assert client != nil
let line = await(readLine(client))
echo("Read: ", line)
for i in 0 .. 10:
await auth(client)
await send(client, "Goodbye.\c\L")
if closeSock:
client.close()
proc processServer() {.async.} =
var sock = AsyncSocket()
# blocks:
sock.bindAddr(TPort(6667))
sock.listen()
# Accept loop
while true:
let client: PAsyncSocket = await(accept(sock))
assert client != nil
reg processRequest(client, "ahha")
var disp = newDispatcher(false)
disp.register(processServer, nil)
while true:
discard disp.poll()

View file

@ -1,69 +0,0 @@
import asyncio, sockets
const qwerty = "qwertyuiopasdfghjklzxcvbnm\c\L"
iterator processRequest(x: PRequest): PRequest {.closure.} =
# Read first message
let client = x.socket
var lineReq = PRequest(socket: client, kind: reqReadLine, line: "")
yield lineReq
if lineReq.line == "HELLO":
echo "Got first message."
var longString = qwerty
for i in 0..109000:
longString.add($i & qwerty)
var writeReq = PRequest(socket: client, kind: reqWrite, toWrite: longString)
yield writeReq
# Read second message: No need to create another request object:
yield lineReq
if lineReq.line == "DOM":
echo("Got second message.")
client.close()
iterator processServer(x: PRequest): PRequest {.closure.} =
type
PTest = ref object of TObject
param: string
let passedInParams = cast[PTest](x.param)
assert passedInParams.param == "blah"
var sock = AsyncSocket()
# blocks:
sock.bindAddr(TPort(6667))
sock.listen()
# Accept loop
while true:
var acceptReq = PRequest(socket: sock, kind: reqAccept, client: nil)
echo("About to yield accept")
yield acceptReq
echo("after yield")
if acceptReq.hasException:
echo("Got exception[EAssertionFailed]: ", acceptReq.exc of EAssertionFailed)
echo(acceptReq.exc.msg)
continue
let client = acceptReq.client
assert client != nil
yield PRequest(socket: client, kind: reqReg, param: nil, worker: processRequest)
when isMainModule:
let param = "blah"
type
PTest = ref object of TObject
param: type(param)
var test = PTest(param: param)
var disp = newDispatcher(false)
disp.register(processServer, test)
while true:
discard disp.poll()