Skip to content

Commit 61842cb

Browse files
committed
Fixed tests
Capture the geocoder until after the completion handler is executed. These failures suggest that MBGeocoder had been leaking all this time until it was refactored to use NSURLSession instead of NSURLConnection.
1 parent 9f267f8 commit 61842cb

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class MBGeocoder: NSObject {
2222

2323
private var task: NSURLSessionDataTask?
2424

25-
internal var errorForSimultaneousRequests: NSError {
25+
private var errorForSimultaneousRequests: NSError {
2626
let userInfo = [
2727
NSLocalizedFailureReasonErrorKey: "Cannot geocode on an MBGeocoder object that is already geocoding.",
2828
]
@@ -59,7 +59,7 @@ public class MBGeocoder: NSObject {
5959

6060
// public func geocodeAddressString(addressString: String, inRegion region: CLRegion, completionHandler: MBGeocodeCompletionHandler)
6161

62-
internal func taskWithRouter(router: MBGeocoderRouter, completionHandler completion: MBGeocodeCompletionHandler) -> NSURLSessionDataTask? {
62+
private func taskWithRouter(router: MBGeocoderRouter, completionHandler completion: MBGeocodeCompletionHandler) -> NSURLSessionDataTask? {
6363
return router.loadJSON(JSON.self) { [weak self] (json, error) in
6464
guard let dataTaskSelf = self where dataTaskSelf.task?.state == .Completed
6565
else {

MapboxGeocoderTests/ReverseGeocodingTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class ReverseGeocodingTests: XCTestCase {
3838
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil)
3939
}
4040

41-
MBGeocoder(accessToken: accessToken).reverseGeocodeLocation(
41+
let geocoder = MBGeocoder(accessToken: accessToken)
42+
geocoder.reverseGeocodeLocation(
4243
CLLocation(latitude: 37.13284000, longitude: -95.78558000)) { (placemarks, error) in
4344
if let result = placemarks?.first where placemarks?.count > 0 {
4445
resultsExpectation.fulfill()
@@ -102,7 +103,10 @@ class ReverseGeocodingTests: XCTestCase {
102103
}
103104
}
104105

105-
waitForExpectationsWithTimeout(1, handler: nil)
106+
waitForExpectationsWithTimeout(1) { (error) in
107+
XCTAssertNil(error, "Error: \(error)")
108+
XCTAssertFalse(geocoder.geocoding)
109+
}
106110
}
107111

108112
func testInvalidReverseGeocode() {
@@ -113,12 +117,16 @@ class ReverseGeocodingTests: XCTestCase {
113117
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil)
114118
}
115119

116-
MBGeocoder(accessToken: accessToken).reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in
120+
let geocoder = MBGeocoder(accessToken: accessToken)
121+
geocoder.reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in
117122
if placemarks?.count == 0 {
118123
resultsExpection.fulfill()
119124
}
120125
}
121126

122-
waitForExpectationsWithTimeout(1, handler: nil)
127+
waitForExpectationsWithTimeout(1) { (error) in
128+
XCTAssertNil(error, "Error: \(error)")
129+
XCTAssertFalse(geocoder.geocoding)
130+
}
123131
}
124132
}

0 commit comments

Comments
 (0)