Skip to content

Commit f78b925

Browse files
committed
Add a test for concurrent access
This test consistently traps on 'main', but succeeds with this `Sendable` implementation.
1 parent d5e6386 commit f78b925

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Tests/RegexTests/MatchTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,5 +1653,35 @@ extension RegexTests {
16531653
let scalarExpected: [Substring] = ["\u{FE0F}💖🧠", "🧠💖☕"]
16541654
XCTAssertEqual(scalarMatches.map { $0.0 }, scalarExpected)
16551655
}
1656+
1657+
func testConcurrentAccess() async throws {
1658+
for _ in 0..<1000 {
1659+
let regex = try Regex(#"abc+d*e?"#)
1660+
let strings = [
1661+
"abc",
1662+
"abccccccccdddddddddde",
1663+
"abcccce",
1664+
"abddddde",
1665+
]
1666+
let matches = await withTaskGroup(of: Optional<Regex<AnyRegexOutput>.Match>.self) { group -> [Regex<AnyRegexOutput>.Match] in
1667+
var result: [Regex<AnyRegexOutput>.Match] = []
1668+
1669+
for str in strings {
1670+
group.addTask {
1671+
str.firstMatch(of: regex)
1672+
}
1673+
}
1674+
1675+
for await match in group {
1676+
guard let match = match else { continue }
1677+
result.append(match)
1678+
}
1679+
1680+
return result
1681+
}
1682+
1683+
XCTAssertEqual(matches.count, 3)
1684+
}
1685+
}
16561686
}
16571687

0 commit comments

Comments
 (0)