Signal-iOS/SignalServiceKit/tests/Util/OWSOperationTest.swift
Matthew Chen fcdca12cf5 Deprecate REST, Part 1
* Port socket manager to Swift.
* Clean up HTTP request success/failure state & errors.
* Rework network manager.
* Rework HTTP errors.
* Rework errors "properties": isRetryable, etc.
* Fix test breakage.
2021-08-18 14:25:36 -03:00

79 lines
2.2 KiB
Swift

//
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
//
import Foundation
import XCTest
import SignalCoreKit
@testable import SignalServiceKit
class OWSOperationTest: SSKBaseTestSwift {
private class TestOperation: OWSOperation {
let expectation: XCTestExpectation
init(expectation: XCTestExpectation) {
self.expectation = expectation
}
override func run() {
// no-op
}
override func didReportError(_ error: Error) {
XCTAssertTrue((error as NSError).isRetryable)
expectation.fulfill()
}
}
func test_castSwiftErrorToNSErrorThenSet() {
let expectedError = expectation(description: "didReportError")
let operation = TestOperation(expectation: expectedError)
let error = OWSRetryableError()
operation.reportError(error)
waitForExpectations(timeout: 0.1, handler: nil)
}
func test_NSError() {
let expectedError = expectation(description: "didReportError")
let operation = TestOperation(expectation: expectedError)
let error = OWSRetryableError()
operation.reportError(error)
waitForExpectations(timeout: 0.1, handler: nil)
}
func test_operationError() {
enum BarError: Error, IsRetryableProvider {
case bar
var isRetryableProvider: Bool {
return true
}
}
let expectedError = expectation(description: "didReportError")
let operation = TestOperation(expectation: expectedError)
operation.reportError(BarError.bar)
waitForExpectations(timeout: 0.1, handler: nil)
}
// MARK: -
func test_retryInterval() {
var totalInterval: TimeInterval = 0
for failureCount: UInt in 0..<110 {
let retryInterval: TimeInterval = OWSOperation.retryIntervalForExponentialBackoff(failureCount: failureCount)
totalInterval += retryInterval
let formattedTotal = OWSFormat.formatDurationSeconds(Int(totalInterval))
Logger.info("failureCount: \(failureCount), retryInterval: \(retryInterval), totalInterval: \(totalInterval) (\(formattedTotal))")
}
Logger.flush()
}
}