 import Foundation
 import Siesta
 
-private let stubPropertyKey = "\(NetworkStub.self).stub"
-
-final class NetworkStub: URLProtocol
-    {
-    private static var stubs = [RequestStub]()
-    private static var requestInitialization = Latch(name: "initialization of requests")
+final class NetworkStub
+    {
+    private static var _stubs = [RequestStub]()
+    private static let stubsAccessQueue = DispatchQueue(label: "NetworkStub.stubs access")
 
     static var defaultConfiguration: URLSessionConfiguration
         { wrap(URLSessionConfiguration.ephemeral) }
 
     static func wrap(_ configuration: URLSessionConfiguration) -> URLSessionConfiguration
         {
-        configuration.protocolClasses = [Self.self]
+        configuration.protocolClasses = [StubbedNetworkProtocol.self]
         return configuration
         }
 


    override func stopLoading()
        { }

    static func synchronized<T>(_ action: () -> T) -> T
        {
        objc_sync_enter(NetworkStub.self)
        defer { objc_sync_exit(NetworkStub.self) }
        return action()
        }
    }

struct RequestStub


    override func startLoading()
        {
        let stub = URLProtocol.property(forKey: Self.stubPropertyKey, in: request) as! RequestStub
        stub.awaitPermissionToGo()

        if SiestaSpec.envFlag("RandomTimeDelayInNetworkStubs")
