-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add thread safety #31
Changes from all commits
69d12a6
6e18211
ff41e14
f04435c
0407c2f
fb89794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// Dip | ||
// | ||
// Copyright (c) 2015 Olivier Halligon <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
import XCTest | ||
@testable import Dip | ||
|
||
class ThreadSafetyTests: XCTestCase { | ||
|
||
let container = DependencyContainer() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
container.reset() | ||
} | ||
|
||
func testSingletonThreadSafety() { | ||
|
||
let queue = NSOperationQueue() | ||
let lock = NSRecursiveLock() | ||
var resultSet = Set<ServiceImp1>() | ||
|
||
container.register(.Singleton) { ServiceImp1() as Service } | ||
|
||
for _ in 1...100 { | ||
queue.addOperationWithBlock { | ||
let serviceInstance = try! self.container.resolve() as Service | ||
|
||
lock.lock() | ||
resultSet.insert(serviceInstance as! ServiceImp1) | ||
lock.unlock() | ||
} | ||
} | ||
|
||
queue.waitUntilAllOperationsAreFinished() | ||
|
||
XCTAssertEqual(resultSet.count, 1) | ||
} | ||
|
||
func testFactoryThreadSafety() { | ||
|
||
let queue = NSOperationQueue() | ||
let lock = NSRecursiveLock() | ||
var resultSet = Set<ServiceImp1>() | ||
|
||
container.register() { ServiceImp1() as Service } | ||
|
||
for _ in 1...100 { | ||
queue.addOperationWithBlock { | ||
let serviceInstance = try! self.container.resolve() as Service | ||
|
||
lock.lock() | ||
resultSet.insert(serviceInstance as! ServiceImp1) | ||
lock.unlock() | ||
} | ||
} | ||
|
||
queue.waitUntilAllOperationsAreFinished() | ||
|
||
XCTAssertEqual(resultSet.count, 100) | ||
} | ||
|
||
func testCircularReferenceThreadSafety() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also test it in a different way - buy switching thread inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have added some thread switching within resolveDependencies block in each threading test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant something different, like this: container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in
var client: Client
dispatch_sync(dispatch_get_global_queue()) {
client = try! container.resolve() as Client
}
server.client = client
} The idea is to check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. I did try resolving on separate thread but without the sync wait in the dependency closure, that doesn't work as each resolve triggers another as resolved items array cleared before other thread gets to it :-) Latest should be good ! |
||
|
||
let queue = NSOperationQueue() | ||
let lock = NSLock() | ||
|
||
container.register(.ObjectGraph) { | ||
Client(server: try! self.container.resolve()) as Client | ||
} | ||
|
||
container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in | ||
var client: Client? | ||
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { | ||
client = try! container.resolve() as Client | ||
} | ||
server.client = client! | ||
} | ||
|
||
var results = Array<Client>() | ||
|
||
for _ in 1...100 { | ||
queue.addOperationWithBlock { | ||
//when | ||
let client = try! self.container.resolve() as Client | ||
|
||
lock.lock() | ||
results.append(client) | ||
lock.unlock() | ||
} | ||
} | ||
|
||
queue.waitUntilAllOperationsAreFinished() | ||
|
||
for client in results { | ||
let server = client.server | ||
XCTAssertTrue(server.client === client) | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For code style coherence, please remove leading and trailing empty lines inside both those
threadSafe
implementations.