fix error binding in without statement on multiple threads

This commit is contained in:
Mark Spanbroek 2023-08-29 09:13:43 +02:00 committed by markspanbroek
parent e56cf86c4a
commit 5c8d422ac8
2 changed files with 12 additions and 1 deletions

View File

@ -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

View File

@ -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