summary refs log tree commit diff stats
path: root/tests/generics/tthread_generic.nim
blob: 2af5a761583e4e058d31688f7b73632121bfd540 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
discard """
  cmd: "nim $target --hints:on --threads:on $options $file"
  action: compile
"""

type
  ThreadFuncArgs[T] = object of RootObj
    a: proc(): T {.thread.}
    b: proc(val: T) {.thread.}

proc handleThreadFunc(arg: ThreadFuncArgs[int]){.thread.} =
  var fn = arg.a
  var callback = arg.b
  var output = fn()
  callback(output)

proc `@||->`*[T](fn: proc(): T {.thread.},
                 callback: proc(val: T){.thread.}): Thread[ThreadFuncArgs[T]] =
  var thr: Thread[ThreadFuncArgs[T]]
  var args: ThreadFuncArgs[T]
  args.a = fn
  args.b = callback
  createThread(thr, handleThreadFunc, args)
  return thr

proc `||->`*[T](fn: proc(): T{.thread.}, callback: proc(val: T){.thread.}) =
  discard fn @||-> callback

when true:
  import os
  proc testFunc(): int {.thread.} =
    return 1
  proc callbackFunc(val: int) {.thread.} =
    echo($(val))

  var thr = (testFunc @||-> callbackFunc)
  echo("test")
  joinThread(thr)
  os.sleep(3000)
s="s">"https://localhost"+portnum+"/api/plain/users", strings.NewReader(params.Encode())) if err != nil { t.Errorf("%v\n", err) } req.Header.Add("Content-Type", "application/x-www-form-urlencoded") rr := httptest.NewRecorder() apiEndpointPOSTHandler(rr, req) if !tt.wantErr { if rr.Code != http.StatusOK { t.Errorf("Received unexpected non-200 response: %v\n", rr.Code) } } else { if rr.Code != http.StatusBadRequest { t.Errorf("Expected 400 Bad Request, but received: %v\n", rr.Code) } } }) } }