Added timers test.
This commit is contained in:
parent
874fe9b5fd
commit
a8212e8910
|
@ -18,6 +18,10 @@ task test, "Run all tests":
|
|||
exec "nim c -r tests/testsoon"
|
||||
exec "nim c -r -d:release tests/testsoon"
|
||||
|
||||
exec "nim c -r -d:useSysAssert -d:useGcAssert tests/testtime"
|
||||
exec "nim c -r tests/testtime"
|
||||
exec "nim c -r -d:release tests/testtime"
|
||||
|
||||
exec "nim c -r -d:useSysAssert -d:useGcAssert tests/testdatagram"
|
||||
exec "nim c -r tests/testdatagram"
|
||||
exec "nim c -r -d:release tests/testdatagram"
|
||||
|
|
|
@ -55,7 +55,8 @@ type
|
|||
of TransportKind.File:
|
||||
todo2: int
|
||||
|
||||
StreamCallback* = proc(t: StreamTransport,
|
||||
StreamCallback* = proc(server: StreamServer,
|
||||
client: StreamTransport,
|
||||
udata: pointer): Future[void] {.gcsafe.}
|
||||
## New connection callback
|
||||
|
||||
|
@ -489,7 +490,7 @@ when defined(windows):
|
|||
if not acceptFut.failed:
|
||||
var sock = acceptFut.read()
|
||||
if sock != asyncInvalidSocket:
|
||||
discard server.function(
|
||||
discard server.function(server,
|
||||
newStreamSocketTransport(sock, server.bufferSize),
|
||||
server.udata)
|
||||
|
||||
|
@ -667,7 +668,7 @@ else:
|
|||
if int(res) > 0:
|
||||
let sock = wrapAsyncSocket(res)
|
||||
if sock != asyncInvalidSocket:
|
||||
discard server.function(
|
||||
discard server.function(server,
|
||||
newStreamSocketTransport(sock, server.bufferSize),
|
||||
server.udata)
|
||||
break
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
||||
# MIT license (LICENSE-MIT)
|
||||
|
||||
import strutils, net, unittest, os
|
||||
import strutils, unittest, os
|
||||
import ../asyncdispatch2
|
||||
|
||||
when defined(windows):
|
||||
|
@ -21,7 +21,8 @@ const
|
|||
FilesCount = 50
|
||||
FilesTestName = "tests/teststream.nim"
|
||||
|
||||
proc serveClient1(transp: StreamTransport, udata: pointer) {.async.} =
|
||||
proc serveClient1(server: StreamServer,
|
||||
transp: StreamTransport, udata: pointer) {.async.} =
|
||||
while not transp.atEof():
|
||||
var data = await transp.readLine()
|
||||
if len(data) == 0:
|
||||
|
@ -35,7 +36,8 @@ proc serveClient1(transp: StreamTransport, udata: pointer) {.async.} =
|
|||
doAssert(res == len(ans))
|
||||
transp.close()
|
||||
|
||||
proc serveClient2(transp: StreamTransport, udata: pointer) {.async.} =
|
||||
proc serveClient2(server: StreamServer,
|
||||
transp: StreamTransport, udata: pointer) {.async.} =
|
||||
var buffer: array[20, char]
|
||||
var check = "REQUEST"
|
||||
while not transp.atEof():
|
||||
|
@ -58,7 +60,8 @@ proc serveClient2(transp: StreamTransport, udata: pointer) {.async.} =
|
|||
doAssert(res == MessageSize)
|
||||
transp.close()
|
||||
|
||||
proc serveClient3(transp: StreamTransport, udata: pointer) {.async.} =
|
||||
proc serveClient3(server: StreamServer,
|
||||
transp: StreamTransport, udata: pointer) {.async.} =
|
||||
var buffer: array[20, char]
|
||||
var check = "REQUEST"
|
||||
var suffixStr = "SUFFIX"
|
||||
|
@ -82,7 +85,8 @@ proc serveClient3(transp: StreamTransport, udata: pointer) {.async.} =
|
|||
doAssert(res == len(ans))
|
||||
transp.close()
|
||||
|
||||
proc serveClient4(transp: StreamTransport, udata: pointer) {.async.} =
|
||||
proc serveClient4(server: StreamServer,
|
||||
transp: StreamTransport, udata: pointer) {.async.} =
|
||||
var pathname = await transp.readLine()
|
||||
var size = await transp.readLine()
|
||||
var sizeNum = parseInt(size)
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
# Asyncdispatch2 Test Suite
|
||||
# (c) Copyright 2018
|
||||
# Status Research & Development GmbH
|
||||
#
|
||||
# Licensed under either of
|
||||
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
||||
# MIT license (LICENSE-MIT)
|
||||
|
||||
import unittest
|
||||
import ../asyncdispatch2, ../asyncdispatch2/timer
|
||||
|
||||
const TimersCount = 10
|
||||
|
||||
proc timeWorker(time: int): Future[int] {.async.} =
|
||||
var st = fastEpochTime()
|
||||
await sleepAsync(time)
|
||||
var et = fastEpochTime()
|
||||
result = int(et - st)
|
||||
|
||||
proc waitAll[T](futs: seq[Future[T]]): Future[void] =
|
||||
var counter = len(futs)
|
||||
var retFuture = newFuture[void]("waitAll")
|
||||
proc cb(udata: pointer) =
|
||||
dec(counter)
|
||||
if counter == 0:
|
||||
retFuture.complete()
|
||||
for fut in futs:
|
||||
fut.addCallback(cb)
|
||||
return retFuture
|
||||
|
||||
proc test(timeout: int): Future[int] {.async.} =
|
||||
var workers = newSeq[Future[int]](TimersCount)
|
||||
for i in 0..<TimersCount:
|
||||
workers[i] = timeWorker(timeout)
|
||||
await waitAll(workers)
|
||||
var sum = 0
|
||||
for i in 0..<TimersCount:
|
||||
var time = workers[i].read()
|
||||
sum = sum + time
|
||||
result = sum div 10
|
||||
|
||||
when isMainModule:
|
||||
suite "Asynchronous timers test suite":
|
||||
test $TimersCount & " timers with 10ms timeout":
|
||||
var res = waitFor(test(10))
|
||||
check (res >= 10) and (res <= 100)
|
||||
test $TimersCount & " timers with 100ms timeout":
|
||||
var res = waitFor(test(100))
|
||||
check (res >= 100) and (res <= 1000)
|
||||
test $TimersCount & " timers with 1000ms timeout":
|
||||
var res = waitFor(test(1000))
|
||||
check (res >= 1000) and (res <= 2000)
|
Loading…
Reference in New Issue