2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-22 23:28:16 +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 23:28:16 +00:00
|
|
|
|
|
|
|
proc testFuture1(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(1.milliseconds)
|
2018-05-22 23:28:16 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
proc testFuture100(): Future[int] {.async.} =
|
|
|
|
await sleepAsync(100.milliseconds)
|
|
|
|
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture1(), InfiniteDuration)
|
2018-07-20 08:58:01 +00:00
|
|
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture2(), InfiniteDuration)
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture1(), 0.milliseconds)
|
2018-07-20 08:58:01 +00:00
|
|
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture2(), 0.milliseconds)
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture100(), 50.milliseconds)
|
2018-07-20 08:58:01 +00:00
|
|
|
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:
|
2019-03-24 16:57:36 +00:00
|
|
|
var res = await wait(testFuture100(), 500.milliseconds)
|
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()
|
2019-03-24 16:57:36 +00:00
|
|
|
if not fut.finished:
|
|
|
|
poll()
|
2018-05-22 23:28:16 +00:00
|
|
|
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
|
|
|
|
2019-03-15 00:54:16 +00:00
|
|
|
proc testAllVarargs(): int =
|
|
|
|
var completedFutures = 0
|
|
|
|
|
|
|
|
proc vlient1() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient4() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient5() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient1f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient2f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient3f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient4f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient5f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client4(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client5(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client1f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
waitFor(all(vlient1(), vlient2(), vlient3(), vlient4(), vlient5()))
|
|
|
|
# 5 completed futures = 5
|
|
|
|
result += completedFutures
|
|
|
|
completedFutures = 0
|
|
|
|
try:
|
|
|
|
waitFor(all(vlient1(), vlient1f(),
|
|
|
|
vlient2(), vlient2f(),
|
|
|
|
vlient3(), vlient3f(),
|
|
|
|
vlient4(), vlient4f(),
|
|
|
|
vlient5(), vlient5f()))
|
|
|
|
result -= 10000
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
# 10 completed futures = 10
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
var res = waitFor(all(client1(), client2(), client3(), client4(), client5()))
|
|
|
|
for item in res:
|
|
|
|
result += item
|
|
|
|
# 5 completed futures + 5 values = 10
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
try:
|
|
|
|
var res = waitFor(all(client1(), client1f(),
|
|
|
|
client2(), client2f(),
|
|
|
|
client3(), client3f(),
|
|
|
|
client4(), client4f(),
|
|
|
|
client5(), client5f()))
|
|
|
|
result -= 10000
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
# 10 completed futures = 10
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
proc testAllSeq(): int =
|
|
|
|
var completedFutures = 0
|
|
|
|
var vfutures = newSeq[Future[void]]()
|
|
|
|
var nfutures = newSeq[Future[int]]()
|
|
|
|
|
|
|
|
proc vlient1() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient2() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient3() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient4() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient5() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc vlient1f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient2f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient3f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient4f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc vlient5f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client1(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client2(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client3(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client4(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client5(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
result = 1
|
|
|
|
|
|
|
|
proc client1f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f(): Future[int] {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
vfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
vfutures.add(vlient1())
|
|
|
|
vfutures.add(vlient2())
|
|
|
|
vfutures.add(vlient3())
|
|
|
|
vfutures.add(vlient4())
|
|
|
|
vfutures.add(vlient5())
|
|
|
|
|
|
|
|
waitFor(all(vfutures))
|
|
|
|
# 5 * 10 completed futures = 50
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
vfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
vfutures.add(vlient1())
|
|
|
|
vfutures.add(vlient1f())
|
|
|
|
vfutures.add(vlient2())
|
|
|
|
vfutures.add(vlient2f())
|
|
|
|
vfutures.add(vlient3())
|
|
|
|
vfutures.add(vlient3f())
|
|
|
|
vfutures.add(vlient4())
|
|
|
|
vfutures.add(vlient4f())
|
|
|
|
vfutures.add(vlient5())
|
|
|
|
vfutures.add(vlient5f())
|
|
|
|
|
|
|
|
try:
|
|
|
|
waitFor(all(vfutures))
|
|
|
|
result -= 10000
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
# 10 * 10 completed futures = 100
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
nfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
nfutures.add(client1())
|
|
|
|
nfutures.add(client2())
|
|
|
|
nfutures.add(client3())
|
|
|
|
nfutures.add(client4())
|
|
|
|
nfutures.add(client5())
|
|
|
|
|
|
|
|
var res = waitFor(all(nfutures))
|
|
|
|
for i in 0..<len(nfutures):
|
|
|
|
result += res[i]
|
|
|
|
# 5 * 10 completed futures + 5 * 10 results = 100
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
completedFutures = 0
|
|
|
|
nfutures.setLen(0)
|
|
|
|
for i in 0..<10:
|
|
|
|
nfutures.add(client1())
|
|
|
|
nfutures.add(client1f())
|
|
|
|
nfutures.add(client2())
|
|
|
|
nfutures.add(client2f())
|
|
|
|
nfutures.add(client3())
|
|
|
|
nfutures.add(client3f())
|
|
|
|
nfutures.add(client4())
|
|
|
|
nfutures.add(client4f())
|
|
|
|
nfutures.add(client5())
|
|
|
|
nfutures.add(client5f())
|
|
|
|
|
|
|
|
try:
|
|
|
|
var results = waitFor(all(nfutures))
|
|
|
|
result -= 10000
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
|
|
|
# 10 * 10 completed futures + 0 * 10 results = 100
|
|
|
|
result += completedFutures
|
|
|
|
|
|
|
|
proc testAsyncDiscard(): int =
|
|
|
|
var completedFutures = 0
|
|
|
|
|
|
|
|
proc client1() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client2() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client3() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client4() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client5() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
|
|
|
|
proc client1f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(100.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client2f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(200.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client3f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(300.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client4f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(400.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
proc client5f() {.async.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2019-03-15 00:54:16 +00:00
|
|
|
inc(completedFutures)
|
|
|
|
if true:
|
|
|
|
raise newException(ValueError, "")
|
|
|
|
|
|
|
|
asyncDiscard client1()
|
|
|
|
asyncDiscard client1f()
|
|
|
|
asyncDiscard client2()
|
|
|
|
asyncDiscard client2f()
|
|
|
|
asyncDiscard client3()
|
|
|
|
asyncDiscard client3f()
|
|
|
|
asyncDiscard client4()
|
|
|
|
asyncDiscard client4f()
|
|
|
|
asyncDiscard client5()
|
|
|
|
asyncDiscard client5f()
|
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
waitFor(sleepAsync(2000.milliseconds))
|
2019-03-15 00:54:16 +00:00
|
|
|
result = completedFutures
|
|
|
|
|
|
|
|
proc testAllZero(): bool =
|
|
|
|
var tseq = newSeq[Future[int]]()
|
|
|
|
var fut = all(tseq)
|
|
|
|
result = fut.finished
|
|
|
|
|
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
|
2019-03-15 00:54:16 +00:00
|
|
|
test "all[T](varargs) test":
|
|
|
|
check testAllVarargs() == 35
|
|
|
|
test "all[T](seq) test":
|
|
|
|
check testAllSeq() == 350
|
|
|
|
test "all[T](zero) test":
|
|
|
|
check testAllZero() == true
|
|
|
|
test "asyncDiscard() test":
|
|
|
|
check testAsyncDiscard() == 10
|