diff --git a/Tests/TSCBasicTests/LazyCacheTests.swift b/Tests/TSCBasicTests/LazyCacheTests.swift index 3c6b2540..f364ee33 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,25 @@ 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 { + DispatchQueue.global().async(group: dispatchGroup) { + XCTAssertEqual(foo.bar, 42) + XCTAssertEqual(foo.numCalls, 1) + } + } + } + + dispatchGroup.notify(queue: .main) { + exp.fulfill() + } + + wait(for: [exp], timeout: 0.2) + } }