Created
January 29, 2022 13:39
-
-
Save loganmoseley/e14769604a5b6fb479e15c6db898ad94 to your computer and use it in GitHub Desktop.
Playground: Generating lots of test cases to look at Xcode test parallelization Raw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Example without a Task | |
func testExample2() async throws { | |
env.testNumber = 12 | |
if false { | |
try await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64.random(in: 0...500)) | |
} | |
XCTAssertEqual(env.caseNumber, 1) | |
XCTAssertEqual(env.testNumber, 12) | |
} | |
Example with a Task | |
func testExample2() async throws { | |
env.testNumber = 1012 | |
Task { | |
if false { | |
try await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64.random(in: 0...500)) | |
} | |
DispatchQueue.main.async { | |
XCTAssertEqual(env.caseNumber, 101) | |
XCTAssertEqual(env.testNumber, 1012) | |
} | |
} | |
} | |
*/ | |
var result = [""" | |
import Dispatch | |
import XCTest | |
@testable import ProcessTest | |
"""] | |
func makeTestCase(number: Int, shouldDispatchTask: Bool, shouldSleep: Bool) -> String { | |
let first = """ | |
class ProcessTestCase\(number): XCTestCase { | |
override func setUpWithError() throws { | |
env.caseNumber = \(number) | |
} | |
func testExample1() throws { | |
env.testNumber = \(number)1 | |
XCTAssertEqual(env.caseNumber, \(number)) | |
XCTAssertEqual(env.testNumber, \(number)1) | |
} | |
func testExample2() async throws { | |
env.testNumber = \(number)2 | |
""" | |
let taskBased = """ | |
Task { | |
if \(shouldSleep) { | |
try await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64.random(in: 0...500)) | |
} | |
DispatchQueue.main.async { | |
XCTAssertEqual(env.caseNumber, \(number)) | |
XCTAssertEqual(env.testNumber, \(number)2) | |
} | |
} | |
""" | |
let noTask = """ | |
if \(shouldSleep) { | |
try await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64.random(in: 0...500)) | |
} | |
XCTAssertEqual(env.caseNumber, \(number)) | |
XCTAssertEqual(env.testNumber, \(number)2) | |
""" | |
let last = """ | |
} | |
} | |
""" | |
if shouldDispatchTask { | |
return [first, taskBased, last].joined(separator: "\n") | |
} else { | |
return [first, noTask, last].joined(separator: "\n") | |
} | |
} | |
result.append(contentsOf: (1...50).map { makeTestCase(number: $0, shouldDispatchTask: false, shouldSleep: false) } ) | |
result.append(contentsOf: (51...100).map { makeTestCase(number: $0, shouldDispatchTask: false, shouldSleep: true) } ) | |
result.append(contentsOf: (101...150).map { makeTestCase(number: $0, shouldDispatchTask: true, shouldSleep: false) } ) | |
result.append(contentsOf: (151...200).map { makeTestCase(number: $0, shouldDispatchTask: true, shouldSleep: true) } ) | |
print(result.joined(separator: "\n\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment