2018-05-22 23:28:16 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
proc testFuture1(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100)
|
|
|
|
|
|
|
|
proc testFuture2(): Future[int] {.async.} =
|
|
|
|
return 1
|
|
|
|
|
2018-05-27 05:49:47 +00:00
|
|
|
proc testFuture3(): Future[int] {.async.} =
|
2018-05-22 23:28:16 +00:00
|
|
|
result = await testFuture2()
|
|
|
|
|
2018-07-20 08:58:01 +00:00
|
|
|
proc testFuture4(): Future[int] {.async.} =
|
|
|
|
## Test for not immediately completed future and timeout = -1
|
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
var res = await wait(testFuture1(), -1)
|
|
|
|
result = 1
|
|
|
|
except:
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
if result == 0:
|
2018-07-24 13:55:51 +00:00
|
|
|
return -1
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
## Test for immediately completed future and timeout = -1
|
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
var res = await wait(testFuture2(), -1)
|
2018-07-24 13:55:51 +00:00
|
|
|
result = 2
|
2018-07-20 08:58:01 +00:00
|
|
|
except:
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
if result == 0:
|
2018-07-24 13:55:51 +00:00
|
|
|
return -2
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
## Test for not immediately completed future and timeout = 0
|
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
var res = await wait(testFuture1(), 0)
|
|
|
|
except AsyncTimeoutError:
|
2018-07-24 13:55:51 +00:00
|
|
|
result = 3
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
if result == 0:
|
2018-07-24 13:55:51 +00:00
|
|
|
return -3
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
## Test for immediately completed future and timeout = 0
|
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
var res = await wait(testFuture2(), 0)
|
2018-07-24 13:55:51 +00:00
|
|
|
result = 4
|
2018-07-20 08:58:01 +00:00
|
|
|
except:
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
if result == 0:
|
2018-07-24 13:55:51 +00:00
|
|
|
return -4
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
## Test for future which cannot be completed in timeout period
|
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
var res = await wait(testFuture1(), 50)
|
|
|
|
except AsyncTimeoutError:
|
2018-07-24 13:55:51 +00:00
|
|
|
result = 5
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
if result == 0:
|
2018-07-24 13:55:51 +00:00
|
|
|
return -5
|
2018-07-20 08:58:01 +00:00
|
|
|
|
|
|
|
## Test for future which will be completed before timeout exceeded.
|
|
|
|
try:
|
2018-07-24 14:02:32 +00:00
|
|
|
var res = await wait(testFuture1(), 300)
|
2018-07-24 13:55:51 +00:00
|
|
|
result = 6
|
2018-07-20 08:58:01 +00:00
|
|
|
except:
|
2018-07-24 13:55:51 +00:00
|
|
|
result = -6
|
2018-07-20 08:58:01 +00:00
|
|
|
|
2018-05-22 23:28:16 +00:00
|
|
|
proc test1(): bool =
|
|
|
|
var fut = testFuture1()
|
|
|
|
poll()
|
|
|
|
poll()
|
|
|
|
result = fut.finished
|
|
|
|
|
|
|
|
proc test2(): bool =
|
|
|
|
var fut = testFuture3()
|
|
|
|
result = fut.finished
|
|
|
|
|
2018-05-27 05:49:47 +00:00
|
|
|
proc test3(): string =
|
|
|
|
var testResult = ""
|
|
|
|
var fut = testFuture1()
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "1"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "2"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "3"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "4"
|
|
|
|
fut.addCallback proc(udata: pointer) =
|
|
|
|
testResult &= "5"
|
|
|
|
discard waitFor(fut)
|
|
|
|
poll()
|
|
|
|
if fut.finished:
|
|
|
|
result = testResult
|
|
|
|
|
2018-05-30 04:35:27 +00:00
|
|
|
proc test4(): string =
|
|
|
|
var testResult = ""
|
|
|
|
var fut = testFuture1()
|
|
|
|
proc cb1(udata: pointer) =
|
|
|
|
testResult &= "1"
|
|
|
|
proc cb2(udata: pointer) =
|
|
|
|
testResult &= "2"
|
|
|
|
proc cb3(udata: pointer) =
|
|
|
|
testResult &= "3"
|
|
|
|
proc cb4(udata: pointer) =
|
|
|
|
testResult &= "4"
|
|
|
|
proc cb5(udata: pointer) =
|
|
|
|
testResult &= "5"
|
|
|
|
fut.addCallback cb1
|
|
|
|
fut.addCallback cb2
|
|
|
|
fut.addCallback cb3
|
|
|
|
fut.addCallback cb4
|
|
|
|
fut.addCallback cb5
|
|
|
|
fut.removeCallback cb3
|
|
|
|
discard waitFor(fut)
|
|
|
|
poll()
|
|
|
|
if fut.finished:
|
|
|
|
result = testResult
|
|
|
|
|
2018-07-24 13:55:51 +00:00
|
|
|
proc test5(): int =
|
|
|
|
result = waitFor(testFuture4())
|
2018-07-20 08:58:01 +00:00
|
|
|
|
2018-05-22 23:28:16 +00:00
|
|
|
when isMainModule:
|
|
|
|
suite "Future[T] behavior test suite":
|
2018-05-27 05:49:47 +00:00
|
|
|
test "Async undefined behavior (#7758) test":
|
2018-05-22 23:28:16 +00:00
|
|
|
check test1() == true
|
|
|
|
test "Immediately completed asynchronous procedure test":
|
|
|
|
check test2() == true
|
2018-05-27 05:49:47 +00:00
|
|
|
test "Future[T] callbacks are invoked in reverse order (#7197) test":
|
|
|
|
check test3() == "12345"
|
2018-05-30 04:35:27 +00:00
|
|
|
test "Future[T] callbacks not changing order after removeCallback()":
|
|
|
|
check test4() == "1245"
|
2018-07-20 08:58:01 +00:00
|
|
|
test "wait[T]() test":
|
2018-07-24 13:55:51 +00:00
|
|
|
check test5() == 6
|