threadsafe MathFont, MTFontV2, MTFontMathTableV2 with concurrent testScripts.

This commit is contained in:
Peter Tang
2023-09-18 13:30:31 +08:00
parent be802ae0c6
commit 5de5ea677e
6 changed files with 293 additions and 69 deletions

View File

@@ -1,6 +1,6 @@
//
// MTFontMathTableV2Tests.swift
//
//
//
// Created by Peter Tang on 15/9/2023.
//
@@ -25,4 +25,57 @@ final class MTFontMathTableV2Tests: XCTestCase {
print("\($0.rawValue).plist: \(values)")
}
}
private let executionQueue = DispatchQueue(label: "com.swiftmath.mathbundle", attributes: .concurrent)
private let executionGroup = DispatchGroup()
let totalCases = 1000
var testCount = 0
func testConcurrentThreadsafeScript() throws {
testCount = 0
var mathFont: MathFont { .allCases.randomElement()! }
var size: CGFloat { CGFloat.random(in: 20 ... 40) }
let mtfonts = Array( 0 ..< 10 ).map { _ in mathFont.mtfont(size: size) }
for caseNumber in 0 ..< totalCases {
helperConcurrentMTFontMathTableV2(caseNumber, mtfont: mtfonts.randomElement()!, in: executionGroup, on: executionQueue)
}
executionGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
XCTAssertEqual(self.testCount, totalCases)
print("\(self.testCount) completed =================")
}
}
func helperConcurrentMTFontMathTableV2(_ count: Int, mtfont: MTFontV2, in group: DispatchGroup, on queue: DispatchQueue) {
let workitem = DispatchWorkItem {
let mTable = mtfont.mathTable
let values = [
mTable?.fractionNumeratorDisplayStyleShiftUp,
mTable?.fractionNumeratorShiftUp,
mTable?.fractionDenominatorDisplayStyleShiftDown,
mTable?.fractionDenominatorShiftDown,
mTable?.fractionNumeratorDisplayStyleGapMin,
mTable?.fractionNumeratorGapMin,
].compactMap{$0}
if count % 50 == 0 {
print(values) // accessed these values on global thread.
}
XCTAssertNotNil(mTable)
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let mTable = mtfont.mathTable
if count % 70 == 0 {
let values = [
mTable?.fractionNumeratorDisplayStyleShiftUp,
mTable?.fractionNumeratorShiftUp,
mTable?.fractionDenominatorDisplayStyleShiftDown,
mTable?.fractionDenominatorShiftDown,
mTable?.fractionNumeratorDisplayStyleGapMin,
mTable?.fractionNumeratorGapMin,
].compactMap{$0}
print(values) // accessed these values on main thread.
}
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
}