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)")
}
})
}
- I have no other async tests
- The test never fails due to exceeded timeout
- Since the test only crashes some of the time, I expect that the problem is a race condition somewhere, but I'm unsure where to look.
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.
testStartMonitoring()
doesnot crash in mine – Wells