Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 547
James Webster Avatar asked Oct 30 '22 16:10

James Webster


1 Answers

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.

like image 75
Edan Avatar answered Nov 15 '22 06:11

Edan