Fix testsync.nim to remove unnecessary imports.

Refactored new callsoon test testsoon.nim
Replace test1.nim with testsoon.nim
This commit is contained in:
cheatfate 2018-05-22 13:16:56 +03:00
parent c220b05a4c
commit fdbeddb49b
4 changed files with 68 additions and 25 deletions

View File

@ -8,9 +8,7 @@
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
## This module implements some core synchronization primitives, which
## `asyncdispatch` is really lacking.
## This module implements some core synchronization primitives
import asyncloop, deques
type
@ -110,8 +108,6 @@ proc release*(lock: AsyncLock) =
## other coroutines are blocked waiting for the lock to become unlocked,
## allow exactly one of them to proceed.
var w: Future[void]
# proc wakeup(udata: pointer) {.gcsafe.} = w.complete()
if lock.locked:
lock.locked = false
while len(lock.waiters) > 0:
@ -215,7 +211,6 @@ proc getNoWait*[T](aq: AsyncQueue[T]): T =
##
## If queue ``aq`` is empty, then ``AsyncQueueEmptyError`` exception raised.
var w: Future[void]
if aq.empty():
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
result = aq.queue.popFirst()

View File

@ -1,18 +0,0 @@
import asyncdispatch2
proc testProc() {.async.} =
for i in 1..1_000:
await sleepAsync(1000)
echo "Timeout event " & $i
proc callbackProc(udata: pointer) {.gcsafe.} =
echo "Callback event"
callSoon(callbackProc)
when isMainModule:
discard getGlobalDispatcher()
asyncCheck testProc()
callSoon(callbackProc)
for i in 1..100:
echo "Iteration " & $i
poll()

66
tests/testsoon.nim Normal file
View File

@ -0,0 +1,66 @@
# 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
const CallSoonTests = 10
var soonTest1 = 0
var timeoutsTest1 = 0
var timeoutsTest2 = 0
proc callback1(udata: pointer) {.gcsafe.} =
soonTest1 += int(cast[uint](udata))
proc test1(): int =
callSoon(callback1, cast[pointer](0x12345678))
callSoon(callback1, cast[pointer](0x23456789))
callSoon(callback1, cast[pointer](0x3456789A))
callSoon(callback1, cast[pointer](0x456789AB))
callSoon(callback1, cast[pointer](0x56789ABC))
callSoon(callback1, cast[pointer](0x6789ABCD))
callSoon(callback1, cast[pointer](0x789ABCDE))
callSoon(callback1, cast[pointer](0x89ABCDEF))
callSoon(callback1, cast[pointer](0x9ABCDEF1))
callSoon(callback1, cast[pointer](0xABCDEF12))
callSoon(callback1, cast[pointer](0xBCDEF123))
callSoon(callback1, cast[pointer](0xCDEF1234))
callSoon(callback1, cast[pointer](0xDEF12345))
callSoon(callback1, cast[pointer](0xEF123456))
callSoon(callback1, cast[pointer](0xF1234567))
## All callbacks must be processed exactly with 1 poll() call.
poll()
result = soonTest1
proc testProc() {.async.} =
for i in 1..CallSoonTests:
await sleepAsync(100)
timeoutsTest1 += 1
proc callbackProc(udata: pointer) {.gcsafe.} =
timeoutsTest2 += 1
callSoon(callbackProc)
proc test2(): int =
discard testProc()
callSoon(callbackProc)
## Test must be completed exactly with (CallSoonTests * 2) poll() calls.
for i in 1..(CallSoonTests * 2):
poll()
result = timeoutsTest2 - timeoutsTest1
when isMainModule:
suite "callSoon() tests suite":
test "User-defined callback argument test":
var expect = 0x12345678 + 0x23456789 + 0x3456789A + 0x456789AB +
0x56789ABC + 0x6789ABCD + 0x789ABCDE + 0x89ABCDEF +
0x9ABCDEF1 + 0xABCDEF12 + 0xBCDEF123 + 0xCDEF1234 +
0xDEF12345 + 0xEF123456 + 0xF1234567
check test1() == expect
test "callSoon() behavior test":
check test2() == CallSoonTests

View File

@ -6,7 +6,7 @@
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
import strutils, net, unittest, os
import unittest
import ../asyncdispatch2
var testLockResult = ""