Section 11/161 menit
11. Serialized vs Parallel Execution
11. Serialized vs Parallel Execution
Default: Paralel
swift
// Semua test ini berjalan paralel (default)
@Suite("Kalkulasi Paralel")
struct CalculationTests {
@Test func testAddition() { #expect(1 + 1 == 2) }
@Test func testSubtraction() { #expect(5 - 3 == 2) }
@Test func testMultiplication() { #expect(3 * 4 == 12) }
// Ketiga test bisa berjalan bersamaan
}
Serialized untuk Shared State
swift
// Database test yang share koneksi
@Suite(.serialized)
struct DatabaseIntegrationTests {
static var db: TestDatabase!
// Karena serialized, setup/teardown predictable
@Test func testInsert() async throws {
try await Self.db.insert(User(name: "Alice"))
let count = try await Self.db.count(User.self)
#expect(count == 1)
}
@Test func testDelete() async throws {
// Berjalan setelah testInsert selesai (serialized)
try await Self.db.deleteAll(User.self)
let count = try await Self.db.count(User.self)
#expect(count == 0)
}
}
Granular Serialization
swift
// Suite luar paralel, sub-suite tertentu serialized
@Suite("Mixed Execution")
struct MixedExecutionTests {
// Test-test ini paralel
@Test func fastTest1() { #expect(true) }
@Test func fastTest2() { #expect(true) }
// Ini serialized karena share resource
@Suite(.serialized, "File System Tests")
struct FileSystemTests {
@Test func testCreate() throws { /* ... */ }
@Test func testRead() throws { /* ... */ }
@Test func testDelete() throws { /* ... */ }
}
}