What is the difference in semaphore vs the Expectation in iOS Unit Test for asynchronous api layer testing

459 views Asked by At

I have implemented using semaphore and expectation , the result is same. What is the fundamental difference between both.

// Using Expectation

func testDownloadWithExpectation(){

        let expect = expectation(description: "Download should exceed")

        if let url = URL(string: "https://httpbin.org"){
            Downloader.download(from: url, completionHandler: { (data, response, error) in
                XCTAssertNil(error, "\(String(describing: error?.localizedDescription)) error occured")
                XCTAssertNotNil(data,"No Payload returned")
                expect.fulfill()
            })
        }

        waitForExpectations(timeout: 10) { (error) in
            XCTAssertNil(error, "Test timed Out")
        }
    }

// Using Semaphore

    func testDownloadWIthSephaMore(){

        let sepahamore = DispatchSemaphore(value: 0)
        if let url = URL(string: "https://httpbin.org"){
            Downloader.download(from: url, completionHandler: { (data, response, error) in
                XCTAssertNil(error, "\(String(describing: error?.localizedDescription)) error occured")
                XCTAssertNotNil(data,"No Payload returned")
                sepahamore.signal()
            })
        }
        let timeout = DispatchTime.now() + DispatchTimeInterval.seconds(5)
        if sepahamore.wait(timeout: timeout) == DispatchTimeoutResult.timedOut {
            XCTFail("Test timed out")
        }
    }
1

There are 1 answers

0
jakhamma On

I believe that the expectation will wait for the expectation to be fulfilled, and will continue running other tests while waiting.

I think that the semaphore will block other tests from running while awaiting the semaphore sign