Skip to content

Commit

Permalink
removed linux thread safety tests, regenerate test manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Jun 30, 2019
1 parent 40ad490 commit de7ad3a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 94 deletions.
84 changes: 2 additions & 82 deletions Tests/DipTests/ThreadSafetyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.
//

#if canImport(ObjectiveC)
import XCTest
@testable import Dip

Expand Down Expand Up @@ -62,22 +63,6 @@ private var resolvedClients = Array<ClientImp>()

private var container: DependencyContainer!

#if os(Linux)
import Glibc

private var runningThreads: Int = 0
private var lock: pthread_spinlock_t = 0

private let resolveClientSync: () -> Client? = {
let pointer = dispatch_sync { _ in
let resolved = try! container.resolve() as Client
return UnsafeMutableRawPointer(Unmanaged.passRetained(resolved as! ClientImp).toOpaque())
}
guard let clientPointer = pointer else { return nil }
return Unmanaged<ClientImp>.fromOpaque(clientPointer).takeRetainedValue()
}

#else
let queue = OperationQueue()
let lock = RecursiveLock()

Expand All @@ -88,42 +73,23 @@ private let resolveClientSync: () -> Client? = {
}
return client
}

#endif

let resolveServerAsync = {
let server = try! container.resolve() as Server
lock.lock()
resolvedServers.insert(server as! ServerImp)

#if os(Linux)
runningThreads -= 1
#endif

lock.unlock()
}

let resolveClientAsync = {
let client = try! container.resolve() as Client
lock.lock()
resolvedClients.append(client as! ClientImp)

#if os(Linux)
runningThreads -= 1
#endif

lock.unlock()
}

class ThreadSafetyTests: XCTestCase {

#if os(Linux)
required init(name: String, testClosure: @escaping (XCTestCase) throws -> Void) {
pthread_spin_init(&lock, 0)
super.init(name: name, testClosure: testClosure)
}
#endif

override func setUp() {
Dip.logLevel = .Verbose
container = DependencyContainer()
Expand All @@ -138,25 +104,10 @@ class ThreadSafetyTests: XCTestCase {
container.register(.singleton) { ServerImp() as Server }

for _ in 0..<100 {
#if os(Linux)
lock.lock()
runningThreads += 1
lock.unlock()

dispatch_async { _ in
resolveServerAsync()
return nil
}
#else
queue.addOperation(resolveServerAsync)
#endif
}

#if os(Linux)
while runningThreads > 0 { sleep(1) }
#else
queue.waitUntilAllOperationsAreFinished()
#endif

XCTAssertEqual(resolvedServers.count, 1, "Should create only one instance")
}
Expand All @@ -166,25 +117,10 @@ class ThreadSafetyTests: XCTestCase {
container.register { ServerImp() as Server }

for _ in 0..<100 {
#if os(Linux)
lock.lock()
runningThreads += 1
lock.unlock()

dispatch_async { _ in
resolveServerAsync()
return nil
}
#else
queue.addOperation(resolveServerAsync)
#endif
}

#if os(Linux)
while runningThreads > 0 { sleep(1) }
#else
queue.waitUntilAllOperationsAreFinished()
#endif

XCTAssertEqual(resolvedServers.count, 100, "All instances should be different")
}
Expand All @@ -201,25 +137,10 @@ class ThreadSafetyTests: XCTestCase {
}

for _ in 0..<100 {
#if os(Linux)
lock.lock()
runningThreads += 1
lock.unlock()

dispatch_async { _ in
resolveClientAsync()
return nil
}
#else
queue.addOperation(resolveClientAsync)
#endif
}

#if os(Linux)
while runningThreads > 0 { sleep(1) }
#else
queue.waitUntilAllOperationsAreFinished()
#endif

XCTAssertEqual(resolvedClients.count, 100, "Instances should be not reused in different object graphs")
for client in resolvedClients {
Expand All @@ -230,5 +151,4 @@ class ThreadSafetyTests: XCTestCase {
}

}


#endif
17 changes: 5 additions & 12 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ extension AutoInjectionTests {
// to regenerate.
static let __allTests__AutoInjectionTests = [
("testThatItAutoInjectsPropertyWithCollaboratingContainer", testThatItAutoInjectsPropertyWithCollaboratingContainer),
("testThatItAutoInjectsWhenOverridenInDefinition", testThatItAutoInjectsWhenOverridenInDefinition),
("testThatItCallsDidInjectOnAutoInjectedProperty", testThatItCallsDidInjectOnAutoInjectedProperty),
("testThatItCallsResolveDependencyBlockWhenAutoInjecting", testThatItCallsResolveDependencyBlockWhenAutoInjecting),
("testThatItDoesNotAutoInjectIfDisabledInContainer", testThatItDoesNotAutoInjectIfDisabledInContainer),
("testThatItDoesNotAutoInjectIfDisabledInDefinition", testThatItDoesNotAutoInjectIfDisabledInDefinition),
("testThatItDoesNotPassTagToAutoInjectedPropertyWithExplicitTag", testThatItDoesNotPassTagToAutoInjectedPropertyWithExplicitTag),
("testThatItPassesTagToAutoInjectedProperty", testThatItPassesTagToAutoInjectedProperty),
("testThatItResolvesAutoInjectedDependencies", testThatItResolvesAutoInjectedDependencies),
Expand Down Expand Up @@ -113,6 +116,7 @@ extension DipTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__DipTests = [
("test_weak_mirror_regression", test_weak_mirror_regression),
("testItCallsResolveDependenciesOnResolableInstance", testItCallsResolveDependenciesOnResolableInstance),
("testThatCollaboratingContainersAreWeakReferences", testThatCollaboratingContainersAreWeakReferences),
("testThatCollaboratingContainersReuseInstancesResolvedByAnotherContainer", testThatCollaboratingContainersReuseInstancesResolvedByAnotherContainer),
Expand Down Expand Up @@ -162,17 +166,6 @@ extension RuntimeArgumentsTests {
]
}

extension ThreadSafetyTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ThreadSafetyTests = [
("testCircularReferenceThreadSafety", testCircularReferenceThreadSafety),
("testFactoryThreadSafety", testFactoryThreadSafety),
("testSingletonThreadSafety", testSingletonThreadSafety),
]
}

extension TypeForwardingTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand All @@ -189,6 +182,7 @@ extension TypeForwardingTests {
("testThatItOverridesIfSeveralDefinitionsWithTheSameTagForwardTheSameType", testThatItOverridesIfSeveralDefinitionsWithTheSameTagForwardTheSameType),
("testThatItResolvesInstanceByTypeForwarding", testThatItResolvesInstanceByTypeForwarding),
("testThatItReusesInstanceResolvedByTypeForwarding", testThatItReusesInstanceResolvedByTypeForwarding),
("testThatItReusesInstancesResolvedForOptionalType", testThatItReusesInstancesResolvedForOptionalType),
("testThatItThrowsErrorWhenResolvingNotImplementedTypeWithTypeForwarding", testThatItThrowsErrorWhenResolvingNotImplementedTypeWithTypeForwarding),
]
}
Expand All @@ -202,7 +196,6 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(DefinitionTests.__allTests__DefinitionTests),
testCase(DipTests.__allTests__DipTests),
testCase(RuntimeArgumentsTests.__allTests__RuntimeArgumentsTests),
//testCase(ThreadSafetyTests.__allTests__ThreadSafetyTests),
testCase(TypeForwardingTests.__allTests__TypeForwardingTests),
]
}
Expand Down

0 comments on commit de7ad3a

Please sign in to comment.