|
|
|
@ -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
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|