ios - Swift singleton tests -
I am trying to learn tests with quick and nimble and I have written simple singleton and some tests
Singleton:
Public Class Location Manager: NSObject {// Singleton Public Square var sharedInstance: Location Manager [struct static {static var example: LocationManager? Static Vane Token: dispatch_once_t = 0} dispatch_once (& static .token) {Static.instance = LocationManager ()} Return Static.instance! }} My test:
class space manager speak: quickspace {override funk speck () {description ("location manager") {locationmizer = location The Manager. ShareInstance Reference ("Start") {This ("is not zero") {Hope (locationManager) .notTo (beNil ())} This is ("unique") {go to tempLocationManager = locationManager () Expect (locationManager) .notTo Execute (tempLocationManager)}} ("more shared examples") {go to tempLocationManager = LocationManager.sharedInstance (locationManager). Campaign (equal (tempLocationManager)}}}}}} How can I test my single thread safe? I / I Ve has made a little bit of sample so you can basically get all the code on it:
- Create a bunch of threads called for singleton
-
- Check that all the objects present in that array are the same
I have seen many problems (using Xcode 6.2 Argo Swift 1.1), Even the compiler crashes too. So after following I have changed your singleton implementation to avoid the classic use of GDC. These indicators and unsecured Muttebl pointers that you need to manage, and I crashed because of that
If your singleton class is now:
Import Foundation Public Class Location Manager: NSOBIES {Public class var sharedInstance: LocationManager {struct Static}: Enter a valid example: LocationManager = LocationManager ()} Static.instance return}} and to test it, You have to wait until all the threads have been completed. As the test runner runs on the main tooth, you need to expect Description ("Adding Location Manager to Multiple Concurrent Threads") {Reference ("When Created") {This ( "it should always back in the same thing for every 500 threads") dispatch_async (self.globalBackgroundQueue) {allSingletons "(launched thread \ (i) {var allSingletons = array & lt; i in 1 ... 10 {println (to)); LocationManager & gt ".append (LocationManager.sharedInstance)}} hope (self.allSingletonsEqual (inArray: allSingletons, Singleton: LocationManager.sharedInstance)) toEventually (beTrue (), timeout: 10).}}} < / Pre> Important part is one:
Hope (self.allSingletonsEqual (inArray: allSingletons, Singleton: LocationManager.sharedInstance)). ToEventually (beTrue (), timeout: 10) Here I'm just calling a function, to ensure that every stored object passes like a singleton, and 10 seconds timeout giving
the Test class:
Import Quick Import nimble imports accelerated enrollment example class location manager Spec: QuickSpec {var globalBackgroundQueue: dispatch_queue_t {return Dispatch _ Get_ Globl_cwu (Int (QOS_CLASS_BACKGROUND.value), 0)} func allSingletonsEqual (#inArray: Array & lt; LocationManager & gt ;, singleton: Loc AtionManager) - & gt; Bull {Local for the locality} If the place! = Singleton {Return false}} Return the funnels () (describe ("Adding location manager to multiple concurrent threads") {reference ("when created") {This ("Always in these 500 threads must return to the same object for each ") {var allSingletons = Array & lt; LocationManager & gt; () i for 1 ... 10 {println (launched" Thread (i) ") dispatch_async ( self.globalBackgroundQueue) {allSingletons.append (LocationManager.sharedInstance)}} hope (self.allSingletonsEqual (inArray: allSingletons, Singleton: LocationManager.sharedInstance)). toEventually (beTrue (), ta Implements: 10)}}}}}
Comments
Post a Comment