James Webster
James Webster

Reputation: 32066

Why does this Swift unit test crash when run in parallel with other tests?

I have a unit test that succeeds when it is run alone but crashes with an EXC_BAD_ACCESS (most of the time) on waitForExpectations when running alongside other tests.

func testStartMonitoring() {

    let mockLocationManager = MockLocationManager()
    let beaconListener = BeaconListener(locationManager: mockLocationManager, uuid: BeaconListenerTests.defaultUUID)

    let e = self.expectation(description: "Expected beacons to be detected")

    //If the listener calls back, the expectation succeeds.
    beaconListener.didReceiveNewRoomInformation = { data in

        //There are three entries in the test data
        XCTAssert(data.count == 3)

        e.fulfill()
    }

    //Start listening
    beaconListener.startListening()

    //Wait up to 15s for a response
    self.waitForExpectations(timeout: 15.0, handler: {error in
        if let error = error {
            print("\(error)")
        }
    })
}

enter image description here enter image description here


I've been able to reproduce this with simpler code too:

func testStartMonitoring() {

    let e = self.expectation(description: "Expected beacons to be detected")

    let deadlineTime = DispatchTime.now() + .seconds(1)
    DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
        e.fulfill()
    }

    //Wait up to 15s for a response
    self.waitForExpectations(timeout: 15.0, handler: {error in
    })
}

I ran the tests from the command line and found this extra piece of information:

Error Domain=IDETestOperationsObserverErrorDomain Code=5 "Early unexpected exit, operation never finished bootstrapping - no restart will be attempted" UserInfo={NSLocalizedDescription=Early unexpected exit, operation never finished bootstrapping - no restart will be attempted}

Some other answers indicate that this may be caused by system alerts. This is understandable, I am using location services which requires a permissions alert. However, the devices on which I'm running the tests already have accepted the permissions and therefore shouldn't be showing the alerts.

Upvotes: 6

Views: 1742

Answers (1)

Edan
Edan

Reputation: 167

I had a similar problem - a set of tests with multiple expectations crashing - and came across your question. It made me realize that its the permissions causing the problem - here for location manager, and speech recognition for me. So I mocked my authorization request class and injected a positive response.

You have to go searching for methods that are calling whatever it is that requires permission - they may or may not be the ones with expectations.

Upvotes: 2

Related Questions