From c6efca5a7f016dc3313698cc9a6077be43a864cd Mon Sep 17 00:00:00 2001 From: PetrGuan Date: Thu, 12 May 2022 21:52:21 +0800 Subject: [PATCH 1/3] Add thread safety unit test for LazyCache Add thread safety unit test for LazyCache --- Tests/TSCBasicTests/LazyCacheTests.swift | 46 +++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Tests/TSCBasicTests/LazyCacheTests.swift b/Tests/TSCBasicTests/LazyCacheTests.swift index 3c6b2540..8736542c 100644 --- a/Tests/TSCBasicTests/LazyCacheTests.swift +++ b/Tests/TSCBasicTests/LazyCacheTests.swift @@ -13,20 +13,19 @@ import XCTest import TSCBasic class LazyCacheTests: XCTestCase { - func testBasics() { - class Foo { - var numCalls = 0 - - var bar: Int { return barCache.getValue(self) } - var barCache = LazyCache(someExpensiveMethod) - func someExpensiveMethod() -> Int { - numCalls += 1 - return 42 - } - + private class Foo { + var numCalls = 0 + + var bar: Int { return barCache.getValue(self) } + var barCache = LazyCache(someExpensiveMethod) + func someExpensiveMethod() -> Int { + numCalls += 1 + return 42 } - // FIXME: Make this a more interesting test once we have concurrency primitives. + } + + func testBasics() { for _ in 0..<10 { let foo = Foo() XCTAssertEqual(foo.numCalls, 0) @@ -36,4 +35,27 @@ class LazyCacheTests: XCTestCase { } } } + + func testThreadSafety() { + let dispatchGroup = DispatchGroup() + let exp = expectation(description: "multi thread") + for _ in 0..<10 { + let foo = Foo() + for _ in 0..<10 { + dispatchGroup.enter() + DispatchQueue.global().async { + XCTAssertEqual(foo.bar, 42) + dispatchGroup.leave() + + XCTAssertEqual(foo.numCalls, 1) + } + } + } + + dispatchGroup.notify(queue: .main) { + exp.fulfill() + } + + wait(for: [exp], timeout: 0.5) + } } From 33184d4678e664b755750edafdc6a2ba472c3ab1 Mon Sep 17 00:00:00 2001 From: PetrGuan Date: Thu, 12 May 2022 22:00:25 +0800 Subject: [PATCH 2/3] Update LazyCacheTests.swift --- Tests/TSCBasicTests/LazyCacheTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/TSCBasicTests/LazyCacheTests.swift b/Tests/TSCBasicTests/LazyCacheTests.swift index 8736542c..7e96c309 100644 --- a/Tests/TSCBasicTests/LazyCacheTests.swift +++ b/Tests/TSCBasicTests/LazyCacheTests.swift @@ -56,6 +56,6 @@ class LazyCacheTests: XCTestCase { exp.fulfill() } - wait(for: [exp], timeout: 0.5) + wait(for: [exp], timeout: 0.2) } } From 2e5b0c1c0793011148b633ba5c11fcb490860733 Mon Sep 17 00:00:00 2001 From: PetrGuan Date: Wed, 15 Jun 2022 20:23:33 +0800 Subject: [PATCH 3/3] Pass dispatchGroup to DispatchQueue instead --- Tests/TSCBasicTests/LazyCacheTests.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Tests/TSCBasicTests/LazyCacheTests.swift b/Tests/TSCBasicTests/LazyCacheTests.swift index 7e96c309..f364ee33 100644 --- a/Tests/TSCBasicTests/LazyCacheTests.swift +++ b/Tests/TSCBasicTests/LazyCacheTests.swift @@ -39,14 +39,12 @@ class LazyCacheTests: XCTestCase { func testThreadSafety() { let dispatchGroup = DispatchGroup() let exp = expectation(description: "multi thread") + for _ in 0..<10 { let foo = Foo() for _ in 0..<10 { - dispatchGroup.enter() - DispatchQueue.global().async { + DispatchQueue.global().async(group: dispatchGroup) { XCTAssertEqual(foo.bar, 42) - dispatchGroup.leave() - XCTAssertEqual(foo.numCalls, 1) } }