2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-22 10:16:56 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
import unittest
|
2019-02-06 14:49:11 +00:00
|
|
|
import ../chronos
|
2018-05-22 10:16:56 +00:00
|
|
|
|
|
|
|
const CallSoonTests = 10
|
2018-05-22 11:08:25 +00:00
|
|
|
var soonTest1 = 0'u
|
2018-05-22 10:16:56 +00:00
|
|
|
var timeoutsTest1 = 0
|
|
|
|
var timeoutsTest2 = 0
|
2018-05-25 20:00:32 +00:00
|
|
|
var soonTest2 = 0
|
2018-05-22 10:16:56 +00:00
|
|
|
|
|
|
|
proc callback1(udata: pointer) {.gcsafe.} =
|
2018-05-22 11:08:25 +00:00
|
|
|
soonTest1 = soonTest1 xor cast[uint](udata)
|
2018-05-22 10:16:56 +00:00
|
|
|
|
2018-05-22 11:08:25 +00:00
|
|
|
proc test1(): uint =
|
|
|
|
callSoon(callback1, cast[pointer](0x12345678'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x23456789'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x3456789A'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x456789AB'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x56789ABC'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x6789ABCD'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x789ABCDE'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x89ABCDEF'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x9ABCDEF1'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xABCDEF12'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xBCDEF123'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xCDEF1234'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xDEF12345'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xEF123456'u))
|
|
|
|
callSoon(callback1, cast[pointer](0xF1234567'u))
|
|
|
|
callSoon(callback1, cast[pointer](0x12345678'u))
|
2018-05-22 10:16:56 +00:00
|
|
|
## All callbacks must be processed exactly with 1 poll() call.
|
|
|
|
poll()
|
|
|
|
result = soonTest1
|
|
|
|
|
|
|
|
proc testProc() {.async.} =
|
|
|
|
for i in 1..CallSoonTests:
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2018-05-22 10:16:56 +00:00
|
|
|
timeoutsTest1 += 1
|
|
|
|
|
|
|
|
proc callbackProc(udata: pointer) {.gcsafe.} =
|
|
|
|
timeoutsTest2 += 1
|
|
|
|
callSoon(callbackProc)
|
|
|
|
|
2018-05-23 12:57:07 +00:00
|
|
|
proc test2(timers, callbacks: var int) =
|
2018-05-22 10:16:56 +00:00
|
|
|
callSoon(callbackProc)
|
2018-05-23 12:57:07 +00:00
|
|
|
waitFor(testProc())
|
|
|
|
timers = timeoutsTest1
|
|
|
|
callbacks = timeoutsTest2
|
2018-05-22 10:16:56 +00:00
|
|
|
|
2018-05-25 20:00:32 +00:00
|
|
|
proc testCallback(udata: pointer) =
|
|
|
|
soonTest2 = 987654321
|
|
|
|
|
|
|
|
proc test3(): bool =
|
|
|
|
callSoon(testCallback)
|
|
|
|
poll()
|
|
|
|
result = soonTest2 == 987654321
|
|
|
|
|
2018-05-22 10:16:56 +00:00
|
|
|
when isMainModule:
|
|
|
|
suite "callSoon() tests suite":
|
|
|
|
test "User-defined callback argument test":
|
2018-05-22 11:08:25 +00:00
|
|
|
var values = [0x12345678'u, 0x23456789'u, 0x3456789A'u, 0x456789AB'u,
|
|
|
|
0x56789ABC'u, 0x6789ABCD'u, 0x789ABCDE'u, 0x89ABCDEF'u,
|
|
|
|
0x9ABCDEF1'u, 0xABCDEF12'u, 0xBCDEF123'u, 0xCDEF1234'u,
|
|
|
|
0xDEF12345'u, 0xEF123456'u, 0xF1234567'u, 0x12345678'u]
|
|
|
|
var expect = 0'u
|
|
|
|
for item in values:
|
|
|
|
expect = expect xor item
|
2018-05-22 10:16:56 +00:00
|
|
|
check test1() == expect
|
2018-05-25 20:00:32 +00:00
|
|
|
test "`Asynchronous dead end` #7193 test":
|
2018-05-23 12:57:07 +00:00
|
|
|
var timers, callbacks: int
|
|
|
|
test2(timers, callbacks)
|
|
|
|
check:
|
|
|
|
timers == CallSoonTests
|
|
|
|
callbacks > CallSoonTests * 2
|
2018-05-25 20:00:32 +00:00
|
|
|
test "`callSoon() is not working prior getGlobalDispatcher()` #7192 test":
|
|
|
|
check test3() == true
|