Skip to content

Commit

Permalink
Merge pull request #16 from orchetect/dev
Browse files Browse the repository at this point in the history
Updated `@Atomic` unit test
  • Loading branch information
orchetect authored Oct 20, 2021
2 parents 388dc87 + 97424ba commit b2d03c6
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions Tests/OTCoreTests/Atomics/Atomics Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {

func testAtomic_BruteForce_ConcurrentMutations() {

let completionTimeout = expectation(description: "Test Completion Timeout")

class Foo {
@Atomic var dict: [String: Int] = [:]
@Atomic var array: [String] = []
Expand Down Expand Up @@ -115,7 +117,12 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {
}
}

g.wait()
DispatchQueue.global().async {
g.wait()
completionTimeout.fulfill()
}

wait(for: [completionTimeout], timeout: 10)

XCTAssertEqual(foo.dict.count, 0)
XCTAssertEqual(foo.array.count, 0)
Expand All @@ -127,6 +134,8 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13.0, watchOS 6.0, *)
func testAtomic_BruteForce_ConcurrentWriteRandomReads() {

let completionTimeout = expectation(description: "Test Completion Timeout")

class Foo {
@Atomic var dict: [String: Int] = [:]
@Atomic var array: [String] = []
Expand Down Expand Up @@ -171,7 +180,12 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {
}
}

g.wait()
DispatchQueue.global().async {
g.wait()
completionTimeout.fulfill()
}

wait(for: [completionTimeout], timeout: 10)

timer.cancel()

Expand All @@ -181,6 +195,8 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {
/// This test is more useful with Thread Sanitizer on.
func testAtomic_BruteForce_ConcurrentWriteAndRead() {

let completionTimeout = expectation(description: "Test Completion Timeout")

class Foo {
@Atomic var dict: [String: Int] = [:]
@Atomic var array: [String] = []
Expand All @@ -195,28 +211,31 @@ class Extensions_Swift_Atomics_Tests: XCTestCase {

let iterations = 100_000

DispatchQueue.global().async {
for index in 0..<iterations {
writeGroup.enter()
DispatchQueue.global().async {
foo.dict["key"] = index
foo.array[0] = "\(index)"
}
for index in 0..<iterations {
writeGroup.enter()
DispatchQueue.global().async {
foo.dict["key"] = index
foo.array[0] = "\(index)"
writeGroup.leave()
}
}

DispatchQueue.global().async {
for _ in 0..<iterations {
writeGroup.enter()
DispatchQueue.global().async {
_ = foo.dict["key"]
if foo.array.count > 0 { _ = foo.array[0] }
}
for _ in 0..<iterations {
readGroup.enter()
DispatchQueue.global().async {
_ = foo.dict["key"]
if foo.array.count > 0 { _ = foo.array[0] }
readGroup.leave()
}
}

writeGroup.wait()
readGroup.wait()
DispatchQueue.global().async {
writeGroup.wait()
readGroup.wait()
completionTimeout.fulfill()
}

wait(for: [completionTimeout], timeout: 10)

}

Expand Down

0 comments on commit b2d03c6

Please sign in to comment.