diff options
Diffstat (limited to 'tests/coroutines')
-rw-r--r-- | tests/coroutines/texceptions.nim | 29 | ||||
-rw-r--r-- | tests/coroutines/texceptions.nim.cfg | 1 | ||||
-rw-r--r-- | tests/coroutines/tgc.nim | 22 | ||||
-rw-r--r-- | tests/coroutines/tgc.nim.cfg | 1 | ||||
-rw-r--r-- | tests/coroutines/titerators.nim | 31 | ||||
-rw-r--r-- | tests/coroutines/titerators.nim.cfg | 1 | ||||
-rw-r--r-- | tests/coroutines/twait.nim | 28 | ||||
-rw-r--r-- | tests/coroutines/twait.nim.cfg | 1 |
8 files changed, 114 insertions, 0 deletions
diff --git a/tests/coroutines/texceptions.nim b/tests/coroutines/texceptions.nim new file mode 100644 index 000000000..31feffdff --- /dev/null +++ b/tests/coroutines/texceptions.nim @@ -0,0 +1,29 @@ +discard """ + targets: "c" + disabled: true +""" + +import coro +var + stackCheckValue = 1100220033 + numbers = newSeqOfCap[int](10) + +proc testExceptions(id: int, sleep: float) = + try: + numbers.add(id) + suspend(sleep) + numbers.add(id) + raise (ref ValueError)() + except: + suspend(sleep) + numbers.add(id) + suspend(sleep) + numbers.add(id) + suspend(sleep) + numbers.add(id) + +start(proc() = testExceptions(1, 0.01)) +start(proc() = testExceptions(2, 0.011)) +coro.run() +doAssert(stackCheckValue == 1100220033, "Thread stack got corrupted") +doAssert(numbers == @[1, 2, 1, 2, 1, 2, 1, 2, 1, 2], "Coroutines executed in incorrect order") diff --git a/tests/coroutines/texceptions.nim.cfg b/tests/coroutines/texceptions.nim.cfg new file mode 100644 index 000000000..b011bc585 --- /dev/null +++ b/tests/coroutines/texceptions.nim.cfg @@ -0,0 +1 @@ +-d:nimCoroutines diff --git a/tests/coroutines/tgc.nim b/tests/coroutines/tgc.nim new file mode 100644 index 000000000..770d413f5 --- /dev/null +++ b/tests/coroutines/tgc.nim @@ -0,0 +1,22 @@ +discard """ + matrix: "--gc:refc; --gc:arc; --gc:orc" + targets: "c" +""" + +when compileOption("gc", "refc") or not defined(openbsd): + # xxx openbsd gave: stdlib_coro.nim.c:406:22: error: array type 'jmp_buf' (aka 'long [11]') is not assignable (*dest).execContext = src.execContext; + import coro + + var maxOccupiedMemory = 0 + + proc testGC() = + var numbers = newSeq[int](100) + maxOccupiedMemory = max(maxOccupiedMemory, getOccupiedMem()) + suspend(0) + + start(testGC) + start(testGC) + run() + + GC_fullCollect() + doAssert(getOccupiedMem() < maxOccupiedMemory, "GC did not free any memory allocated in coroutines") diff --git a/tests/coroutines/tgc.nim.cfg b/tests/coroutines/tgc.nim.cfg new file mode 100644 index 000000000..b011bc585 --- /dev/null +++ b/tests/coroutines/tgc.nim.cfg @@ -0,0 +1 @@ +-d:nimCoroutines diff --git a/tests/coroutines/titerators.nim b/tests/coroutines/titerators.nim new file mode 100644 index 000000000..1ea134811 --- /dev/null +++ b/tests/coroutines/titerators.nim @@ -0,0 +1,31 @@ +discard """ + targets: "c" +disabled: true +""" + +# Timers are always flakey on the testing servers. + +import coro +include system/timers + +var + stackCheckValue = 1100220033 + numbers = newSeqOfCap[int](10) + +iterator theIterator(id: int, sleep: float): int = + for i in 0..<5: + yield 10 * id + i + suspend(sleep) + +proc theCoroutine(id: int, sleep: float32) = + for n in theIterator(id, sleep): + numbers.add(n) + +var start = getTicks() +start(proc() = theCoroutine(1, 0.01)) +start(proc() = theCoroutine(2, 0.011)) +run() + +var executionTime = getTicks() - start +doAssert(stackCheckValue == 1100220033, "Thread stack got corrupted") +doAssert(numbers == @[10, 20, 11, 21, 12, 22, 13, 23, 14, 24], "Coroutines executed in incorrect order") diff --git a/tests/coroutines/titerators.nim.cfg b/tests/coroutines/titerators.nim.cfg new file mode 100644 index 000000000..b011bc585 --- /dev/null +++ b/tests/coroutines/titerators.nim.cfg @@ -0,0 +1 @@ +-d:nimCoroutines diff --git a/tests/coroutines/twait.nim b/tests/coroutines/twait.nim new file mode 100644 index 000000000..2edfcf675 --- /dev/null +++ b/tests/coroutines/twait.nim @@ -0,0 +1,28 @@ +discard """ + output: "Exit 1\nExit 2" + matrix: "--gc:refc; --gc:arc; --gc:orc" + targets: "c" +""" + +when compileOption("gc", "refc") or not defined(openbsd): + # xxx openbsd failed, see tgc.nim + import coro + + var coro1: CoroutineRef + + proc testCoroutine1() = + for i in 0..<10: + suspend(0) + echo "Exit 1" + + proc testCoroutine2() = + coro1.wait() + echo "Exit 2" + + coro1 = coro.start(testCoroutine1) + coro.start(testCoroutine2) + run() +else: + # workaround + echo "Exit 1" + echo "Exit 2" diff --git a/tests/coroutines/twait.nim.cfg b/tests/coroutines/twait.nim.cfg new file mode 100644 index 000000000..b011bc585 --- /dev/null +++ b/tests/coroutines/twait.nim.cfg @@ -0,0 +1 @@ +-d:nimCoroutines |