summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/async/tfuturestream.nim53
-rw-r--r--tests/ccgbugs/twrong_method.nim27
-rw-r--r--tests/coroutines/texceptions.nim23
-rw-r--r--tests/coroutines/texceptions.nim.cfg1
-rw-r--r--tests/coroutines/tgc.nim15
-rw-r--r--tests/coroutines/tgc.nim.cfg1
-rw-r--r--tests/coroutines/titerators.nim24
-rw-r--r--tests/coroutines/titerators.nim.cfg1
-rw-r--r--tests/stdlib/thttpclient.nim10
-rw-r--r--tests/threads/tonthreadcreation.nim11
10 files changed, 156 insertions, 10 deletions
diff --git a/tests/async/tfuturestream.nim b/tests/async/tfuturestream.nim
new file mode 100644
index 000000000..9a8e986a0
--- /dev/null
+++ b/tests/async/tfuturestream.nim
@@ -0,0 +1,53 @@
+discard """
+  file: "tfuturestream.nim"
+  exitcode: 0
+  output: '''
+0
+1
+2
+3
+4
+5
+Done
+Finished
+'''
+"""
+import asyncdispatch
+
+var fs = newFutureStream[int]()
+
+proc alpha() {.async.} =
+  for i in 0 .. 5:
+    await sleepAsync(1000)
+    await fs.write(i)
+
+  echo("Done")
+  fs.complete()
+
+proc beta() {.async.} =
+  while not fs.finished:
+    let (hasValue, value) = await fs.read()
+    if hasValue:
+      echo(value)
+
+  echo("Finished")
+
+asyncCheck alpha()
+waitFor beta()
+
+# TODO: Something like this should work eventually.
+# proc delta(): FutureStream[string] {.async.} =
+#   for i in 0 .. 5:
+#     await sleepAsync(1000)
+#     result.put($i)
+
+#   return ""
+
+# proc omega() {.async.} =
+#   let fut = delta()
+#   while not fut.finished():
+#     echo(await fs.takeAsync())
+
+#   echo("Finished")
+
+# waitFor omega()
\ No newline at end of file
diff --git a/tests/ccgbugs/twrong_method.nim b/tests/ccgbugs/twrong_method.nim
new file mode 100644
index 000000000..9879c6114
--- /dev/null
+++ b/tests/ccgbugs/twrong_method.nim
@@ -0,0 +1,27 @@
+discard """
+  cmd: "nim c -d:release $file"
+  output: '''correct method'''
+"""
+# bug #5439
+type
+  Control* = ref object of RootObj
+
+  ControlImpl* = ref object of Control
+
+  Container* = ref object of ControlImpl
+
+  ContainerImpl* = ref object of Container
+
+method testProc*(control: Control) {.base.} = echo "wrong method"
+
+method testProc*(container: Container) = echo "correct method"
+
+proc main()
+
+main() # wrong method called
+
+proc main() =
+  var container = new ContainerImpl
+  container.testProc()
+
+# main() # correct method called
diff --git a/tests/coroutines/texceptions.nim b/tests/coroutines/texceptions.nim
new file mode 100644
index 000000000..f3debf0a7
--- /dev/null
+++ b/tests/coroutines/texceptions.nim
@@ -0,0 +1,23 @@
+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:
+    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))
+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..66a12ab9d
--- /dev/null
+++ b/tests/coroutines/tgc.nim
@@ -0,0 +1,15 @@
+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..e2623ce2d
--- /dev/null
+++ b/tests/coroutines/titerators.nim
@@ -0,0 +1,24 @@
+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(executionTime >= 55_000_000.Nanos and executionTime < 56_000_000.Nanos, "Coroutines executed too short")
+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/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim
index 7b1111f9b..62c1ebee7 100644
--- a/tests/stdlib/thttpclient.nim
+++ b/tests/stdlib/thttpclient.nim
@@ -13,7 +13,9 @@ proc asyncTest() {.async.} =
   var client = newAsyncHttpClient()
   var resp = await client.request("http://example.com/")
   doAssert(resp.code.is2xx)
-  doAssert("<title>Example Domain</title>" in resp.body)
+  var body = await resp.body
+  body = await resp.body # Test caching
+  doAssert("<title>Example Domain</title>" in body)
 
   resp = await client.request("http://example.com/404")
   doAssert(resp.code.is4xx)
@@ -47,7 +49,8 @@ proc asyncTest() {.async.} =
       echo("Downloaded ", progress, " of ", total)
       echo("Current rate: ", speed div 1000, "kb/s")
     client.onProgressChanged = onProgressChanged
-    discard await client.getContent("http://speedtest-ams2.digitalocean.com/100mb.test")
+    await client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test",
+                              "100mb.test")
 
   client.close()
 
@@ -94,7 +97,8 @@ proc syncTest() =
       echo("Downloaded ", progress, " of ", total)
       echo("Current rate: ", speed div 1000, "kb/s")
     client.onProgressChanged = onProgressChanged
-    discard client.getContent("http://speedtest-ams2.digitalocean.com/100mb.test")
+    client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test",
+                        "100mb.test")
 
   client.close()
 
diff --git a/tests/threads/tonthreadcreation.nim b/tests/threads/tonthreadcreation.nim
index 5d9b777b8..f588a21c9 100644
--- a/tests/threads/tonthreadcreation.nim
+++ b/tests/threads/tonthreadcreation.nim
@@ -7,19 +7,16 @@ var
   someGlobal: string = "some string here"
   perThread {.threadvar.}: string
 
-proc setPerThread() =
-  {.gcsafe.}:
-    deepCopy(perThread, someGlobal)
-
-proc threadDied() {.gcsafe} =
+proc threadDied() {.gcsafe.} =
   echo "dying ", perThread
 
 proc foo() {.thread.} =
+  onThreadDestruction threadDied
+  {.gcsafe.}:
+    deepCopy(perThread, someGlobal)
   echo perThread
 
 proc main =
-  onThreadCreation setPerThread
-  onThreadDestruction threadDied
   var t: Thread[void]
   createThread[void](t, foo)
   t.joinThread()