Skip to content

Commit

Permalink
Fixed issue with swizzling
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielDemarco committed Feb 18, 2025
1 parent 6832b2a commit 0348557
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
static let timeoutQuick = 0.2

// MARK: - Setup

override func tearDown() async throws {
do {
urlSessionCaptureService.swizzlers.forEach { swizzler in
try? swizzler.unswizzleClassMethod()
try? swizzler.unswizzleInstanceMethod()
}
try otherSwizzler?.unswizzleClassMethod()
} catch let exception {
print(exception)
}
}

func givenCaptureServiceInstalled() {
urlSessionCaptureService = URLSessionCaptureService(options: .init())
openTelemetry = MockEmbraceOpenTelemetry()
Expand Down Expand Up @@ -59,6 +46,18 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
return url
}

func unswizzleDefaultCaptureService() {
urlSessionCaptureService.swizzlers.forEach { swizzler in
try? swizzler.unswizzleClassMethod()
try? swizzler.unswizzleInstanceMethod()
}
}

func unswizzleOtherSwizzler() {
try? otherSwizzler?.unswizzleClassMethod()
try? otherSwizzler?.unswizzleInstanceMethod()
}

// MARK: - Assertions

/// Methods dealing with URLSessionTaskDelegate
Expand All @@ -83,6 +82,8 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
sessionDelegate.didReceiveDataExpectation,
sessionDelegate.didCompleteWithErrorExpectation
], timeout: Self.timeoutQuick)

unswizzleDefaultCaptureService()
}

func givenSomebodyElseSwizzlesURLSessionInit() throws {
Expand Down Expand Up @@ -123,6 +124,9 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeRespondsTo))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))

unswizzleDefaultCaptureService()
unswizzleOtherSwizzler()
}

@available(iOS 15.0, watchOS 8.0, *)
Expand Down Expand Up @@ -158,6 +162,9 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeRespondsTo))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didInvokeForwardingTarget))
XCTAssertTrue(try XCTUnwrap(otherSwizzler?.proxy?.didForwardToTargetSuccessfully))

unswizzleOtherSwizzler()
unswizzleDefaultCaptureService()
}

@available(iOS 15.0, watchOS 8.0, *)
Expand All @@ -182,6 +189,7 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
taskDelegate.didReceiveDataExpectation,
taskDelegate.didCompleteWithErrorExpectation
], timeout: Self.timeoutQuick)
unswizzleDefaultCaptureService()
}

@available(iOS 15.0, watchOS 8.0, *)
Expand All @@ -201,6 +209,8 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
sessionDelegate.didReceiveDataExpectation,
sessionDelegate.didCompleteWithErrorExpectation
], timeout: Self.timeoutQuick)

unswizzleDefaultCaptureService()
}

@available(iOS 15.0, watchOS 8.0, *)
Expand All @@ -218,6 +228,8 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {

// DEV: async/await calls do not call `didCompleteWithError` method as response is handled inline
XCTAssertFalse(sessionDelegate.didCallDidCompleteWithError)

unswizzleDefaultCaptureService()
}

@available(iOS 15.0, watchOS 8.0, *)
Expand All @@ -242,5 +254,7 @@ final class URLSessionDelegateProxyAsTaskDelegateTests: XCTestCase {
XCTAssertFalse(sessionDelegate.didCallDidFinishCollecting)
// DEV: async/await calls do not call `didCompleteWithError` method as response is handled inline
XCTAssertFalse(sessionDelegate.didCallDidCompleteWithError)

unswizzleDefaultCaptureService()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class URLSessionCaptureServiceTests: XCTestCase {
givenURLSessionSwizzlerProvider()
}

override func tearDown() {
sut.swizzlers.forEach {
try? $0.unswizzleClassMethod()
try? $0.unswizzleInstanceMethod()
}
}

func test_onInit_collectorIsUninstalled() {
whenInitializingURLSessionCaptureService()
thenCaptureServiceStatus(is: .uninstalled)
Expand Down

0 comments on commit 0348557

Please sign in to comment.