1

セマフォと期待を使用して実装しましたが、結果は同じです。両者の根本的な違いは何ですか。

// 期待値の使用

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")
        }
    }

// セマフォの使用

    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")
        }
    }
4

1 に答える 1