|
@@ -103,12 +103,15 @@ class RetryStrategyTests: XCTestCase {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func testDelayRetryStrategyExceededCount() {
|
|
func testDelayRetryStrategyExceededCount() {
|
|
|
-
|
|
|
|
|
- var blockCalled: [Bool] = []
|
|
|
|
|
|
|
+ let exp = expectation(description: #function)
|
|
|
|
|
+ let blockCalled: ActorArray<Bool> = ActorArray([])
|
|
|
|
|
|
|
|
let source = Source.network(URL(string: "url")!)
|
|
let source = Source.network(URL(string: "url")!)
|
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
|
|
|
|
|
|
|
|
+ let group = DispatchGroup()
|
|
|
|
|
+
|
|
|
|
|
+ group.enter()
|
|
|
let context1 = RetryContext(
|
|
let context1 = RetryContext(
|
|
|
source: source,
|
|
source: source,
|
|
|
error: .responseError(reason: .URLSessionError(error: E()))
|
|
error: .responseError(reason: .URLSessionError(error: E()))
|
|
@@ -119,9 +122,13 @@ class RetryStrategyTests: XCTestCase {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
XCTAssertNil(userInfo)
|
|
XCTAssertNil(userInfo)
|
|
|
- blockCalled.append(true)
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await blockCalled.append(true)
|
|
|
|
|
+ group.leave()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ group.enter()
|
|
|
let context2 = RetryContext(
|
|
let context2 = RetryContext(
|
|
|
source: source,
|
|
source: source,
|
|
|
error: .responseError(reason: .URLSessionError(error: E()))
|
|
error: .responseError(reason: .URLSessionError(error: E()))
|
|
@@ -134,21 +141,34 @@ class RetryStrategyTests: XCTestCase {
|
|
|
XCTFail("The decision should be `stop`")
|
|
XCTFail("The decision should be `stop`")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- blockCalled.append(true)
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await blockCalled.append(true)
|
|
|
|
|
+ group.leave()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- XCTAssertEqual(blockCalled.count, 2)
|
|
|
|
|
- XCTAssertTrue(blockCalled.allSatisfy { $0 })
|
|
|
|
|
|
|
+
|
|
|
|
|
+ group.notify(queue: .main) {
|
|
|
|
|
+ Task {
|
|
|
|
|
+ let result = await blockCalled.value
|
|
|
|
|
+ XCTAssertEqual(result.count, 2)
|
|
|
|
|
+ XCTAssertTrue(result.allSatisfy { $0 })
|
|
|
|
|
+ exp.fulfill()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ waitForExpectations(timeout: 1, handler: nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func testDelayRetryStrategyNotRetryForErrorReason() {
|
|
func testDelayRetryStrategyNotRetryForErrorReason() {
|
|
|
|
|
+ let exp = expectation(description: #function)
|
|
|
// Only non-user cancel error && response error should be retied.
|
|
// Only non-user cancel error && response error should be retied.
|
|
|
- var blockCalled: [Bool] = []
|
|
|
|
|
|
|
+ let blockCalled: ActorArray<Bool> = ActorArray([])
|
|
|
let source = Source.network(URL(string: "url")!)
|
|
let source = Source.network(URL(string: "url")!)
|
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
|
|
|
|
|
|
let task = URLSession.shared.dataTask(with: URL(string: "url")!)
|
|
let task = URLSession.shared.dataTask(with: URL(string: "url")!)
|
|
|
|
|
|
|
|
|
|
+ let group = DispatchGroup()
|
|
|
|
|
+ group.enter()
|
|
|
let context1 = RetryContext(
|
|
let context1 = RetryContext(
|
|
|
source: source,
|
|
source: source,
|
|
|
error: .requestError(reason: .taskCancelled(task: .init(task: task), token: .init()))
|
|
error: .requestError(reason: .taskCancelled(task: .init(task: task), token: .init()))
|
|
@@ -158,9 +178,13 @@ class RetryStrategyTests: XCTestCase {
|
|
|
XCTFail("The decision should be `stop` if user cancelled the task.")
|
|
XCTFail("The decision should be `stop` if user cancelled the task.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- blockCalled.append(true)
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await blockCalled.append(true)
|
|
|
|
|
+ group.leave()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ group.enter()
|
|
|
let context2 = RetryContext(
|
|
let context2 = RetryContext(
|
|
|
source: source,
|
|
source: source,
|
|
|
error: .cacheError(reason: .imageNotExisting(key: "any_key"))
|
|
error: .cacheError(reason: .imageNotExisting(key: "any_key"))
|
|
@@ -170,15 +194,25 @@ class RetryStrategyTests: XCTestCase {
|
|
|
XCTFail("The decision should be `stop` if the error type is not response error.")
|
|
XCTFail("The decision should be `stop` if the error type is not response error.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- blockCalled.append(true)
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await blockCalled.append(true)
|
|
|
|
|
+ group.leave()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- XCTAssertEqual(blockCalled.count, 2)
|
|
|
|
|
- XCTAssertTrue(blockCalled.allSatisfy { $0 })
|
|
|
|
|
|
|
+
|
|
|
|
|
+ group.notify(queue: .main) {
|
|
|
|
|
+ Task {
|
|
|
|
|
+ let result = await blockCalled.value
|
|
|
|
|
+ XCTAssertEqual(result.count, 2)
|
|
|
|
|
+ XCTAssertTrue(result.allSatisfy { $0 })
|
|
|
|
|
+ exp.fulfill()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ waitForExpectations(timeout: 1, handler: nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func testDelayRetryStrategyDidRetried() {
|
|
func testDelayRetryStrategyDidRetried() {
|
|
|
- var called = false
|
|
|
|
|
|
|
+ let called = ActorBox(false)
|
|
|
let source = Source.network(URL(string: "url")!)
|
|
let source = Source.network(URL(string: "url")!)
|
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
|
|
|
let context = RetryContext(
|
|
let context = RetryContext(
|
|
@@ -190,10 +224,16 @@ class RetryStrategyTests: XCTestCase {
|
|
|
XCTFail("The decision should be `retry`.")
|
|
XCTFail("The decision should be `retry`.")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- called = true
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ await called.setValue(true)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- XCTAssertTrue(called)
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ let result = await called.value
|
|
|
|
|
+ XCTAssertTrue(result)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|