Skip to content

Commit

Permalink
Add synchronous networking (#174)
Browse files Browse the repository at this point in the history
* Add synchronous networking

* Fix wrong merging
  • Loading branch information
3lvis committed Jan 29, 2017
1 parent ecbc85b commit 828f3c5
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Sources/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public extension Networking {
public func imageFromCache(_ path: String, cacheName: String? = nil, completion: @escaping (_ image: NetworkingImage?) -> Void) {
let object = self.imageFromCache(path, cacheName: cacheName)

TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(object)
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@ public extension Networking {
public func dataFromCache(for path: String, cacheName: String? = nil, completion: @escaping (_ data: Data?) -> Void) {
let object = self.dataFromCache(for: path, cacheName: cacheName)

TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(object)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking+Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public extension Networking {
@discardableResult
public func downloadImage(_ path: String, cacheName: String? = nil, completion: @escaping (_ image: NetworkingImage?, _ error: NSError?) -> Void) -> String {
let requestIdentifier = self.request(.GET, path: path, cacheName: cacheName, parameterType: nil, parameters: nil, parts: nil, responseType: .image) { response, _, error in
TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(response as? NetworkingImage, error)
}
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public class Networking {
var configurationType: ConfigurationType

/**
Flag used to disable synchronous request when running automatic tests.
Flag used to indicate synchronous request.
*/
var disableTestingMode = false
public var isSynchronous = false

/**
Flag used to disable error logging. Useful when want to disable log before release build.
Expand Down Expand Up @@ -456,14 +456,14 @@ extension Networking {
}
}
}
TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(returnedResponse, headers, returnedError)
}
}
case .data, .image:
let object = self.objectFromCache(for: path, cacheName: cacheName, responseType: responseType)
if let object = object {
TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(object, [String: Any](), nil)
}
} else {
Expand All @@ -486,7 +486,7 @@ extension Networking {
fatalError("Response Type is different than Data and Image")
}
}
TestCheck.testBlock(self.disableTestingMode) {
TestCheck.testBlock(self.isSynchronous) {
completion(returnedResponse, headers, error)
}
}
Expand Down Expand Up @@ -623,7 +623,7 @@ extension Networking {
}
}

if TestCheck.isTesting && self.disableTestingMode == false {
if TestCheck.isTesting && self.isSynchronous == false {
semaphore.signal()
} else {
DispatchQueue.main.async {
Expand All @@ -642,7 +642,7 @@ extension Networking {
session.taskDescription = requestID
session.resume()

if TestCheck.isTesting && self.disableTestingMode == false {
if TestCheck.isTesting && self.isSynchronous == false {
_ = semaphore.wait(timeout: DispatchTime.now() + 60.0)
self.logError(parameterType: parameterType, parameters: parameters, data: returnedData, request: request as URLRequest, response: returnedResponse, error: connectionError as NSError?)
if let unauthorizedRequestCallback = self.unauthorizedRequestCallback, let error = connectionError as NSError?, error.code == 403 || error.code == 401 {
Expand Down
4 changes: 2 additions & 2 deletions Tests/DELETETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class DELETETests: XCTestCase {
let expectation = self.expectation(description: "testCancelDELETE")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.DELETE("/delete") { _, error in
XCTAssertTrue(completed)
Expand All @@ -105,7 +105,7 @@ class DELETETests: XCTestCase {
let expectation = self.expectation(description: "testCancelDELETE")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
let requestID = networking.DELETE("/delete") { _, error in
XCTAssertEqual(error?.code, URLError.cancelled.rawValue)
expectation.fulfill()
Expand Down
20 changes: 10 additions & 10 deletions Tests/DeprecatedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DeprecatedTests: XCTestCase {
func testCancelWithRequestID() {
let expectation = self.expectation(description: "testCancelAllRequests")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var cancelledGET = false

let requestID = networking.GET("/get") { _, error in
Expand All @@ -27,7 +27,7 @@ class DeprecatedTests: XCTestCase {
func testCancelAllRequests() {
let expectation = self.expectation(description: "testCancelAllRequests")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var cancelledGET = false
var cancelledPOST = false

Expand Down Expand Up @@ -58,7 +58,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelGET")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.GET("/get") { _, error in
XCTAssertTrue(completed)
Expand All @@ -77,7 +77,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelGET")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
let requestID = networking.GET("/get") { _, error in
XCTAssertTrue(completed)
Expand All @@ -96,7 +96,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPOST")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.POST("/post", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -115,7 +115,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPOST")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
let requestID = networking.POST("/post", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -134,7 +134,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPUT")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.PUT("/put", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -153,7 +153,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPUT")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
let requestID = networking.PUT("/put", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -172,7 +172,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelDELETE")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.DELETE("/delete") { _, error in
XCTAssertTrue(completed)
Expand All @@ -191,7 +191,7 @@ class DeprecatedTests: XCTestCase {
let expectation = self.expectation(description: "testCancelDELETE")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
let requestID = networking.DELETE("/delete") { _, error in
XCTAssertTrue(completed)
Expand Down
6 changes: 3 additions & 3 deletions Tests/GETTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GETTests: XCTestCase {
func testRequestReturnBlockInMainThread() {
let expectation = self.expectation(description: "testRequestReturnBlockInMainThread")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
networking.GET("/get") { _, _ in
XCTAssertTrue(Thread.isMainThread)
expectation.fulfill()
Expand Down Expand Up @@ -117,7 +117,7 @@ class GETTests: XCTestCase {
let expectation = self.expectation(description: "testCancelGET")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.GET("/get") { _, error in
XCTAssertTrue(completed)
Expand All @@ -135,7 +135,7 @@ class GETTests: XCTestCase {
let expectation = self.expectation(description: "testCancelGET")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
let requestID = networking.GET("/get") { _, error in
XCTAssertEqual(error?.code, URLError.cancelled.rawValue)
expectation.fulfill()
Expand Down
4 changes: 2 additions & 2 deletions Tests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImageTests: XCTestCase {
func testDownloadImageReturnBlockInMainThread() {
let expectation = self.expectation(description: "testDownloadImageReturnBlockInMainThread")
let networking = Networking(baseURL: self.baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
networking.downloadImage("/image/png") { _, _ in
XCTAssertTrue(Thread.isMainThread)
expectation.fulfill()
Expand Down Expand Up @@ -128,7 +128,7 @@ class ImageTests: XCTestCase {
let expectation = self.expectation(description: "testCancelImageDownload")

let networking = Networking(baseURL: self.baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
let path = "/image/png"

try! Helper.removeFileIfNeeded(networking, path: path)
Expand Down
8 changes: 4 additions & 4 deletions Tests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class NetworkingTests: XCTestCase {
let expectation = self.expectation(description: "testSkipTestMode")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true

var synchronous = false
networking.GET("/get") { _, _ in
Expand Down Expand Up @@ -144,7 +144,7 @@ class NetworkingTests: XCTestCase {
func testCancelWithRequestID() {
let expectation = self.expectation(description: "testCancelAllRequests")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var cancelledGET = false

let requestID = networking.GET("/get") { _, error in
Expand All @@ -164,7 +164,7 @@ class NetworkingTests: XCTestCase {
func testCancelAllRequests() {
let expectation = self.expectation(description: "testCancelAllRequests")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var cancelledGET = false
var cancelledPOST = false

Expand Down Expand Up @@ -194,7 +194,7 @@ class NetworkingTests: XCTestCase {
func testCancelRequestsReturnInMainThread() {
let expectation = self.expectation(description: "testCancelRequestsReturnInMainThread")
let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
networking.GET("/get") { _, error in
XCTAssertTrue(Thread.isMainThread)
XCTAssertEqual(error?.code, URLError.cancelled.rawValue)
Expand Down
4 changes: 2 additions & 2 deletions Tests/POSTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class POSTTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPOST")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.POST("/post", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -222,7 +222,7 @@ class POSTTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPOST")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
let requestID = networking.POST("/post", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertEqual(error?.code, URLError.cancelled.rawValue)
expectation.fulfill()
Expand Down
4 changes: 2 additions & 2 deletions Tests/PUTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PUTTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPUT")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
var completed = false
networking.PUT("/put", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertTrue(completed)
Expand All @@ -106,7 +106,7 @@ class PUTTests: XCTestCase {
let expectation = self.expectation(description: "testCancelPUT")

let networking = Networking(baseURL: baseURL)
networking.disableTestingMode = true
networking.isSynchronous = true
let requestID = networking.PUT("/put", parameters: ["username": "jameson", "password": "secret"]) { _, error in
XCTAssertEqual(error?.code, URLError.cancelled.rawValue)
expectation.fulfill()
Expand Down

0 comments on commit 828f3c5

Please sign in to comment.