Re-order files

pull/162/head
nielsandriesse 6 years ago
parent 86e54ad968
commit e186f9df5d

@ -32,7 +32,7 @@ class FailingTSAccountManager: TSAccountManager {
failure(VerificationFailedError()) failure(VerificationFailedError())
} }
override func registerForPushNotifications(pushToken: String, voipToken: String, success successHandler: @escaping () -> Void, failure failureHandler: @escaping (Error) -> Void) { override func registerForPushNotifications(pushToken: String, voipToken: String, isForcedUpdate: Bool, success successHandler: @escaping () -> Void, failure failureHandler: @escaping (Error) -> Void) {
if pushToken == PushNotificationRequestResult.FailTSOnly.rawValue || pushToken == PushNotificationRequestResult.FailBoth.rawValue { if pushToken == PushNotificationRequestResult.FailTSOnly.rawValue || pushToken == PushNotificationRequestResult.FailBoth.rawValue {
failureHandler(OWSErrorMakeUnableToProcessServerResponseError()) failureHandler(OWSErrorMakeUnableToProcessServerResponseError())
} else { } else {
@ -145,7 +145,7 @@ class AccountManagerTest: SignalBaseTest {
let expectation = self.expectation(description: "should fail") let expectation = self.expectation(description: "should fail")
firstly { firstly {
accountManager.updatePushTokens(pushToken: PushNotificationRequestResult.FailTSOnly.rawValue, voipToken: "whatever") accountManager.updatePushTokens(pushToken: PushNotificationRequestResult.FailTSOnly.rawValue, voipToken: "whatever", isForcedUpdate: false)
}.done { }.done {
XCTFail("Expected to fail.") XCTFail("Expected to fail.")
}.catch { _ in }.catch { _ in

@ -27,13 +27,14 @@ public final class LokiAPI : NSObject {
public static let errorHandlingQueue = DispatchQueue(label: "LokiAPI.errorHandlingQueue") public static let errorHandlingQueue = DispatchQueue(label: "LokiAPI.errorHandlingQueue")
// MARK: Settings // MARK: Settings
private static let useOnionRequests = true
private static let maxRetryCount: UInt = 4 private static let maxRetryCount: UInt = 4
private static let defaultTimeout: TimeInterval = 20 private static let defaultTimeout: TimeInterval = 20
private static let longPollingTimeout: TimeInterval = 40 private static let longPollingTimeout: TimeInterval = 40
private static var userIDScanLimit: UInt = 4096 private static var userIDScanLimit: UInt = 4096
internal static var powDifficulty: UInt = 1 internal static var powDifficulty: UInt = 1
/// - Note: Changing this on the fly is not recommended.
internal static var useOnionRequests = true
// MARK: Nested Types // MARK: Nested Types
public typealias RawResponse = Any public typealias RawResponse = Any

@ -4,50 +4,21 @@ import PromiseKit
import XCTest import XCTest
class OnionRequestAPITests : XCTestCase { class OnionRequestAPITests : XCTestCase {
private let maxRetryCount: UInt = 2 // Be a bit more stringent when testing
private let testPublicKey = "0501da4723331eb54aaa9a6753a0a59f762103de63f1dc40879cb65a5b5f508814"
func testOnionRequestSending() { func testOnionRequestSending() {
let semaphore = DispatchSemaphore(value: 0) let semaphore = DispatchSemaphore(value: 0)
var totalSuccessRate: Double = 0 var error: Error? = nil
let testCount = 10 LokiAPI.useOnionRequests = true
LokiAPI.getRandomSnode().then(on: OnionRequestAPI.workQueue) { snode -> Promise<LokiAPITarget> in let _ = attempt(maxRetryCount: maxRetryCount, recoveringOn: LokiAPI.workQueue) { [testPublicKey = self.testPublicKey] in
print("[Loki] [Onion Request API] Target snode: \(snode).") LokiAPI.getSwarm(for: testPublicKey)
return OnionRequestAPI.getPath(excluding: snode).map(on: OnionRequestAPI.workQueue) { _ in snode } // Ensure we only build a path once }.done(on: LokiAPI.workQueue) { _ in
}.done(on: OnionRequestAPI.workQueue) { snode in
var successCount = 0
let promises: [Promise<Void>] = (0..<testCount).map { _ in
let mockSessionID = "0582bc30f11e8a9736407adcaca03b049f4acd4af3ae7eb6b6608d30f0b1e6a20e"
let parameters: JSON = [ "pubKey" : mockSessionID ]
let (promise, seal) = Promise<Void>.pending()
OnionRequestAPI.sendOnionRequest(invoking: .getSwarm, on: snode, with: parameters, associatedWith: mockSessionID).done(on: OnionRequestAPI.workQueue) { json in
successCount += 1
print("[Loki] [Onion Request API] Onion request succeeded with result: \(json.prettifiedDescription).")
seal.fulfill(())
}.catch(on: OnionRequestAPI.workQueue) { error in
if case GCM.Error.fail = error {
print("[Loki] [Onion Request API] Onion request failed due to a decryption error.")
} else {
print("[Loki] [Onion Request API] Onion request failed due to error: \(error).")
}
seal.reject(error)
}.finally(on: OnionRequestAPI.workQueue) {
let currentSuccessRate = min((100 * Double(successCount)) / Double(testCount), 100)
print("[Loki] [Onion Request API] Current onion request success rate: \(String(format: "%.1f", currentSuccessRate))%.")
}
return promise
}
when(resolved: promises).done(on: OnionRequestAPI.workQueue) { _ in
totalSuccessRate = min((100 * Double(successCount)) / Double(testCount), 100)
semaphore.signal()
}
}.catch(on: OnionRequestAPI.workQueue) { error in
print("[Loki] [Onion Request API] Path building failed due to error: \(error).")
semaphore.signal() semaphore.signal()
}.catch(on: LokiAPI.workQueue) {
error = $0; semaphore.signal()
} }
semaphore.wait() semaphore.wait()
print("[Loki] [Onion Request API] Total onion request success rate: \(String(format: "%.1f", totalSuccessRate))%.") XCTAssert(error == nil)
XCTAssert(totalSuccessRate >= 90)
} }
// TODO: Test error handling
// TODO: Test race condition handling
} }

Loading…
Cancel
Save