Moya: How to avoid error: Error Domain=NSURLErrorDomain Code=-999 "cancelled"

Created on 4 Apr 2019  路  16Comments  路  Source: Moya/Moya

in provider test,

import Quick
import Nimble
import Alamofire
import RxSwift
import RxBlocking
import RxTest
import OHHTTPStubs

@testable import Moya
@testable import DSZHClientFramework

class DSZHApiServiceSpec: QuickSpec {
    override func spec() {
        describe("provider with Single") {
            var provider: MoyaProvider<DSZHApi>!

            beforeEach {
                provider = MoyaProvider<DSZHApi>(endpointClosure: endpointClosure, stubClosure: MoyaProvider.immediatelyStub)
            }

            it("emits a Response object") {
                var response: Response?

                let target: DSZHApi = .getForums
                response = try! provider.rx.request(target).toBlocking().single()
                expect(response).toNot(beNil())
            }

            it("returns stubbed data for get forums request") {
                var json: String?

                let target: DSZHApi = .getForums
                provider.request(target) { result in
                    if case let .success(response) = result {
                        json = String(data: response.data, encoding: .utf8)
                    }
                }

                let sampleData = target.sampleData
                expect(json).to(equal(String(data: sampleData, encoding: .utf8)))
            }
        }
    }
}

I always get error message in xcode output:

2019-04-04 16:18:37.906181+0800 xctest[89156:12465616] Task .<1> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled"

but test case is passed

How to fix it?

question

Most helpful comment

Alright, the PR is in. This fixed our test suite from random logs so it looks fine. Should be releasing a version 13.0.1 when it's merged.

All 16 comments

I have the same error:

2019-04-08 15:34:38.731206-0400 xctest[57365:3755069] Task <4C784BB5-6B62-4A73-90F6-DCD516C9D192>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=http://www.foo.com/api/v1/mobile/ticket_manager/tickets/0, NSErrorFailingURLKey=http://www.foo.com/api/v1/mobile/ticket_manager/tickets/0, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <4C784BB5-6B62-4A73-90F6-DCD516C9D192>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <4C784BB5-6B62-4A73-90F6-DCD516C9D192>.<1>, NSLocalizedDescription=cancelled} [-999]

final class TicketRemoteRepositoryTests: XCTestCase {

  // MARK: - Private Properties

  private var sut: TicketRemoteRepository!

  private var provider: MoyaProvider<APIv3>!

  // MARK: - Lifecycle

  override func setUp() {
    super.setUp()

    provider = MoyaProvider<APIv3>(stubClosure: MoyaProvider.immediatelyStub)

    sut = TicketRemoteRepository(provider: provider)
  }

  override func tearDown() {
    super.tearDown()

    sut = nil
    provider = nil
  }

  // MARK: - Tests

  func test_request_shouldReturnTickets() throws {
    let expectation = XCTestExpectation(description: #function)
    let tickets: [APIv3TicketResponseDTO] = []

    task = provider.request(.ticket(id: 0)) { _ in
      expectation.fulfill()
    }

    wait(for: [expectation], timeout: 1)

    XCTAssertEqual(tickets.count, 13)
  }
}

I had the same error ,it was normal in a few days ago

2019-04-11 16:31:53.861504+0800 MyProject[31077:2361845] Task .<2> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://www.hahamx.cn/mobile_app_data_api.php, NSErrorFailingURLKey=https://www.hahamx.cn/mobile_app_data_api.php, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask .<2>"

@smoothdvd @leohidalgo @huhaosanxiong hey guys thanks for the report. Can you specify what version of Moya do you use? Does it happen only in test cases? Is it only a warning but it still works as expected anyways?

@sunshinejr My Moya is 12.0.1, I will try 13.0.0 to test again.

@smoothdvd @leohidalgo @huhaosanxiong hey guys thanks for the report. Can you specify what version of Moya do you use? Does it happen only in test cases? Is it only a warning but it still works as expected anyways?

  • Version: 12.0.1
  • It only happends in tests cases
  • It still works as expected

@sunshinejr I just test with 13.0.0 and has same error log. But this issue not effect the test cases result(they pass all).

@sunshinejr The version of Moya is 13.0.0 , due to this error, it doesn't work

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Something new?

@huhaosanxiong @leohidalgo @smoothdvd I'm able to reproduce the issue and I'm currently troubleshooting this one. Gonna keep you all up to date.

Alright, the PR is in. This fixed our test suite from random logs so it looks fine. Should be releasing a version 13.0.1 when it's merged.

@sunshinejr Great work!

@sunshinejr Thanks

@sunshinejr Thanks

Moya 13.0.1 with a fix was just released 馃殌 Please give it a go and let me know if anything weird occurs again.

@sunshinejr Just upgrade to 13.0.1 and Moya works perfect!

Was this page helpful?
0 / 5 - 0 ratings