-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SSDK-623] Improve error reporting when guard-let-self fails to unwra…
…p in SearchEngine (#193) ### Description Fixes https://mapbox.atlassian.net/browse/SSDK-623 - Invoking SDK functions on transient/weak objects (such as `Category().search()`) will create an object with no ownership, it will be deallocated, the network request will complete/fail, and the handler will not have any self object to unwrap and report to. - Previously this could cause an obtuse error message about the network API failing - With this change the error is more correctly attributed to object ownership in the local code ### Checklist - [x] Update `CHANGELOG`
- Loading branch information
Showing
9 changed files
with
81 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Tests/MapboxSearchIntegrationTests/OwningObjectDeallocatedErrorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright © 2024 Mapbox. All rights reserved. | ||
|
||
@testable import MapboxSearch | ||
import XCTest | ||
|
||
/// Tests for SearchEngine objects that fail `guard-let-self` unwrapping during network response completion blocks. | ||
/// These exemplify incorrect behavior such as: `Category().search(for: ...` —— you must keep an owning | ||
/// reference to a search engine that you instantiate and use for network requests. | ||
final class OwningObjectDeallocatedErrorTests: MockServerIntegrationTestCase<SearchBoxMockResponse> { | ||
/// Do not use Category() this way because you will hit the same error. | ||
/// Instead you should own your category instance, such as: `let category = Category()` | ||
func test_category_object() throws { | ||
try server.setResponse(.categoryHotelSearchAlongRoute_JP) | ||
let expectation = XCTestExpectation(description: "Expecting results") | ||
let coordinate1 = CLLocationCoordinate2D(latitude: 35.655614, longitude: 139.7081684) | ||
|
||
Category(locationProvider: DefaultLocationProvider()) | ||
.search(for: .cafe, proximity: coordinate1) { response in | ||
if case .failure(let failure) = response, | ||
let searchFailure = failure as? SearchError | ||
{ | ||
XCTAssertEqual( | ||
searchFailure, | ||
SearchError.owningObjectDeallocated | ||
) | ||
expectation.fulfill() | ||
} else { | ||
XCTFail( | ||
"Expected transient Category() instance to be deallocated before network response completed resulting in an error." | ||
) | ||
} | ||
} | ||
wait(for: [expectation], timeout: 10) | ||
} | ||
} |