diff --git a/questionable/private/binderror.nim b/questionable/private/binderror.nim index 5154e37..9ddb5bb 100644 --- a/questionable/private/binderror.nim +++ b/questionable/private/binderror.nim @@ -1,7 +1,7 @@ import std/options var captures {.global, compileTime.}: int -var errorVariable: ptr ref CatchableError +var errorVariable {.threadvar.}: ptr ref CatchableError template captureBindError*(error: var ref CatchableError, expression): auto = let previousErrorVariable = errorVariable diff --git a/testmodules/results/test.nim b/testmodules/results/test.nim index 5059d3a..4eb27be 100644 --- a/testmodules/results/test.nim +++ b/testmodules/results/test.nim @@ -3,8 +3,11 @@ import std/options import std/sequtils import std/strutils import std/sugar +import std/threadpool import pkg/questionable/results +{.experimental: "parallel".} + suite "result": let error = newException(CatchableError, "error") @@ -401,6 +404,14 @@ suite "result": foo() + test "without statement with error works with multiple threads": + proc fail(number: int) = + without _ =? int.failure "error" & $number, error: + check error.msg == "error" & $number + parallel: + for i in 0..<1000: + spawn fail(i) + test "catch can be used to convert exceptions to results": check parseInt("42").catch == 42.success check parseInt("foo").catch.error of ValueError